用c语言写一个斗地主小游戏
时间: 2024-06-09 16:05:24 浏览: 135
java编写的斗地主小游戏.zip
以下是一个简单的斗地主小游戏的 C 代码示例。这个游戏只有命令行界面。在游戏开始前,玩家需要输入自己的名字,并且可以选择是否要玩游戏。在游戏中,程序会随机分配牌,并将三张牌作为底牌,然后轮流出牌,直到有玩家出完牌或游戏结束。
```c
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define CARD_NUM 54
char *card_name[CARD_NUM] = {"大王", "小王",
"A", "2", "3", "4", "5", "6", "7", "8", "9", "10",
"J", "Q", "K",
"A", "2", "3", "4", "5", "6", "7", "8", "9", "10",
"J", "Q", "K",
"A", "2", "3", "4", "5", "6", "7", "8", "9", "10",
"J", "Q", "K",
"A", "2", "3", "4", "5", "6", "7", "8", "9", "10",
"J", "Q", "K"};
int card[CARD_NUM];
int player1[CARD_NUM / 3], player2[CARD_NUM / 3], player3[CARD_NUM / 3];
int bottom_card[3];
int player1_num, player2_num, player3_num;
void shuffle_cards();
void print_cards(int *cards, int num);
void deal_cards();
void play_game();
int main()
{
char player_name[20];
char play_game_choice;
printf("请输入您的名字: ");
scanf("%s", player_name);
printf("%s, 是否要玩斗地主? (Y/N): ", player_name);
scanf(" %c", &play_game_choice);
if (play_game_choice == 'Y' || play_game_choice == 'y')
{
printf("%s, 开始玩斗地主!\n", player_name);
shuffle_cards();
deal_cards();
play_game();
}
else
{
printf("欢迎再来玩斗地主!\n");
}
return 0;
}
void shuffle_cards()
{
int i, j, temp;
srand(time(NULL));
for (i = 0; i < CARD_NUM; i++)
{
card[i] = i;
}
for (i = CARD_NUM - 1; i >= 0; i--)
{
j = rand() % (i + 1);
temp = card[i];
card[i] = card[j];
card[j] = temp;
}
}
void print_cards(int *cards, int num)
{
int i;
for (i = 0; i < num; i++)
{
printf("%s ", card_name[cards[i]]);
}
}
void deal_cards()
{
int i;
for (i = 0; i < CARD_NUM / 3; i++)
{
player1[i] = card[i * 3];
player2[i] = card[i * 3 + 1];
player3[i] = card[i * 3 + 2];
}
for (i = CARD_NUM / 3 * 3; i < CARD_NUM; i++)
{
bottom_card[i - CARD_NUM / 3 * 3] = card[i];
}
}
void play_game()
{
int current_player = 1;
int last_player = 0;
int current_card[CARD_NUM], current_num;
int i, j, k;
char play_game_choice;
player1_num = player2_num = player3_num = CARD_NUM / 3;
while (1)
{
printf("底牌: ");
print_cards(bottom_card, 3);
printf("\n");
switch (current_player)
{
case 1:
printf("玩家1出牌: ");
print_cards(player1, player1_num);
printf("\n");
break;
case 2:
printf("玩家2出牌: ");
print_cards(player2, player2_num);
printf("\n");
break;
case 3:
printf("玩家3出牌: ");
print_cards(player3, player3_num);
printf("\n");
break;
}
printf("请出牌 (输入-1结束游戏): ");
scanf("%d", ¤t_card[0]);
if (current_card[0] == -1)
{
printf("是否要结束游戏? (Y/N): ");
scanf(" %c", &play_game_choice);
if (play_game_choice == 'Y' || play_game_choice == 'y')
{
printf("游戏结束!\n");
return;
}
}
else
{
current_num = 1;
while (1)
{
printf("请继续出牌 (输入-1结束出牌): ");
scanf("%d", ¤t_card[current_num]);
if (current_card[current_num] == -1)
{
break;
}
current_num++;
}
int valid = 0;
switch (current_player)
{
case 1:
for (i = 0; i < current_num; i++)
{
for (j = 0; j < player1_num; j++)
{
if (current_card[i] == player1[j])
{
valid = 1;
for (k = j; k < player1_num - 1; k++)
{
player1[k] = player1[k + 1];
}
player1_num--;
break;
}
}
if (!valid)
{
printf("出牌不合法!\n");
break;
}
valid = 0;
}
break;
case 2:
for (i = 0; i < current_num; i++)
{
for (j = 0; j < player2_num; j++)
{
if (current_card[i] == player2[j])
{
valid = 1;
for (k = j; k < player2_num - 1; k++)
{
player2[k] = player2[k + 1];
}
player2_num--;
break;
}
}
if (!valid)
{
printf("出牌不合法!\n");
break;
}
valid = 0;
}
break;
case 3:
for (i = 0; i < current_num; i++)
{
for (j = 0; j < player3_num; j++)
{
if (current_card[i] == player3[j])
{
valid = 1;
for (k = j; k < player3_num - 1; k++)
{
player3[k] = player3[k + 1];
}
player3_num--;
break;
}
}
if (!valid)
{
printf("出牌不合法!\n");
break;
}
valid = 0;
}
break;
}
if (valid)
{
printf("出牌合法!\n");
}
else
{
printf("出牌不合法!\n");
current_player = last_player;
}
if (player1_num == 0)
{
printf("玩家1赢了!\n");
return;
}
else if (player2_num == 0)
{
printf("玩家2赢了!\n");
return;
}
else if (player3_num == 0)
{
printf("玩家3赢了!\n");
return;
}
last_player = current_player;
current_player = (current_player % 3) + 1;
}
}
}
```
阅读全文