提交 217579c2 编写于 作者: 云里云空's avatar 云里云空

Upload New File

上级 fc9a4a1b
#pragma once
#include <iostream>
#include <string>
#include <stdlib.h>
#include <time.h>
#include <iomanip>
using namespace std;
void text() {
cout << "\n\n" << "through text success" << "\n";
}
void charsort(char *p0) {
int cout = 1;
char* p, key;
while (cout) {
cout = 0;
p = p0;
while (*(p+1)!='\0') {
if (*p > *(p+1)) {
cout = 1;
key = *p;
*p = *(p + 1);
*(p + 1) = key;
}
p++;
}
}
}
int conclude(char* p, char head, char foot) {
for (int i = 0; p[i+1] != '\0'; i++) {
if (p[i] >= p[i + 1] or p[i] <head or p[i] > foot) {
return 0;
}
}
return 1;
}
struct paper { //扑克牌,每个paper代表一张扑克牌
string style;
int number;
string color;
int ready;
int order;
struct paper* next;
int sign; //标记,待出牌为0,选中欲出牌为1,已出牌为-1
};
class puke { //一副扑克牌,每个对象具有54张扑克牌成员和一个发牌方法
protected :
paper new_puke[54];
public :
puke();
int prints(); //输出初始化牌堆
paper** prove(int x,int y,int z);
void print(int x); //输出牌堆中序号x的牌
int printhand(paper *p); //输出数组中每个值所对应的牌
paper* sort(paper* headpuke);
void maxpuke(int x);
};
paper **puke::prove(int x,int y,int z) {
int pukenum[54],pukenow,pukeless=54;
for (int i = 0; i < 54; i++) {
pukenum[i] = i;
}
paper** nes;
nes = new paper* [x+1];
for (int i = 0; i <= x; i++) {
nes[i] = NULL;
}
for (int i = 0; i < x; i++) {
srand(time(nullptr));//设置随机数种子
for (int j = 0; j < y; j++) {
pukenow = rand() % pukeless;//产生一个随机数,随机发出一张牌
new_puke[pukenum[pukenow]].next = nes[i];
nes[i] = &new_puke[pukenum[pukenow]];
pukeless--;
pukenum[pukenow] = pukenum[pukeless];
}
}
for (int i = 0; i < pukeless; i++) {
new_puke[pukenum[i]].next = nes[x];
nes[x] = &new_puke[pukenum[i]];
}
return nes;
}
puke::puke() { //初始化每张扑克牌
paper* p;
string color;
string s001 = "A 2 3 4 5 6 7 8 9 10J Q K ";
p = &new_puke[0];
p->style = "王";
p->color = "大";
p->number = 30;
p->ready = 15;
p->order = 0;
p->next = NULL;
p++;
p->style = "王";
p->color = "小";
p->number = 29;
p->ready = 14;
p->order = 1;
p->next = NULL;
for (int i = 0; i < 4; i++) {
switch (i) {
case 0:
color = "红桃";
break;
case 1:color = "方块";
break;
case 2:color = "黑桃";
break;
case 3:color = "梅花";
}
for (int j = 0; j < 13; j++) {
p++;
p->color = color;
p->number = j+1;
p->ready = j + 1;
p->style = s001.substr(j * 2, 2);
p->order = i * 13 + j + 2;
p->next = NULL;
}
}
}
int puke::prints() {
for(int i=0;i<54;i++){
cout << new_puke[i].color << new_puke[i].style << "\n";
}
return 0;
}
void puke::print(int x) {
cout << setw(5) << new_puke[x].color << new_puke[x].style;
}
paper* puke::sort(paper* headpuke) {
int cout = 1;
paper* p0 = new paper;
p0->next = headpuke;
p0->number = 0;
paper *p, *key;
while (cout) {
cout = 0;
p = p0->next, key = p0;
while(p->next != NULL){
if (p->number > p->next->number) {
cout = 1;
key->next = p->next;
p->next = key->next->next;
key->next->next = p;
}
else { p = p->next; }
key = key->next;
}
}
p = p0->next;
return p;
free(p0);
}
int puke::printhand(paper* p) {
int count = 0;
while(p != NULL) {
count++;
cout << setw(5) << p->color << p->style;
p = p->next;
}
return count;
}
void puke::maxpuke(int x) {
while (x != 0) {
for (int i = 0; i < 54; i++) {
if (new_puke[i].number == x) {
new_puke[i].number = x + 13;
}
}
x--;
}
}
class dofamer :public puke { //斗地主牌桌,定义斗地主出牌及判定规则
public:
dofamer();
int since[20],record[20],recordnum[20],recordcount[20],gamerplayer = 2,pukecounts = 20,nogo = 2,sincerecord = 0,nowrecord = 0,sincegameplayer = 2;
char sing[22] = "abcdefghijklmnopqrst0",input[21];
paper** player;
void gameon();
void first(); //初始化出牌记录
void recordprint();
void formulatesince();
void formulatepukesign();
void write(paper* p); //将指针指向牌做预出牌记录
void delect(paper* p);
int computerchoice(int num,paper *p);
int choice(int play); //决定要出的牌,记录在record数组中。
int memmory();
int gopuke(); //出牌成功,将牌从手牌中删除,若手牌为零,返回0
int all();
int alone();
int couple();
int alonesize();
int couplesize();
int threealone();
int threecouple();
int plane();
int exploudcouple();
int exploud();
int (dofamer::* ragular)() = &dofamer::all; //规则判定,出牌是否合法,合法返回1
};
dofamer::dofamer() {
this->maxpuke(2);
this->first();
}
void dofamer::recordprint() {
cout << "\n"<<"recordnum: ";
for (int i = 0; i < 20; i++) {
cout << setw(3) << recordnum[i];
}
cout << "\n"<<"recordcount:";
for (int i = 0; i < 20; i++) {
cout << setw(3) << recordcount[i];
}
cout << "\n"<<"since: ";
for (int i = 0; i < 20; i++) {
cout << setw(3) << since[i];
}
cout << "\n"<<"record: ";
for (int i = 0; i < 20; i++) {
cout << setw(3) << record[i];
}
}
void dofamer::gameon() {
paper* p = player[2];
while (p->next != NULL) {
p = p->next;
}
p->next = player[3];
player[2] = this->sort(player[2]);
}
void dofamer::first() {
for (int i = 0; i < 20; i++) {
record[i] = -1;
recordnum[i] = 0;
recordcount[i] = 0;
input[i] = '\0';
}
}
void dofamer::formulatesince() {
for (int i = 0; i < 20; i++) {
since[i] = 0;
}
}
void dofamer::formulatepukesign() {
paper* p = player[gamerplayer];
while (p != NULL) {
p->sign = 0;
p = p->next;
}
}
void dofamer::write(paper* p) {
int i = 0;
while (recordnum[i] != p->number and recordnum[i] != 0) {
i++;
}
recordnum[i] = p->number;
recordcount[i]++;
}
void dofamer::delect(paper* p) {
sincerecord++;
paper* q = player[gamerplayer];
if (p == q) {
player[gamerplayer] = p->next;
p->next = NULL;
}
else {
while (q->next != p) {
q=q->next;
}
q->next = p->next;
p->next = NULL;
}
}
int dofamer::computerchoice(int num,paper *p) {
// cout << num << "\n";
if (num == 0) {
// text();
// cout << num << "\n";
this->memmory();
if ((this->*this->ragular)()) {
return 1;
}
else {return 0;}
}
else {
num--;
while (p != NULL) {
while (p->sign == 1) {
if (p->next != NULL) {
p = p->next;
}
else { return 0; }
}
p->sign = 1;
if (this->computerchoice(num,p->next) == 0) {
p->sign = 0;
p = p->next;
}
else { return 1; }
}
return 0;
}
return 0;
}
int dofamer::choice(int play) {
int counts = 0,j=0;
this->first();
paper* here = player[gamerplayer];
if(gamerplayer == play){
cout <<"\n\n"<< "您的手牌(请输入每张牌对应的小写字母出牌,若过则输入0):"<<"\n";
counts = this->printhand(here);//输出当前玩家手中剩余牌并返回牌数
pukecounts = counts;
cout << "\n" ;
for (int i = 0; i < counts; i++) {
cout << setw(5) << sing[i]<<" ";
}
cout << "\n";
cin >> input;
charsort(input);
cout << "\n";
if (input[0] == '0') {
if (nogo < 2) {
return 0;
}
else {
cout << "\n" << "您必须选择出牌:";
return this->choice(play);
}
}
if (nogo == 2) {
this->formulatesince();
ragular = &dofamer::all;
}
if (!conclude(input,sing[0],sing[counts-1]) ) {
cout << "\n" << "您的输入不符合规则,请重新输入——" << "\n";
return this->choice(play);
}
else {
for (int i = 0; i < counts; i++) {
if (sing[i] == input[j]) {
here->sign = 1;
j++;
}
here = here->next;
}
this->memmory();
// this->recordprint();
if (!(this->*this->ragular)()) {
this->formulatepukesign();
cout << "\n" << "您的出牌不符合规则,请重新出牌——" << "\n";
return this->choice(play);
}
return 1;
}
}
else {
if (sincegameplayer != 2 and gamerplayer != 2 and sincegameplayer != gamerplayer) { return 0; }
while (here != NULL) {
counts++;
here = here->next;
}
pukecounts = counts;
if (nogo == 2) {
ragular = &dofamer::all;
this->formulatesince();
while (counts != 0) {
if (this->computerchoice(counts,player[gamerplayer]) == 1) {
return 1;
}
else {
// cout << counts;
counts--; }
}
}
else { return this->computerchoice(sincerecord,player[gamerplayer]); }
}
}
int dofamer::memmory() {
this->first();
nowrecord = 0;
paper* here = player[gamerplayer];
int j = 0;
while (here != NULL) {
if (here->sign == 1) {
record[j] = here->order;
nowrecord++;
write(here);
j++;
}
here = here->next;
}
return 1;
}
int dofamer::gopuke() {
sincerecord = 0;
sincegameplayer = gamerplayer;
paper* p = player[gamerplayer];
int i = 0;
while (record[i] != -1) {
p = &new_puke[record[i]];
cout << setw(5) << p->color << p->style;
this->delect(p);
i++;
pukecounts--;
}
return pukecounts;
}
int dofamer::all() {
if (alone()) {
return 1;
}
if(couple()) {
return 1;
}
if (alonesize()) {
return 1;
}
if (couplesize()) {
return 1;
}
if (threealone()) {
return 1;
}
if (threecouple()) {
return 1;
}
if (plane()) {
return 1;
}
/* if (exploudcouple()) {
return 1;
}*/
if (exploud()) {
return 1;
}
return 0;
}
int dofamer::alone() {
// this->recordprint();
if (this->exploud() == 1) {
return 1;
}
if (recordnum[0] != 0 and recordnum[1] == 0 and recordcount[0]==1) {
if (since[0] < recordnum[0]) {
since[0] = recordnum[0];
ragular = &dofamer::alone;
cout << "\n\n" << gamerplayer << "号玩家:单走一张:";
return 1;
}
else {
// cout << "\n" << 11111 << "\n";
return 0; }
}
else {
// cout << "\n" << 2222 << "\n";
return 0; }
}
int dofamer::couple() {
if (this->exploud() == 1) {
return 1;
}
if (recordnum[0] != 0 and recordnum[1] == 0 and recordcount[0] == 2) {
if (since[0] < recordnum[0]) {
since[0] = recordnum[0];
ragular = &dofamer::couple;
cout << "\n\n" << gamerplayer << "号玩家:对子:";
return 1;
}
else { return 0; }
}
else { return 0; }
}
int dofamer::alonesize() {
if (this->exploud() == 1) {
return 1;
}
int i = 0;
if (recordnum[0] <= since[0]) {
return 0;
}
while (recordnum[i + 1] != 0) {
if (recordnum[i] == 14 or recordcount[i] != 1 or recordnum[i + 1] != recordnum[i] + 1) {
return 0;
}
i++;
}
if (since[0] != 0) {
if (recordcount[i] != 1 or since[i] == 0 or since[i + 1] != 0 or i < 4) { return 0; }
}
else{if(recordcount[i] != 1 or i < 4) return 0;}
for (int j = 0; j <= i; j++) {
since[j] = recordnum[j];
}
ragular = &dofamer::alonesize;
cout << "\n\n" << gamerplayer << "号玩家:顺子:";
return 1;
}
int dofamer::couplesize() {
if (this->exploud() == 1) {
return 1;
}
int i = 0;
if (recordnum[0] <= since[0]) {
return 0;
}
while (recordnum[i + 1] != 0) {
if (recordnum[i] == 14 or recordcount[i] != 2 or recordnum[i + 1] != recordnum[i] + 1) {
return 0;
}
i++;
}
if (since[0] != 0) {
if (recordcount[i] != 2 or since[i] == 0 or since[i + 1] != 0 or i < 4) { return 0; }
}
else { if (recordcount[i] != 2 or i < 4) return 0; }
for (int j = 0; j <= i; j++) {
since[j] = recordnum[j];
}
ragular = &dofamer::alonesize;
cout << "\n\n" << gamerplayer << "号玩家:双顺子:";
return 1;
}
int dofamer::threealone() {
if (this->exploud() == 1) {
return 1;
}
if (recordcount[0] == 1 and recordcount[1] == 3 and recordcount[2] == 0) {
if (recordnum[1] <= since[0]) {
return 0;
}
since[0] = recordcount[1];
ragular = &dofamer::threealone;
cout << "\n\n" << gamerplayer << "号玩家:三带一:";
return 1;
}
if (recordcount[0] == 3 and recordcount[1] == 1 and recordcount[2] == 0) {
if (recordnum[0] <= since[0]) {
return 0;
}
since[0] = recordcount[0];
ragular = &dofamer::threealone;
cout << "\n\n" << gamerplayer << "号玩家:三带一:";
return 1;
}
return 0;
}
int dofamer::threecouple() {
if (this->exploud() == 1) {
return 1;
}
if (recordcount[0] == 2 and recordcount[1] == 3 and recordcount[2] == 0) {
if (recordnum[1] <= since[0]) {
return 0;
}
since[0] = recordcount[1];
ragular = &dofamer::threecouple;
cout << "\n\n" << gamerplayer << "号玩家:三带一对:";
return 1;
}
if (recordcount[0] == 3 and recordcount[1] == 2 and recordcount[2] == 0) {
if (recordnum[0] <= since[0]) {
return 0;
}
since[0] = recordcount[0];
ragular = &dofamer::threecouple;
cout << "\n\n" << gamerplayer << "号玩家:三带一对:";
return 1;
}
return 0;
}
int dofamer::plane() {
if (this->exploud() == 1) {
return 1;
}
int i = 0, j = 0,key =0, nowrecordcopy = nowrecord;
while (since[i] != 0) {i++;}
while (recordnum[j] != 0) { j++; }
i--, j--;
if (j <= 0) { return 0; }
while (recordcount[j] != 3 or recordnum[j] != recordnum[j-1]+1 or recordcount[j-1] != 3) {
j--;
if (j == 0) { return 0; }
}
if (i != -1) {
if (since[i] >= recordnum[j]) {
return 0;
}
}
while (nowrecordcopy > 0 and j != -1 and recordcount[j] ==3) {
key++;
i--;
j--;
nowrecordcopy -= 4;
}
i++, j++;
if (nowrecordcopy == 0) {
if (since[0] != 0) {
if (i!=0 or since[i] >= recordnum[j]) {
return 0;
}
}
for (key--; key != 0; key--) {
since[key] = recordnum[j + key];
}
ragular = &dofamer::plane;
cout << "\n\n" << gamerplayer << "号玩家:飞机:";
return 1;
}
return 0;
}
int dofamer::exploudcouple() {
if (this->exploud() == 1) {
return 1;
}
return 1;
}
int dofamer::exploud() {
if (recordnum[0] == 29 and recordnum[1] == 30 and recordnum[2] == 0) {
this->formulatesince();
since[0] = 100;
ragular = &dofamer::exploud;
cout << "\n\n" << gamerplayer << "号玩家:王炸:";
return 1;
}
if (recordcount[0] == 4 and recordcount[1] == 0) {
if (ragular == &dofamer::exploud) {
if (recordnum[0] > since[0]) {
this->formulatesince();
since[0] = recordnum[0];
ragular = &dofamer::exploud;
cout << "\n\n" << gamerplayer << "号玩家:炸弹:";
return 1;
}
return 0;
}
else {
this->formulatesince();
since[0] = recordnum[0];
ragular = &dofamer::exploud;
cout << "\n\n" << gamerplayer << "号玩家:炸弹:";
return 1;
}
}
else { return 0; }
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册