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

时间:2022-01-24 05:51:49

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

以下是代码的实现:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#define _crt_secure_no_deprecate
#include<stdio.h>
#include<stdlib.h>
 
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;
}

运行的结果:

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

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

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/LX18792732127/article/details/73435594