Files
CardGame/CardGame.Test.GamePlay1/CardGame.Test.GamePlay1.cpp

156 lines
3.8 KiB
C++
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#include <iostream>
using namespace std;
#include "SqList.h"
#include "SqQueue.h"
#include "SqStack.h"
#include "Functions.h"
// 发牌
void DealCards(int N, SqList<int>*& PA, SqList<int>*& PB, int times)
{
int* CardPile = new int[N]; // 发牌堆
srand((int)time(nullptr) + times); // 基于当前时间和游玩次数(对于测试代码,多次测试可能在同一秒完成)设置随机数种子
for (int i = 0; i < N; i++) CardPile[i] = 2; // 默认情况下每个面值有两张牌
for (int i = 0; i < N * 2; i++)
{
int j = 1, k = 1;
int num = rand() % N; // 获取一个 0~N-1 的随机数
while (true)
{
if (CardPile[num] != 0) // 如果该数对应面值的牌未发完
{
if (i % 2 == 0) // 发给PA
{
InsertElem_i(*PA, j++, num + 1);
CardPile[num]--;
}
else // 发给PB
{
InsertElem_i(*PB, k++, num + 1);
CardPile[num]--;
}
break;
}
else num = (num + 1) % N; // 若发完则检查下一位数字对应面值的牌
}
}
delete[] CardPile; // 释放发牌堆占用的内存
}
int GamePlay1(SqList<int>& PA, SqList<int>& PB, int N)
{
int winner = 0;
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) {
DeQueue(*QA, a);
x = a;
}
else {
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);
}
}
}
// 判断是否有一方获胜
if (QueueEmpty(*QA))
{
winner = 2;
break;
}
else if (QueueEmpty(*QB))
{
winner = 1;
break;
}
}
DestroyStack(*Desk);
DestroyQueue(*QA);
DestroyQueue(*QB);
delete Desk;
delete QA;
delete QB;
return winner;
}
int main()
{
int counts = 0;
cout << "Enter test counts: ";
cin >> counts;
int N = 9;
int PlayerAWinCount = 0, PlayerBWinCount = 0;
int RealCounts = counts;
for (int i = 0; i < counts; i++)
{
SqList<int>* PA = new SqList<int>; // PA的手牌
InitList(*PA, N);
SqList<int>* PB = new SqList<int>; // PB的手牌
InitList(*PB, N);
DealCards(N, PA, PB, i);
int result = GamePlay1(*PA, *PB, N);
DestroyList(*PA);
DestroyList(*PB);
delete PA;
delete PB;
if (result == 1) PlayerAWinCount++;
else if (result == 2) PlayerBWinCount++;
else RealCounts--;
}
double PlayerAWinRate = (double)PlayerAWinCount / RealCounts;
double PlayerBWinRate = (double)PlayerBWinCount / RealCounts;
cout << "Test Result:\n";
cout << "Total test counts: " << RealCounts << endl;
cout << "Player A wins: " << PlayerAWinCount << " times, rate: " << PlayerAWinRate * 100 << "%" << endl;
cout << "Player B wins: " << PlayerBWinCount << " times, rate: " << PlayerBWinRate * 100 << "%" << endl;
}