新人做的第一个小游戏,以后可能会改为更为严谨的规则,以及加入筹码。
代码如下:
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
static int yes1 = 1, yes2 = 1;/*非0代表能继续摸牌 */
void introduction()/*介绍游戏规则 */
{
printf("BlackJack(21点)游戏规则:\n2至10点数不变,JQK点数为10,A点数可为11或1\n总点数越接近21点的为胜者,总点数超过21点清零\n--------------------\n");
}
int getsum(char poker[21])/*输入牌组,输出当前牌组的点数 */
{
int numA = 0, sum = 0, x = 0;
for (int i = 0; poker[i]; i++) {
if (poker[i] == 'A') numA++;
switch (poker[i]) {
case 'A': sum += 11; break; /*牌为A加11点 */
case '1':case '2':case '3':case '4':case '5':
case '6':case '7':case '8':case '9': sum += poker[i] - '0'; break;/*牌为1~9加对应点数 */
case '0':case 'J':case 'Q':case 'K': sum += 10; break;/*牌为10、J、Q、K加10点 */
}
}
if (numA == 0) return sum;
else {
while (numA && sum > 21) {/*若总点数大于21且有A,则依次减去10直至点数小于21 */
sum -= 10;
numA--;
}
return sum;
}
}
void situation(char player1_poker[21], char player2_poker[21], char* now1, char* now2,int round) /*分析并显示当前局势 */
{
int value1 = getsum(player1_poker), value2 = getsum(player2_poker);/*得到总点数 */
printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");/*清屏 */
if (yes1 && round != 1) {/*显示此轮摸到的牌,第一轮固定摸的两张牌不显示 */
if (*(now1 - 1) == '0') printf("\n你摸到了10!");
else printf("\n你摸到了%c!", *(now1 - 1));
}
printf("\n你的牌的点数为%d", value1);/*显示总点数 */
if (value1 == 21) {/*若总点数为21点,则不再摸牌 */
printf(",21点!\n");
yes1 = 0;
}
else if (value1 > 21) {/*若总点数大于21点,则爆牌,不再摸牌 */
printf(",爆牌!\n");
yes1 = 0;
}
printf ("\n你的当前牌组为:");
for (int i = 0; player1_poker[i]; i++){/*显示当前牌组 */
if (player1_poker[i] == '0') printf("10 ");
else printf("%c ",player1_poker[i]);
}
putchar('\n');
if (yes2 && round != 1) {/*同上*/
if (*(now2 - 1) == '0') printf("\n对手摸到了10!");
else printf("\n对手摸到了%c!", *(now2 - 1));
}
printf("\n对手的牌的点数为%d", value2);
if (value2 == 21) {
printf(",21点!\n");
yes2 = 0;
}
else if (value2 > 21) {
printf(",爆牌!\n");
yes2 = 0;
}
printf ("\n对手当前牌组为:");
for (int i = 0; player2_poker[i]; i++){
if (player2_poker[i] == '0') printf("10 ");
else printf("%c ",player2_poker[i]);
}
printf("\n--------------------");/*分割线 */
if (yes2 && !yes1) {/*若对手可连续摸牌而自己已停牌,输入数字继续以便于观察局势 */
printf("\n输入任意数字继续\n");
int c;
scanf("%d",&c);
}
}
char getpoker() /*随机得到一张A~K的牌 */
{
int n = (rand()) % 13;
if (n == 0) return '0';/*0表示10 */
else if (n == 1) return 'A';
else if (n > 1 && n <= 9) return n + '0';
else if (n == 10) return 'J';
else if (n == 11) return 'Q';
else if (n == 12) return 'K';
}
int main() {
introduction();
int play = 0;
printf("输入1开始\n");
scanf("%d",&play);
while (play == 1)/*当play变量为1时开始/重开一局 */
{
printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");/*清屏 */
yes1 = 1; yes2 = 1;
srand(time(0));
char player1_poker[21] = { 0 }, player2_poker[21] = { 0 };
player1_poker[0] = getpoker();
player1_poker[1] = getpoker();
player2_poker[0] = getpoker();
player2_poker[1] = getpoker();
char* now1 = &player1_poker[2], * now2 = &player2_poker[2];
int round;
for (round = 1; yes1 || yes2; round++)/*当玩家或对手有至少一人可摸牌时继续 */
{
situation(player1_poker, player2_poker, now1, now2,round);/*显示当前局势 */
int value1 = getsum(player1_poker), value2 = getsum(player2_poker);
if (yes1) {/*若玩家能摸牌,则玩家决定是否继续摸牌 */
printf("\n输入0停牌,输入其它数字摸牌\n");
scanf("%d", &yes1);
}
if (yes1) *(now1++) = getpoker();
value1 = getsum(player1_poker), value2 = getsum(player2_poker);
if (value1 > 21) value1 = 0;
if (value2 > 21) value2 = 0;/*将大于21点的点数设置为0,供机器判断 */
if (yes2) {/*若机器能摸牌,则机器决定是否继续摸牌 */
if (value2 < value1) yes2 = 1;/*若玩家总点数大于机器,则摸牌 */
else if (yes1 == 0) yes2 = 0;/*若玩家总点数小于机器,且玩家已停牌,则停牌 */
else if (value2 <= 14) yes2 = 1;/*若玩家总点数小于机器,且玩家未停牌,且机器总点数过小,摸牌 */
else yes2 = 0; /*否则停牌 */
}
if (yes2) *(now2++) = getpoker();
}
situation(player1_poker, player2_poker, now1, now2,round);/*结束,显示最终局势 */
int value1 = getsum(player1_poker), value2 = getsum(player2_poker);
if (value1 > 21) value1 = 0;
if (value2 > 21) value2 = 0;
if (value1 == value2) printf("\n平局!\n");/*判断输赢 */
else if (value1 > value2) printf("\n你赢了!\n");
else if (value1 < value2) printf("\n你输了!\n");
printf("输入1重新开始\n");
scanf("%d", &play);/*玩家决定是否重新开始 */
}
return 0;
}
试玩一局:
标签:case,poker,21,C语言,简易版,value2,printf,点数 From: https://blog.csdn.net/2404_88304514/article/details/143438654