简单的斗地主
复习一下C语言
实现的点:
1 生成不重复的牌
2 分牌
3 打牌(当前是一张一张出)
#include#include #include char A[20] = "牛魔王"; char B[20] = "白晶晶"; char C[20] = "至尊宝"; int owner = 0; //地主是谁 enum cards{ meihua = 1, hongtao, fangkuai, heitao, smallKing, bigKing, }; typedef struct card{ int shape; int number; }cardPoker; cardPoker Bit[55]; cardPoker playerA[21]; cardPoker playerB[21]; cardPoker playerC[21]; cardPoker BosCard[4]; cardPoker NoUseCard = {0,0}; cardPoker cardTemp = {0,0}; void initCards(cardPoker player[],int n) { for(int i = 0; i < n;i++) { player[i].shape = 0; player[i].number = 0; } } void initAllCards(void) { initCards(Bit,55); initCards(playerA,21); initCards(playerB,21); initCards(playerC,21); initCards(BosCard,4); } //发牌给某位玩家 void handCards(cardPoker player[],int start,int end) { for(int i = 0,j = start;i <= end - start;i++,j++) { player[i] = Bit[j]; } } //发牌 void setCards(void) { handCards(playerA,0,16); handCards(playerB,17,33); handCards(playerC,34,50); handCards(BosCard,51,53); } int cardNum = 0; bool check(cardPoker a) { if(cardNum == 0) return 0; for(int i = 0;i < cardNum;i++) { if((a.shape == Bit[i].shape) && (a.number == Bit[i].number)) //if(a == Bit[i]) 结构变量不能直接比较,但是可以赋值 { return 1; } } return 0; } void insert(cardPoker a) { if(cardNum == 0) { Bit[cardNum] = a; cardNum++; } else if(check(a) == 1) { return; } else { Bit[cardNum -1] = a; cardNum++; } } void cardMake(void) { int shape = 0,number = 0; cardPoker card = {0,0}; srand((unsigned)time(NULL)); while(cardNum < 55) { shape = rand()%6 + 1; number = rand()%13 + 1; if(shape == 5) //小王处理 { number = 14; } if(shape == 6)//大王处理 { number = 15; } card.shape = shape; card.number = number; insert(card); } if(cardNum >= 55) { cardNum = 54; } return; } void sortcards(cardPoker a[]) { int lenth = 0; while(a[lenth].shape != 0) { lenth++; } int flag = 0; cardPoker temp = {0,0}; for(int i = lenth-1;i >= 1;i--) { for(int j = 0; j a[j+1].number) { temp = a[j]; a[j] = a[j+1]; a[j+1] = temp; flag = 1; } } if(flag == 0) { break; } } } void print(cardPoker card) { switch(card.shape){ case meihua: printf("梅花",3);break; case hongtao: printf("红桃",4);break; case fangkuai: printf("方块",5);break; case heitao: printf("黑桃",6);break; case smallKing: printf("小王");break; case bigKing: printf("大王");break; default:break; } //打印牌点 switch(card.number){ case 1: printf("3 ");break; case 2: printf("4 ");break; case 3: printf("5 ");break; case 4: printf("6 ");break; case 5: printf("7 ");break; case 6: printf("8 ");break; case 7: printf("9 ");break; case 8: printf("10 ");break; case 9: printf("J ");break; case 10: printf("Q ");break; case 11: printf("K ");break; case 12: printf("A ");break; case 13: printf("2 ");break; default: printf(" ");break; } } void show(cardPoker p[]) { //定义玩家 if(p == playerA) printf("<玩家%s的牌>:n",A); else if(p == playerB) printf("<玩家%s的牌>:n",B); else if(p == playerC) printf("<玩家%s的牌>:n",C); else if(p == BosCard) printf("<地主牌>:n"); else if(p == Bit) printf("<随机发牌序列>:n"); for(int i = 0;p[i].shape != 0; i++) { print(p[i]); } printf("n"); //跨行结束 } //此函数只有一个功能,把地主牌给到玩家 void addCards(cardPoker player[],cardPoker b[]) { int i = 0,j =0; while(player[i].shape != 0) { i++; } while(b[j].shape != 0) { player[i] = b[j]; i++; j++; } } //地主选择 void choose(void) { srand((unsigned)time(NULL)); int i = rand()%3; if(i == 0) { owner = 0; printf("玩家%s当地主!n",A); addCards(playerA,BosCard); } else if(i == 1) { owner = 1; printf("玩家%s当地主!n",B); addCards(playerB,BosCard); } else { owner = 2; printf("玩家%s当地主!n",C); addCards(playerC,BosCard); } } //显示大家的牌 void showAllPlayer(void) { printf("--------------------------------------------n"); show(playerA); show(playerB); show(playerC); printf("--------------------------------------------n"); } //发牌阶段 void part1(void) { initAllCards; cardMake(); setCards(); //show(Bit); printf("玩家%s、%s、%s加入本局游戏,开始发牌!n",A,B,C); showAllPlayer(); show(BosCard); printf("玩家地主选择:n"); choose(); //printf("选择地主后的牌:n"); //showAllPlayer(); printf("各位玩家整理牌:n"); sortcards(playerA); sortcards(playerB); sortcards(playerC); show(playerA); show(playerB); show(playerC); } //看当前玩家有没有这个牌 bool findCard(cardPoker player[],cardPoker temp) { int i = 0; int findFlag = 0; while(player[i].number != 0) { if(player[i].number == temp.number) { findFlag = 1; } i++; } return findFlag; } //删除当前玩家手里的这个牌:相当于打出去了 void delCard(cardPoker player[],cardPoker temp) { int i = 0; if(findCard(player,temp)) { while(player[i].number != temp.number) { i++; } while(player[i+1].shape != 0) { player[i] = player[i+1]; i++; } player[i] = NoUseCard; } } //出牌1 出最小的牌 void OutSmallCard(cardPoker player[]) { int i = 0; while(player[i+1].shape != 0) { player[i] = player[i+1]; i++; } player[i] = NoUseCard; } //出牌2 比别人大的牌 bool OutBitCard(cardPoker player[],cardPoker temp) { int i = 0; while(player[i].number != 0) { if(player[i].number > temp.number) { print(player[i]); cardTemp = player[i]; delCard(player,player[i]); return 1; } i++; } return 0; } //出牌 bool play(cardPoker player[],cardPoker temp) { int CanOut = 0; if(temp.shape == 0) { cardTemp = player[0]; OutSmallCard(player); print(cardTemp); if(player[0].shape == 0) { printf("老子没牌了!"); } printf("n"); CanOut = 1; } else { if(OutBitCard(player,temp)) { if(player[0].shape == 0) { printf("老娘打完了!n"); } else { printf("管上!n"); } CanOut = 1; } else { printf("臣妾要不起啊!n"); CanOut = 0; } } return CanOut; } //接牌 //bool fight(cardPoker card) //打牌阶段 void part2(void) { int prtPlayer = owner; //首先地主出牌 int fCount = 0; cardPoker *p; int BitState = 0; while(1) { if(prtPlayer == 0) { p = playerA; printf("现在%s出牌:",A); } else if(prtPlayer == 1) { p = playerB; printf("现在%s出牌:",B); } else { p = playerC; printf("现在%s出牌:",C); } BitState = play(p,cardTemp); if(BitState == 0) { fCount++; } else { fCount = 0; } if(fCount >= 2) { showAllPlayer(); fCount = 0; cardTemp = NoUseCard; } if(p[0].shape == 0) { printf("游戏结束!n"); break; } prtPlayer++; if(prtPlayer >= 3) { prtPlayer = 0; } } if(prtPlayer == owner) //当前获胜的玩家拥有地主牌就是地主胜利 { printf("地主获得胜利!n"); } else { printf("农民获得胜利!n"); } } int main(void) { part1(); part2(); return 0; }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)