C语言发牌游戏 21点

编程入门 行业动态 更新时间:2024-10-10 17:30:41

C<a href=https://www.elefans.com/category/jswz/34/1770116.html style=语言发牌游戏 21点"/>

C语言发牌游戏 21点

非常简单的实现发牌游戏 21点

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<string.h>
#include<ctype.h>
#define NUMBER 13
#define SIZE 1000
#define length 100
//玩家模型 
struct Player{char name[length];int points;int refuse;    
};
int Shuffle_cards(int S_cards[]);
int play_game();
int Reset_game(struct Player* players[],int bots);
struct Player *Create_bot(char name[]);
int IS_card(int Is_card[],int a);
//重置游戏,角色不释放 
int Reset_game(struct Player* players[],int bots){int i;for(i=0;i<bots;i++){players[i]->points=0;players[i]->refuse=1;}return 1;
} 
int Read_line(char str[],int n);
//字符串输入函数 
int Read_line(char str[],int n){int ch,i=0;while(isspace(ch=getchar()));while(ch!='\n'&&ch!=EOF){if(i<n)str[i++]=ch;ch=getchar();}str[i]='\0';return i;
}
//当前牌在牌库中是否发完 
int IS_card(int Is_card[],int a){if(Is_card[a])return 1;elsereturn 0;
}
//随机发牌 
int Shuffle_cards(int S_cards[]){int a,i;while(1){a=rand()%NUMBER+1;if(IS_card(S_cards,a)){for(i=0;i<NUMBER;i++){if(a==S_cards[i]){S_cards--;break;}}break;}else{break;}}return a;
}
//创建角色 
struct Player *Create_bot(void){struct Player *p;p=(Player*)malloc(sizeof(struct Player));if(p==NULL){printf("Create bot failed!\n");}Read_line(p->name,length);p->points=0;p->refuse=1;return p;
}
int play_game(){int i,bots_num,cards[NUMBER]={4},card,choose,max,maxid,players[4]={0},drawCOunt=0;struct Player *plays[5];printf("Welcome you play game!\n");printf("Now,I will tell you rules of the game:\n");printf("1.you need to choose the number of robots to fight against you.\n");printf("2.you will get the cards in the game.If the sum of the card points is greater than 21, you will lose the game.The cards will be distributed isx times in total.During this period,you can choose to get the cards,you will not get the cards .Finally,you can settle who has the largest sum of the card points.\n");printf("Please input no more than three robots to fight:");scanf("%d",&bots_num);printf("Please input you name:");plays[0]=Create_bot();i=1;while(bots_num--){printf("Please input bots name:");	plays[i++]=Create_bot();}bots_num=i;while(1){if(plays[0]->refuse){card=Shuffle_cards(cards);printf("%s Player you take a card: %d\n",plays[0]->name,card);plays[0]->points+=card;printf("Sum of current cards is:%d\n",plays[0]->points);if(plays[0]->points>21){printf("Sorry, your total points exceeded 21 and you lost the game!^M^\n");printf("Whether %s players continue to play the game?(Yes/1 or No/0)",plays[0]->name);scanf("%d",&choose);if(choose){Reset_game(plays,bots_num);continue;}else{return 0;}}		}else{max=plays[0]->points;//寻找胜利者 for(i=0;i<bots_num;i++){if(max<=plays[i]->points){max=plays[i]->points;maxid=i;}}//寻找平局的可能for(i=0;i<bots_num;i++){if(plays[i]->points==max){players[i]=1;drawCOunt++;}}printf("Your final total points are:%d\n",plays[0]->points);for(i=1;i<bots_num;i++){printf("Botname %s total points are:%d\n",plays[i]->name,plays[i]->points);}if((maxid==0)&&(drawCOunt==1)){//玩家获胜 printf("Congratulations on winning the game!^v^\n");printf("Whether %s players continue to play the game?(Yes/1 or No/0)",plays[0]->name);scanf("%d",&choose);if(choose){Reset_game(plays,bots_num);continue;}else{return 1;}}	else{// 电脑获胜 printf("Botname %s won the game! Sorry you lost the game!^m^\n",plays[maxid]->name);printf("Whether %s players continue to play the game?(Yes/1 or No/0)",plays[0]->name);scanf("%d",&choose);if(choose){Reset_game(plays,bots_num);continue;}else{return 0;}}if((maxid==0)&&(drawCOunt>1)){//玩家和电脑平局 for(i=1;i<bots_num;i++){if(players[i]){printf("Botname%s draw with ",plays[i]->name);}}printf("you!\n");printf("Whether %s players continue to play the game?(Yes/1 or No/0)",plays[0]->name);scanf("%d",&choose);if(choose){Reset_game(plays,bots_num);continue;}else{return 2;}}}if(plays[0]->refuse){for(i=1;i<bots_num;i++){if(plays[i]->refuse){if(plays[i]->points>=10){card=Shuffle_cards(cards);while(1){if(card>3) card=Shuffle_cards(cards);else break;}}plays[i]->points+=card;if(plays[i]->points>=19) plays[i]->refuse=1;}else{continue;	}}printf("Do you want to continue the next deal?(Yes/1 or No/0)");scanf("%d",&choose);plays[0]->refuse=choose; }  }
}
int main(){srand(time(NULL));play_game();return 0;
}

更多推荐

C语言发牌游戏 21点

本文发布于:2024-03-13 18:56:21,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1734602.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:语言   游戏

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!