#include <iostream>
using namespace std;
#include "SqList.h"
#include "SqQueue.h"
#include "SqStack.h"
#include "FuncRef.h"
#include "Functions.h"

void DisplayStatus(SqQueue<int>& QA, SqQueue<int>& QB, SqStack<int>& Desk, int i)
{
	cout << "------------------------------" << endl;
	cout << "Round " << i << endl;
	cout << "Player A: ";
	DispQueue(QA);
	cout << "Player B: ";
	DispQueue(QB);
	cout << "Desk: ";
	DispStack(Desk);
}

void GamePlay1(SqList<int>& PA, SqList<int>& PB, int N)
{
	SqStack<int>* Desk = new SqStack<int>; // 牌桌上的牌
	InitStack(*Desk, N * 2 + 1);
	// 玩法1初始化
	// 对于玩法1，双方均为先抓到的牌先出，赢的牌加在牌尾
	SqQueue<int>* QA = new SqQueue<int>;
	InitQueue(*QA, N * 2 + 1);
	SqQueue<int>* QB = new SqQueue<int>;
	InitQueue(*QB, N * 2 + 1);
	// 将PA和PB的牌分别放入QA和QB
	for (int i = 1; i <= N; i++)
	{
		int e = 0;
		GetElem_i(PA, i, e);
		EnQueue(*QA, e);
		GetElem_i(PB, i, e);
		EnQueue(*QB, e);
	}
	// Step2 玩牌
	int a = 0, b = 0, i = 0; // i的奇偶性决定出牌方
	while (true)
	{
		int x = 0;
		// 双方交替出牌
		if (i++ % 2 == 0) // 奇数回合A出牌
		{
			DeQueue(*QA, a);
			x = a;
		}
		else // 偶数回合B出牌
		{
			DeQueue(*QB, b);
			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放入牌尾
				EnQueue(*QB, x);
				// 将桌面上直到X的牌依次放入牌尾
				for (int j = 0; j <= times; j++)
				{
					int e = 0;
					Pop(*Desk, e);
					EnQueue(*QB, e);
				}
			}
		}
		DisplayStatus(*QA, *QB, *Desk, i);
		// 判断是否有一方获胜
		if (QueueEmpty(*QA))
		{
			cout << "Player B wins!" << endl;
			break;
		}
		else if (QueueEmpty(*QB))
		{
			cout << "Player A wins!" << endl;
			break;
		}
	}
	DestroyStack(*Desk);
	DestroyQueue(*QA);
	DestroyQueue(*QB);
	delete Desk;
	delete QA;
	delete QB;
}