编程练习,自己做的21点游戏
#include <stdio.h> #include <time.h> #include <stdlib.h> int N; //牌序 int integral_player; //玩家积分 int integral_computer; //电脑积分 int card[52]= {102,103,104,105,106,107,108,109,110,111,112,113,114,202,203,204,205,206,207,208,209,210,211,212,213,214 ,302,303,304,305,306,307,308,309,310,311,312,313,314,402,403,404,405,406,407,408,409,410,411,412,413,414, }; int player1[10]= {0,}; int player2[10]= {0,}; int player1_card=0; int player2_card=0; void show_cards(int i); //示牌 void riffle(); //洗牌 void Send_cards(); //发牌 void add_player(); //玩家要牌 void add_computer(); //电脑要牌 void cal_point(); //计算点数 void integral_read(); //积分读取,显示 void integral_write(); //积分写入 int main() { char choose; integral_read(); //积分读取 do { //printf("洗牌測试 \n"); riffle(); //洗牌 Send_cards(); //发牌 add_player(); //玩家要牌 add_computer(); //电脑要牌 cal_point(); //计算点数 integral_write(); //积分写入 printf("******************* SUCCESS ***********************\n"); printf("按 N 退出,其它键继续....\n\n"); fflush(stdin); choose=getchar(); printf("\n"); } while (choose !='n'&&choose!='N'); return 0; } void riffle() { int a,t,i; srand(time(0)); /*for (i=0; i<52; i++) { if (i%13==0) { printf("\n\n"); } show_cards(i); } printf("\n\n"); printf("上面是正常牌序,后面是洗牌后的牌序\n"); //洗牌測试*/ for (i=0; i<52; i++) { a=rand()%52; t=card[i]; card[i]=card[a]; card[a]=t; } N=0; //牌序重置; player1_card=0; //玩家牌序重置。 player2_card=0; //电脑牌序重置; /*for (i=0; i<52; i++) //洗牌測试 { if (i%13==0) { printf("\n\n"); } show_cards(i); } printf("\n\n");*/ } void Send_cards() { int j; printf("玩家拿牌:"); for (j=0; j<2; j++) { show_cards(N); player1[player1_card]=card[N]; player1_card++; N++; } printf("\n\n"); printf("电脑拿牌:"); for (j=0; j<2; j++) { show_cards(N); player2[player2_card]=card[N]; player2_card++; N++; } printf("\n\n"); } void show_cards(int i) { int choose; choose=card[i]/100; switch (choose) { case 1: if (card[i]%100==14) { printf("SA\t"); break; } else if (card[i]%100==13) { printf("SK\t"); break; } else if (card[i]%100==12) { printf("SQ\t"); break; } else if (card[i]%100==11) { printf("SJ\t"); break; } else { printf("S%d\t",card[i]%100); break; } case 2: if (card[i]%100==14) { printf("DA\t"); break; } else if (card[i]%100==13) { printf("DK\t"); break; } else if (card[N]%100==12) { printf("DQ\t"); break; } else if (card[i]%100==11) { printf("DJ\t"); break; } else { printf("D%d\t",card[i]%100); break; } case 3: if (card[i]%100==14) { printf("CA\t"); break; } else if (card[i]%100==13) { printf("CK\t"); break; } else if (card[i]%100==12) { printf("CQ\t"); break; } else if (card[i]%100==11) { printf("CJ\t"); break; } else { printf("C%d\t",card[i]%100); break; } case 4: if (card[N]%100==14) { printf("HA\t"); break; } else if (card[N]%100==13) { printf("HK\t"); break; } else if (card[N]%100==12) { printf("HQ\t"); break; } else if (card[i]%100==11) { printf("HJ\t"); break; } else { printf("H%d\t",card[i]%100); break; } } } void add_player() { int choose; do { printf("玩家: 1.要牌 2.不要\040"); scanf("%d",&choose); switch (choose) { case 1: show_cards(N); player1[player1_card]=card[N]; player1_card++; N++; printf("\n"); continue; case 2: printf("不要\n"); return; } } while (1); } void add_computer() { int i; while (1) { int choose=2; int point=0; for (i=0; i<player2_card; i++) { point+=player2[i]%100; } if (point<=18) { choose=1; } printf("电脑:1.要牌 2.不要\n"); switch (choose) { case 1: printf("要一张\n"); show_cards(N); player2[player2_card]=card[N]; player2_card++; N++; printf("\n"); continue; case 2: printf("不要\n"); return; } } } void cal_point() { int winner1; int winner2; int i; int point1; int point2; int point; point1=0; point2=0; for (i=0; i<player1_card; i++) { point=player1[i]%100; switch(point) { case 14: point=11; break; case 13: point=10; break; case 12: point=10; break; case 11: point=10; break; case 10: point=10;; break; default : point=player1[i]%100; break; } point1+=point; } for (i=0; i<player2_card; i++) { point=player2[i]%100; switch(point) { case 14: point=11; break; case 13: point=10; break; case 12: point=10; break; case 11: point=10; break; case 10: point=10;; break; default : point=player2[i]%100; break; } point2+=point; } printf("玩家的点数为:%d\t",point1); printf("电脑的点数为: %d\n\n",point2); if (point1>21) { printf("玩家爆掉\n\n"); winner1=0; } else winner1=1; if (point2>21) { printf("电脑爆掉\n\n"); winner2=0; } else winner2=1; if (winner1>winner2){ printf("玩家胜,得1分\n\n"); integral_player++; return; } else if((winner1=winner2==1)){ if (point1>point2){ printf("玩家胜。得1分\n\n"); integral_player++; return; } else{ printf("电脑胜,得1分\n\n"); integral_computer++; return; } } else if ((winner1=winner2==0)){ printf("电脑胜,得1分\n\n"); integral_computer++; return; } else if (winner1<winner2){ printf("电脑胜。得1分\n\n"); integral_computer++; return; } else return; } void integral_read() { FILE *fp; if ((fp=fopen("integral.txt","r"))==NULL) { printf("integral file cannot open!"); exit(0); } fscanf(fp,"%d %d",&integral_player,&integral_computer); //读入积分 fclose(fp); printf("玩家一共%d胜 %d败\n\n",integral_player,integral_computer); } void integral_write() { FILE *p; if ((p=fopen("integral.txt", "w"))==NULL) { printf("integral.txt file cannot open!"); exit(0); } fprintf(p,"%d %d",integral_player,integral_computer); fclose(p); }
还有不完好的地方
比方 A仅仅能作为11使用 不能看情况做1 计算
假设2个牌同样 不算豹子
下次继续改进