扑克牌游戏c语言课程设计
时间: 2023-12-17 11:28:36 浏览: 86
以下是一个简单的扑克牌游戏的C语言课程设计,实现了21点的基本规则:
```c
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define MAX_CARD_NUM 52
#define MAX_PLAYER_CARD_NUM 5
// 扑克牌结构体
typedef struct {
char suit; // 花色
char rank; // 点数
} Card;
// 玩家结构体
typedef struct {
Card cards[MAX_PLAYER_CARD_NUM]; // 手牌
int card_num; // 手牌数量
int point; // 点数
} Player;
// 初始化扑克牌
void init_cards(Card *cards) {
char suits[4] = {'S', 'H', 'D', 'C'}; // 花色
char ranks[13] = {'A', '2', '3', '4', '5', '6', '7', '8', '9', 'X', 'J', 'Q', 'K'}; // 点数
int i, j, k = 0;
for (i = 0; i < 4; i++) {
for (j = 0; j < 13; j++) {
cards[k].suit = suits[i];
cards[k].rank = ranks[j];
k++;
}
}
}
// 洗牌
void shuffle_cards(Card *cards) {
srand(time(NULL));
int i, j;
Card temp;
for (i = 0; i < MAX_CARD_NUM; i++) {
j = rand() % MAX_CARD_NUM;
temp = cards[i];
cards[i] = cards[j];
cards[j] = temp;
}
}
// 计算点数
int calc_point(Card *cards, int card_num) {
int i, point = 0, ace_num = 0;
for (i = 0; i < card_num; i++) {
if (cards[i].rank == 'A') {
ace_num++;
point += 11;
} else if (cards[i].rank == 'X' || cards[i].rank == 'J' || cards[i].rank == 'Q' || cards[i].rank == 'K') {
point += 10;
} else {
point += cards[i].rank - '0';
}
}
while (point > 21 && ace_num > 0) {
point -= 10;
ace_num--;
}
return point;
}
// 发牌
void deal_cards(Card *cards, Player *player, int card_num) {
int i;
for (i = 0; i < card_num; i++) {
player->cards[player->card_num] = cards[i];
player->card_num++;
player->point = calc_point(player->cards, player->card_num);
}
}
// 显示牌
void show_cards(Player *player) {
int i;
printf("Cards: ");
for (i = 0; i < player->card_num; i++) {
printf("%c%c ", player->cards[i].suit, player->cards[i].rank);
}
printf("\nPoint: %d\n", player->point);
}
// 判断是否爆了
int is_busted(Player *player) {
return player->point > 21;}
// 判断是否21点
int is_21(Player *player) {
return player->point == 21;
}
// 判断是否可以要牌
int can_hit(Player *player) {
return player->card_num < MAX_PLAYER_CARD_NUM && player->point < 21;
}
// 比较点数大小
int compare_point(Player *player1, Player *player2) {
if (player1->point > player2->point) {
return 1;
} else if (player1->point < player2->point) {
return -1;
} else {
return 0;
}
}
int main() {
Card cards[MAX_CARD_NUM];
Player player, dealer;
int i, choice;
init_cards(cards);
shuffle_cards(cards);
player.card_num = 0;
dealer.card_num = 0;
deal_cards(cards, &player, 2);
deal_cards(cards + 2, &dealer, 2);
printf("Player:\n");
show_cards(&player);
printf("Dealer:\n");
printf("Cards: %c%c **\n", dealer.cards[0].suit, dealer.cards[0].rank);
while (can_hit(&player)) {
printf("Do you want to hit? (1 for yes, 0 for no): ");
scanf("%d", &choice);
if (choice == 1) {
deal_cards(cards + 4 + player.card_num, &player, 1);
printf("Player:\n");
show_cards(&player);
} else {
break;
}
}
if (is_busted(&player)) {
printf("Player busted! Dealer wins!\n");
} else if (is_21(&player)) {
printf("Player got 21 points!\n");
} else {
while (dealer.point < 17) {
deal_cards(cards + 4 + player.card_num + dealer.card_num, &dealer, 1);
}
printf("Dealer:\n");
show_cards(&dealer);
if (is_busted(&dealer)) {
printf("Dealer busted! Player wins!\n");
} else {
int result = compare_point(&player, &dealer);
if (result == 1) {
printf("Player wins!\n");
} else if (result == -1) {
printf("Dealer wins!\n");
} else {
printf("Tie!\n");
}
}
}
return 0;
}
```
阅读全文