149 lines
3.2 KiB
C++
149 lines
3.2 KiB
C++
#include <iostream>
|
||
using namespace std;
|
||
#include "SqList.h"
|
||
#include "SqQueue.h"
|
||
#include "SqStack.h"
|
||
#include "FuncRef.h"
|
||
#include "Functions.h"
|
||
|
||
// 输出B的手牌
|
||
void DisplayPlayerBCards(int*& QB, int N)
|
||
{
|
||
for (int i = 0; i < N; i++)
|
||
for (int j = 0; j < QB[i]; j++)
|
||
cout << i + 1 << " ";
|
||
cout << endl;
|
||
}
|
||
|
||
void DisplayStatus(SqQueue<int>& QA, int*& QB, SqStack<int>& Desk, int i, int N)
|
||
{
|
||
cout << "------------------------------" << endl;
|
||
cout << "Round " << i << endl;
|
||
cout << "Player A: ";
|
||
DispQueue(QA);
|
||
cout << "Player B: ";
|
||
DisplayPlayerBCards(QB, N);
|
||
cout << "Desk: ";
|
||
DispStack(Desk);
|
||
}
|
||
|
||
bool IsPlayerAWin(int*& QB, int N)
|
||
{
|
||
for (int i = 0; i < N; i++)
|
||
if (QB[i] > 0) return false;
|
||
return true;
|
||
}
|
||
|
||
// 玩家B出牌
|
||
int PlayerBPlay(int*& QB, SqStack<int>& Desk, int N)
|
||
{
|
||
// 如果栈为空,且B有两张相同面值的牌,则出该面值的牌
|
||
if (StackEmpty(Desk))
|
||
for (int i = 1; i <= N; i++)
|
||
if (QB[i - 1] == 2)
|
||
return i;
|
||
// 如果栈非空,则从栈底遍历,找到第一张手牌中拥有的牌出之
|
||
for (int i = 0; i <= Desk.top; i++)
|
||
{
|
||
int card = Desk.base[i];
|
||
if (QB[card - 1] > 0)
|
||
return card;
|
||
}
|
||
// 如果以上情况都不满足,则随机出一张牌
|
||
int card;
|
||
do
|
||
card = rand() % N + 1;
|
||
while (QB[card - 1] == 0);
|
||
return card;
|
||
}
|
||
|
||
void GamePlay2(SqList<int>& PA, SqList<int>& PB, int N)
|
||
{
|
||
SqStack<int>* Desk = new SqStack<int>; // 牌桌上的牌
|
||
InitStack(*Desk, N * 2 + 1);
|
||
// 玩法2初始化
|
||
// 对于玩法2,PA为先抓到的牌先出,赢的牌加在牌尾;PB为任意出牌
|
||
SqQueue<int>* QA = new SqQueue<int>;
|
||
InitQueue(*QA, N * 2 + 1);
|
||
// PB手牌存储方式:下标为面值-1,值为张数
|
||
int* QB = new int[N];
|
||
for (int i = 0; i < N; i++) QB[i] = 0;
|
||
// 将PA的牌放入QA
|
||
for (int i = 1; i <= N; i++)
|
||
{
|
||
int e = 0;
|
||
GetElem_i(PA, i, e);
|
||
EnQueue(*QA, e);
|
||
}
|
||
// 将PB的牌放入QB
|
||
for (int i = 1; i <= N; i++)
|
||
{
|
||
int e = 0;
|
||
GetElem_i(PB, i, e);
|
||
QB[e - 1]++;
|
||
}
|
||
// Step2 玩牌
|
||
int a = 0, b = 0, i = 0; // i的奇偶性决定出牌方
|
||
while (true)
|
||
{
|
||
int x = 0;
|
||
// 双方交替出牌
|
||
if (i++ % 2 == 0) {
|
||
DeQueue(*QA, a);
|
||
x = a;
|
||
}
|
||
else {
|
||
b = PlayerBPlay(QB, *Desk, N);
|
||
QB[b - 1]--;
|
||
x = b;
|
||
}
|
||
// 如果桌面上没有面值为X的牌,则将牌放到牌桌上
|
||
if (FindElem(*Desk, x) == -1) Push(*Desk, x);
|
||
// 如果桌面上有面值为X的牌
|
||
else
|
||
{
|
||
int times = FindElem(*Desk, x);
|
||
if (IsPlayerA(i)) // 如果是A出的牌
|
||
{
|
||
// 将X放入牌尾
|
||
EnQueue(*QA, x);
|
||
// 将桌面上直到X的牌依次放入牌尾
|
||
for (int j = 0; j <= times; j++)
|
||
{
|
||
int e = 0;
|
||
Pop(*Desk, e);
|
||
EnQueue(*QA, e);
|
||
}
|
||
}
|
||
else // 如果是B出的牌
|
||
{
|
||
// 将X放入手牌
|
||
QB[x - 1]++;
|
||
// 将桌面上直到X的牌依次放入手牌
|
||
for (int j = 0; j <= times; j++)
|
||
{
|
||
int e = 0;
|
||
Pop(*Desk, e);
|
||
QB[e - 1]++;
|
||
}
|
||
}
|
||
}
|
||
DisplayStatus(*QA, QB, *Desk, i, N);
|
||
// 判断是否有一方获胜
|
||
if (QueueEmpty(*QA))
|
||
{
|
||
cout << "Player B wins!" << endl;
|
||
break;
|
||
}
|
||
else if (IsPlayerAWin(QB, N))
|
||
{
|
||
cout << "Player A wins!" << endl;
|
||
break;
|
||
}
|
||
}
|
||
DestroyStack(*Desk);
|
||
DestroyQueue(*QA);
|
||
delete Desk;
|
||
delete QA;
|
||
delete[] QB;
|
||
} |