将一副撲克牌平均分成兩份,每人拿一份。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;
}
運作的結果:
