栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

C语言实现简易扑克牌游戏

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C语言实现简易扑克牌游戏

将一副扑克牌平均分成两份,每人拿一份。a先拿出手中的第一张扑克牌放在桌上,然后b也拿出手中的第一张扑克牌,并放在a刚打出的扑克牌的上面,就像这样两人交替出牌。出牌时,如果某人打出的牌与桌上某张牌的牌面相同,即可将两张相同的牌及其中间所夹的牌全部取走,并依次放到自己手中牌的末尾。当任意一人手中的牌全部出完时,游戏结束,对手获胜。

以下是代码的实现:

#define _crt_secure_no_deprecate
#include
#include
 
struct queue//定义队列的结构体
{
 int data[1000];
 int head;
 int tail;
};
struct stack//定义栈的结构体
{
 int data[10];
 int top;
};
 
void poker()
{
 struct queue q1;
 struct queue q2;
 struct stack s;
 int arr[10];
 int i, t;
 q1.head = 1; q1.tail = 1;
 q2.head = 1; q2.tail = 1;
 s.top = 0;
 for (i = 1; i <= 9; i++)
 {
 arr[i] = 0;//对数组进行初始化,全部为0
 }
 
 for (i = 1; i <= 6; i++)
 {
 scanf("%d", &q1.data[q1.tail]);
 q1.tail++;
 }
 for (i = 1; i <= 6; i++)
 {
 scanf("%d", &q2.data[q2.tail]);
 q2.tail++;
 }
 
 while (q1.head < q1.tail&&q2.head < q2.tail)
 {
 t = q1.data[q1.head];
 if (arr[t] == 0)
 {
 q1.head++;
 s.top++;
 s.data[s.top] = t;
 arr[t] = 1;
 }
 else
 {
 q1.head++;
 q1.data[q1.tail] = t;
 q1.tail++;
 while (s.data[s.top] != t)
 {
 arr[s.data[s.top]] = 0;
 q1.data[q1.tail] = s.data[s.top];
 q1.tail++;
 s.top--;
 }
 }
 t = q2.data[q2.head];
 
 if (arr[t] == 0)
 {
 q2.head++;
 s.top++;
 s.data[s.top] = t;
 arr[t] = 1;
 }
 else
 {
 q2.head++;
 q2.data[q2.tail] = t;
 q2.tail++;
 while (s.data[s.top] != t)
 {
 arr[s.data[s.top]] = 0;
 q2.data[q2.tail] = s.data[s.top];
 q2.tail++;
 s.top--;
 }
 }
 }
 if (q2.head == q2.tail)
 {
 printf("a赢n");
 printf("a当前手中的牌是:");
 for (i = q1.head; i <= q1.tail - 1; i++)
 {
 printf(" %d", q1.data[i]);
 }
 if (s.top > 0)
 {
 printf("n桌上的牌是:");
 for (i = 1; i <= s.top; i++)
 {
 printf(" %d", s.data[i]);
 }
 printf("n");
 }
 else
 {
 printf("n桌上已经没有牌了");
 }
 }
 else
 {
 printf("b赢n");
 printf("b当前手中的牌是:");
 for (i = q2.head; i <= q2.tail - 1; i++)
 {
 printf(" %d", q2.data[i]);
 }
 if (s.top > 0)
 {
 printf("n桌上的牌是:");
 for (i = 1; i <= s.top; i++)
 {
 printf(" %d", s.data[i]);
 }
 printf("n");
 }
 else
 {
 printf("n桌上已经没有牌了");
 }
 }
}
int main()
{
 poker();
 system("pause");
 return 0;
}

运行的结果:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/62292.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号