扑克牌JQKA,俩个人各四张。四张牌顺序不一样,对应的比较大小的结果也不一样,俩个人,打乱顺序输入四张牌,最后结果大的人加一分 十局之后,积分多的人获胜。
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
// 输出数组中的牌
void print_cards(int *cards) {
for (int i = 0; i < 4; i++) {
switch(cards[i]) {
case 10: printf("10"); break;
case 11: printf("J"); break;
case 12: printf("Q"); break;
case 13: printf("K"); break;
case 14: printf("A"); break;
default: printf("%d", cards[i]); break;
}
if (i < 3) printf(" ");
}
printf("\n");
}
// 比较两张牌的大小,返回1代表第一张牌大,返回0代表一样大,返回-1代表第二张牌大
int compare_card(int a, int b) {
if (a == b) return 0;
if (a == 14) return 1;
if (b == 14) return -1;
return a > b ? 1 : -1;
}
int main() {
srand(time(NULL)); // 初始化随机种子
// 两位选手的姓名和得分
char player1_name[] = "Player 1";
char player2_name[] = "Player 2";
int player1_score = 0;
int player2_score = 0;
// 十局比赛
for (int i = 1; i <= 10; i++) {
printf("Round %d\n", i);
// 随机生成两个人的牌
int player1_cards[4], player2_cards[4];
for (int j = 0; j < 4; j++) {
player1_cards[j] = rand() % 13 + 2;
player2_cards[j] = rand() % 13 + 2;
}
// 输出两个人的牌
printf("%s: ", player1_name);
print_cards(player1_cards);
printf("%s: ", player2_name);
print_cards(player2_cards);
// 对比每一张牌的大小,积累胜利积分
int player1_win = 0, player2_win = 0;
for (int j = 0; j < 4; j++) {
int compare_res = compare_card(player1_cards[j], player2_cards[j]);
if (compare_res > 0) {
player1_win++;
} else if (compare_res < 0) {
player2_win++;
}
}
// 输出比较结果,加分
if (player1_win > player2_win) {
printf("%s wins\n", player1_name);
player1_score++;
} else if (player1_win < player2_win) {
printf("%s wins\n", player2_name);
player2_score++;
} else {
printf("Draw\n");
}
printf("\n");
}
// 比赛结束,输出获胜者和得分
if (player1_score > player2_score) {
printf("%s wins with a score of %d:%d\n", player1_name, player1_score, player2_score);
} else if (player1_score < player2_score) {
printf("%s wins with a score of %d:%d\n", player2_name, player2_score, player1_score);
} else {
printf("Draw with a score of %d:%d\n", player1_score, player2_score);
}
return 0;
}
该回答通过自己思路及引用到GPTᴼᴾᴱᴺᴬᴵ搜索,得到内容具体如下:
以下是使用 C 语言数组实现比大小小游戏的完整代码和运行结果。
步骤如下:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main() {
int cards[4] = {11, 12, 13, 14}; // 定义扑克牌数组
int player1_score = 0, player2_score = 0; // 初始化玩家分数为 0
srand(time(NULL)); // 设置随机数种子,以当前时间为准
for (int i = 1; i <= 10; i++) { // 执行 10 局游戏
printf("第 %d 局游戏开始!\n", i);
// 随机生成两个玩家的四张牌
int player1[4], player2[4];
for (int j = 0; j < 4; j++) {
player1[j] = cards[rand() % 4];
player2[j] = cards[rand() % 4];
}
// 计算两个玩家的牌面点数,判断大小,更新玩家得分
int player1_sum = 0, player2_sum = 0;
for (int j = 0; j < 4; j++) {
player1_sum += player1[j];
player2_sum += player2[j];
}
if (player1_sum > player2_sum) {
printf("玩家 1 获胜!\n");
player1_score++;
} else if (player1_sum < player2_sum) {
printf("玩家 2 获胜!\n");
player2_score++;
} else {
printf("本局游戏平局!\n");
}
printf("当前得分:玩家 1:%d,玩家 2:%d\n", player1_score, player2_score);
}
if (player1_score > player2_score) {
printf("恭喜玩家 1 获胜!\n");
} else if (player1_score < player2_score) {
printf("恭喜玩家 2 获胜!\n");
} else {
printf("比赛结束,两位玩家打成平局!\n");
}
return 0;
}
运行结果:
第 1 局游戏开始!
玩家 2 获胜!
当前得分:玩家 1:0,玩家 2:1
第 2 局游戏开始!
玩家 2 获胜!
当前得分:玩家 1:0,玩家 2:2
第 3 局游戏开始!
玩家 1 获胜!
当前得分:玩家 1:1,玩家 2:2
第 4 局游戏开始!
玩家 2 获胜!
当前得分:玩家 1:1,玩家 2:3
第 5 局游戏开始!
本局游戏平局!
当前得分:玩家 1:1,玩家 2:3
第 6 局游戏开始!
玩家 2 获胜!
当前得分:玩家 1:1,玩家 2:4
第 7 局游戏开始!
玩家 2 获胜!
当前得分:玩家 1:1,玩家 2:5
第 8 局游戏开始!
玩家 1 获胜!
当前得分:玩家 1:2,玩家 2:5
第 9 局游戏开始!
本局游戏平局!
当前得分:玩家 1:2,玩家 2:5
第 10 局游戏开始!
玩家 1 获胜!
当前得分:玩家 1:3,玩家 2:5
恭喜玩家 2 获胜!
如果以上回答对您有所帮助,点击一下采纳该答案~谢谢