关注博主不迷路,博主带你码代码!
1. 基本要求
- 一副没有花牌(J、Q、K、A、大小王)的扑克牌,两个人进行纸牌游戏,其中一个人为用户,另一个人为计算机;
- 每轮每人各发5张牌,各自以这5张牌建立二叉排序树;
- 由用户先出,轮流出牌,每次只能出一张并且要比别人出的大,如:用户出3,计算机则要出比3大的牌,没有则选择不出;
- 最先出完的人获胜。
2. 运行界面
1. 首页面
2. 游戏说明
3. 开始游戏
4. 开始出牌
5. 游戏结束
3. 代码解释
#include<stdio.h>
#include<stdlib.h>
#include<windows.h>
#include<time.h>
int card[5][2];
int playercard[5];
int computercard[5];
char bhuase[5];
char chuase[5];
typedef struct node
{
int data;
struct node *LChild;
struct node *RChild;
}node;
typedef struct tree
{
int data;
struct node *LChild;
struct node *RChild;
}tree;
void PlayingCardsyxsm()
{
printf(" 每轮每人各发5张牌,各自以五张牌建立二叉树,由用户先出,轮流出牌,\n");
printf(" 每次只能出一张并且要比别人出的大,\n");
printf(" 如:用户出3,计算机则要算出比3大的牌,没有则选择不出;\n");
printf(" 最先出完的人获胜。\n");
}
void PlayingCardsfp()
{
int player;
int computer;
for(int i = 0; i < 5; i ++ ){
player = rand() % 9 + 2;
printf("玩家发到的牌:%d \n", player);
playercard[i] = player;
computer = rand() % 9 + 2;
printf("电脑发到的牌:%d \n", computer);
computercard[i] = computer;
}
}
void PlayingCardscp()
{
int player = 0;
int computer = 0;
int playercount = 0;
int computercount = 0;
bool flag = false;
for(int m = 0; m < 5; m ++ )
{
for(int k = 0; k < 5; k ++ )
{
for(int j = 0; j < 5; j ++ )
{
if(playercard[j] > computer && card[j][0] == 0)
{
printf("玩家出牌%c%d\n", bhuase[j],playercard[j]);
playercount ++ ;
card[j][0] = 1;
player = playercard[j];
Sleep(800);
break;
}
}
if(playercount == 5)
{
printf("玩家赢了!");
flag = true;
break;
}
for(int j = 0; j < 5; j ++ )
{
if(computercard[j] > player && card[j][1] == 0)
{
printf("电脑出牌%c%d\n", chuase[j],computercard[j]);
computercount ++ ;
card[j][1] = 1;
computer = computercard[j];
Sleep(800);
break;
}
}
if(computercount == 5)
{
printf("电脑赢了!");
flag = true;
break;
}
}
if(player > computer) computer = 0;
if(computer > player) player = 0;
if(flag) break;
}
}
void PlayingCardsxswj()
{
printf("玩家\n");
printf(" %d %d %d %d %d\n", playercard[0], playercard[1], playercard[2], playercard[3], playercard[4], playercard[5]);
}
void PlayingCardsxsdn()
{
printf("电脑\n");
printf(" %d %d %d %d %d\n", computercard[0], computercard[1], computercard[2], computercard[3], computercard[4], computercard[5]);
}
void treechushihua(node *t)
{
t = NULL;
}
node *treecharu(node *t, int key)
{
if(t == NULL)
{
node *p;
p = (node*)malloc(sizeof(node));
p->data = key;
p->LChild = NULL;
p->RChild = NULL;
t = p;
}
else
{
if(key < t->data)
t->LChild = treecharu(t->LChild, key);
else
t->RChild = treecharu(t->RChild, key);
}
return t;
}
node *jianlib(node *t)
{
int i, key;
for(i = 0; i < 5; i ++ )
{
key = playercard[i];
t = treecharu(t, key);
}
return t;
}
node *jianlic(node *t)
{
int i, key;
for(i = 0; i < 5; i ++ )
{
key = computercard[i];
t = treecharu(t, key);
}
return t;
}
void treepaixu1(node *t)
{
if(t != NULL)
{
treepaixu1(t->LChild);
printf("%d ", t->data);
treepaixu1(t->RChild);
}
}
void treepaixu2(node *t, int *p)
{
if(t == NULL) return;
else
{
treepaixu2(t->LChild, p);
playercard[(*p) ++ ] = t->data;
treepaixu2(t->RChild, p);
}
}
void treepaixu3(node *t, int *p)
{
if(t == NULL) return;
else
{
treepaixu3(t->LChild, p);
computercard[(*p) ++ ] = t->data;
treepaixu3(t->RChild, p);
}
}
int main()
{
int k = 0;
srand((unsigned)time(NULL));
int n = 0;
while(k != -1)
{
puts("");
puts("");
puts("");
printf("\t\t\t ****纸牌游戏****\n");
printf("\t\t\t****1.游戏说明****\n");
printf("\t\t\t****2.开始游戏****\n");
printf("\t\t\t****3.开始出牌****\n");
printf("\t\t\t****4.游戏结束****\n");
printf("\t\t\t******************\n");
puts("");
printf("\t\t**********************************\n");
printf("\t\t**********************************\n");
puts("");
printf("\t\t\t请输入(1、2、3、4):\n");
scanf("%d", &k);
switch(k){
case 1: PlayingCardsyxsm(); break;
case 2:{
PlayingCardsfp();
node *t1 = NULL;
t1 = jianlib(t1);
printf("玩家手牌为:");
treepaixu1(t1);
node *t2 = NULL;
t2 = jianlic(t2);
puts("");
printf("电脑手牌为:");
treepaixu1(t2);
n = 0;
treepaixu2(t1, &n);
n = 0;
treepaixu3(t2, &n);
puts("");
PlayingCardsxswj();
PlayingCardsxsdn();
break;
}
case 3:{
PlayingCardscp();
break;
}
case 4:k=-1; break;
}
}
return 0;
}
关注博主不迷路,博主带你码代码!
|