project rebuild, add test code, fix memory leak, etc
This commit is contained in:
200
CardGame.Test.GamePlay2/CardGame.Test.GamePlay2.cpp
Normal file
200
CardGame.Test.GamePlay2/CardGame.Test.GamePlay2.cpp
Normal file
@@ -0,0 +1,200 @@
|
||||
#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; // 释放发牌堆占用的内存
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
bool IsPlayerBWin(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;
|
||||
}
|
||||
|
||||
int GamePlay2(SqList<int>& PA, SqList<int>& PB, int N)
|
||||
{
|
||||
int winner = 0;
|
||||
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]++;
|
||||
}
|
||||
}
|
||||
}
|
||||
// 判断是否有一方获胜
|
||||
if (QueueEmpty(*QA))
|
||||
{
|
||||
winner = 2;
|
||||
break;
|
||||
}
|
||||
else if (IsPlayerBWin(QB, N))
|
||||
{
|
||||
winner = 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
DestroyStack(*Desk);
|
||||
DestroyQueue(*QA);
|
||||
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 = GamePlay2(*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;
|
||||
}
|
||||
144
CardGame.Test.GamePlay2/CardGame.Test.GamePlay2.vcxproj
Normal file
144
CardGame.Test.GamePlay2/CardGame.Test.GamePlay2.vcxproj
Normal file
@@ -0,0 +1,144 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<ItemGroup Label="ProjectConfigurations">
|
||||
<ProjectConfiguration Include="Debug|Win32">
|
||||
<Configuration>Debug</Configuration>
|
||||
<Platform>Win32</Platform>
|
||||
</ProjectConfiguration>
|
||||
<ProjectConfiguration Include="Release|Win32">
|
||||
<Configuration>Release</Configuration>
|
||||
<Platform>Win32</Platform>
|
||||
</ProjectConfiguration>
|
||||
<ProjectConfiguration Include="Debug|x64">
|
||||
<Configuration>Debug</Configuration>
|
||||
<Platform>x64</Platform>
|
||||
</ProjectConfiguration>
|
||||
<ProjectConfiguration Include="Release|x64">
|
||||
<Configuration>Release</Configuration>
|
||||
<Platform>x64</Platform>
|
||||
</ProjectConfiguration>
|
||||
</ItemGroup>
|
||||
<PropertyGroup Label="Globals">
|
||||
<VCProjectVersion>17.0</VCProjectVersion>
|
||||
<Keyword>Win32Proj</Keyword>
|
||||
<ProjectGuid>{6383c4b6-1282-4f20-864a-7208ec5805ca}</ProjectGuid>
|
||||
<RootNamespace>CardGameTestGamePlay2</RootNamespace>
|
||||
<WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
|
||||
</PropertyGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
|
||||
<ConfigurationType>Application</ConfigurationType>
|
||||
<UseDebugLibraries>true</UseDebugLibraries>
|
||||
<PlatformToolset>v143</PlatformToolset>
|
||||
<CharacterSet>Unicode</CharacterSet>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
|
||||
<ConfigurationType>Application</ConfigurationType>
|
||||
<UseDebugLibraries>false</UseDebugLibraries>
|
||||
<PlatformToolset>v143</PlatformToolset>
|
||||
<WholeProgramOptimization>true</WholeProgramOptimization>
|
||||
<CharacterSet>Unicode</CharacterSet>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
|
||||
<ConfigurationType>Application</ConfigurationType>
|
||||
<UseDebugLibraries>true</UseDebugLibraries>
|
||||
<PlatformToolset>v143</PlatformToolset>
|
||||
<CharacterSet>Unicode</CharacterSet>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
|
||||
<ConfigurationType>Application</ConfigurationType>
|
||||
<UseDebugLibraries>false</UseDebugLibraries>
|
||||
<PlatformToolset>v143</PlatformToolset>
|
||||
<WholeProgramOptimization>true</WholeProgramOptimization>
|
||||
<CharacterSet>Unicode</CharacterSet>
|
||||
</PropertyGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
|
||||
<ImportGroup Label="ExtensionSettings">
|
||||
</ImportGroup>
|
||||
<ImportGroup Label="Shared">
|
||||
</ImportGroup>
|
||||
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
|
||||
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
|
||||
</ImportGroup>
|
||||
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
|
||||
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
|
||||
</ImportGroup>
|
||||
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
|
||||
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
|
||||
</ImportGroup>
|
||||
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
|
||||
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
|
||||
</ImportGroup>
|
||||
<PropertyGroup Label="UserMacros" />
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
|
||||
<IncludePath>..\include;$(IncludePath)</IncludePath>
|
||||
</PropertyGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
|
||||
<ClCompile>
|
||||
<WarningLevel>Level3</WarningLevel>
|
||||
<SDLCheck>true</SDLCheck>
|
||||
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<ConformanceMode>true</ConformanceMode>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<SubSystem>Console</SubSystem>
|
||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
|
||||
<ClCompile>
|
||||
<WarningLevel>Level3</WarningLevel>
|
||||
<FunctionLevelLinking>true</FunctionLevelLinking>
|
||||
<IntrinsicFunctions>true</IntrinsicFunctions>
|
||||
<SDLCheck>true</SDLCheck>
|
||||
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<ConformanceMode>true</ConformanceMode>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<SubSystem>Console</SubSystem>
|
||||
<EnableCOMDATFolding>true</EnableCOMDATFolding>
|
||||
<OptimizeReferences>true</OptimizeReferences>
|
||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
|
||||
<ClCompile>
|
||||
<WarningLevel>Level3</WarningLevel>
|
||||
<SDLCheck>true</SDLCheck>
|
||||
<PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<ConformanceMode>true</ConformanceMode>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<SubSystem>Console</SubSystem>
|
||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
|
||||
<ClCompile>
|
||||
<WarningLevel>Level3</WarningLevel>
|
||||
<FunctionLevelLinking>true</FunctionLevelLinking>
|
||||
<IntrinsicFunctions>true</IntrinsicFunctions>
|
||||
<SDLCheck>true</SDLCheck>
|
||||
<PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<ConformanceMode>true</ConformanceMode>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<SubSystem>Console</SubSystem>
|
||||
<EnableCOMDATFolding>true</EnableCOMDATFolding>
|
||||
<OptimizeReferences>true</OptimizeReferences>
|
||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="CardGame.Test.GamePlay2.cpp" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="..\include\Functions.h" />
|
||||
<ClInclude Include="..\include\SqList.h" />
|
||||
<ClInclude Include="..\include\SqQueue.h" />
|
||||
<ClInclude Include="..\include\SqStack.h" />
|
||||
</ItemGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
||||
<ImportGroup Label="ExtensionTargets">
|
||||
</ImportGroup>
|
||||
</Project>
|
||||
@@ -0,0 +1,36 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<ItemGroup>
|
||||
<Filter Include="源文件">
|
||||
<UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>
|
||||
<Extensions>cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
|
||||
</Filter>
|
||||
<Filter Include="头文件">
|
||||
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
|
||||
<Extensions>h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd</Extensions>
|
||||
</Filter>
|
||||
<Filter Include="资源文件">
|
||||
<UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier>
|
||||
<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions>
|
||||
</Filter>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="CardGame.Test.GamePlay2.cpp">
|
||||
<Filter>源文件</Filter>
|
||||
</ClCompile>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="..\include\SqList.h">
|
||||
<Filter>头文件</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="..\include\SqQueue.h">
|
||||
<Filter>头文件</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="..\include\SqStack.h">
|
||||
<Filter>头文件</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="..\include\Functions.h">
|
||||
<Filter>头文件</Filter>
|
||||
</ClInclude>
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
Reference in New Issue
Block a user