纸牌游戏十点半10点半代码c语言
时间: 2023-11-05 09:13:32 浏览: 103
十点半纸牌游戏
3星 · 编辑精心推荐
以下是一个简单的10点半纸牌游戏的代码,使用C语言编写:
```c
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main() {
int cards[52] = {1,2,3,4,5,6,7,8,9,10,10,10,10,
1,2,3,4,5,6,7,8,9,10,10,10,10,
1,2,3,4,5,6,7,8,9,10,10,10,10,
1,2,3,4,5,6,7,8,9,10,10,10,10}; //初始化一副扑克牌
int player_cards[10] = {0}; //玩家手牌
int dealer_cards[10] = {0}; //庄家手牌
int player_sum = 0; //玩家手牌点数
int dealer_sum = 0; //庄家手牌点数
int player_count = 0; //玩家手牌数量
int dealer_count = 0; //庄家手牌数量
int i, j; //循环计数器
char hit; //是否继续要牌
srand(time(NULL)); //设置随机数种子
//洗牌
for (i = 0; i < 52; i++) {
int r = rand() % 52;
int temp = cards[i];
cards[i] = cards[r];
cards[r] = temp;
}
//发牌
player_cards[0] = cards[0];
dealer_cards[0] = cards[1];
player_cards[1] = cards[2];
dealer_cards[1] = cards[3];
player_count = 2;
dealer_count = 2;
//计算玩家手牌点数
for (i = 0; i < player_count; i++) {
if (player_cards[i] == 1 && player_sum + 11 <= 10) {
player_sum += 11;
} else {
player_sum += player_cards[i];
}
}
//计算庄家手牌点数
for (i = 0; i < dealer_count; i++) {
if (dealer_cards[i] == 1 && dealer_sum + 11 <= 10) {
dealer_sum += 11;
} else {
dealer_sum += dealer_cards[i];
}
}
//玩家要牌
while (player_sum < 10.5) {
printf("Your cards: ");
for (i = 0; i < player_count; i++) {
printf("%d ", player_cards[i]);
}
printf("\n");
printf("Do you want to hit? (y/n)\n");
scanf(" %c", &hit);
if (hit == 'y') {
player_cards[player_count] = cards[4 + player_count];
player_count++;
if (player_cards[player_count - 1] == 1 && player_sum + 11 <= 10) {
player_sum += 11;
} else {
player_sum += player_cards[player_count - 1];
}
} else {
break;
}
}
//庄家要牌
while (dealer_sum < 10.5) {
dealer_cards[dealer_count] = cards[4 + dealer_count + player_count];
dealer_count++;
if (dealer_cards[dealer_count - 1] == 1 && dealer_sum + 11 <= 10) {
dealer_sum += 11;
} else {
dealer_sum += dealer_cards[dealer_count - 1];
}
}
//输出玩家和庄家的手牌和点数
printf("Your cards: ");
for (i = 0; i < player_count; i++) {
printf("%d ", player_cards[i]);
}
printf("\n");
printf("Your sum: %.1f\n", player_sum);
printf("Dealer's cards: ");
for (i = 0; i < dealer_count; i++) {
printf("%d ", dealer_cards[i]);
}
printf("\n");
printf("Dealer's sum: %.1f\n", dealer_sum);
//判断胜负
if (player_sum > 10.5) {
printf("You bust!\n");
} else if (dealer_sum > 10.5) {
printf("Dealer bust!\n");
} else if (player_sum > dealer_sum) {
printf("You win!\n");
} else if (player_sum < dealer_sum) {
printf("Dealer win!\n");
} else {
printf("Push!\n");
}
return 0;
}
```
注意:这只是一个简单的实现,可能还有很多需要完善的地方。
阅读全文