diff --git a/CardGame/.idea/.idea.CardGame/.idea/.gitignore b/.idea/.idea.CardGame/.idea/.gitignore similarity index 100% rename from CardGame/.idea/.idea.CardGame/.idea/.gitignore rename to .idea/.idea.CardGame/.idea/.gitignore index b856034..3745a6c 100644 --- a/CardGame/.idea/.idea.CardGame/.idea/.gitignore +++ b/.idea/.idea.CardGame/.idea/.gitignore @@ -2,10 +2,10 @@ /shelf/ /workspace.xml # Rider 忽略的文件 -/projectSettingsUpdater.xml -/.idea.CardGame.iml -/modules.xml /contentModel.xml +/modules.xml +/.idea.CardGame.iml +/projectSettingsUpdater.xml # 基于编辑器的 HTTP 客户端请求 /httpRequests/ # Datasource local storage ignored files diff --git a/.idea/.idea.CardGame/.idea/.name b/.idea/.idea.CardGame/.idea/.name new file mode 100644 index 0000000..ca27b3b --- /dev/null +++ b/.idea/.idea.CardGame/.idea/.name @@ -0,0 +1 @@ +CardGame \ No newline at end of file diff --git a/CardGame/.idea/.idea.CardGame/.idea/encodings.xml b/.idea/.idea.CardGame/.idea/encodings.xml similarity index 100% rename from CardGame/.idea/.idea.CardGame/.idea/encodings.xml rename to .idea/.idea.CardGame/.idea/encodings.xml diff --git a/CardGame/.idea/.idea.CardGame/.idea/indexLayout.xml b/.idea/.idea.CardGame/.idea/indexLayout.xml similarity index 100% rename from CardGame/.idea/.idea.CardGame/.idea/indexLayout.xml rename to .idea/.idea.CardGame/.idea/indexLayout.xml diff --git a/CardGame/.idea/.idea.CardGame/.idea/vcs.xml b/.idea/.idea.CardGame/.idea/vcs.xml similarity index 69% rename from CardGame/.idea/.idea.CardGame/.idea/vcs.xml rename to .idea/.idea.CardGame/.idea/vcs.xml index 6c0b863..35eb1dd 100644 --- a/CardGame/.idea/.idea.CardGame/.idea/vcs.xml +++ b/.idea/.idea.CardGame/.idea/vcs.xml @@ -1,6 +1,6 @@ - + \ No newline at end of file diff --git a/Build.bat b/Build.bat index 5ba598d..c7086b2 100644 --- a/Build.bat +++ b/Build.bat @@ -3,9 +3,9 @@ set "MSBUILD_PATH=" for /f "delims=" %%i in ('where /r "C:\Program Files\Microsoft Visual Studio" msbuild.exe') do ( set "MSBUILD_PATH=%%i" ) -"%MSBUILD_PATH%" CardGame\CardGame.sln /t:Build /p:Configuration=Debug /p:Platform=x64 +"%MSBUILD_PATH%" CardGame.sln /t:Build /p:Configuration=Debug /p:Platform=x64 if %errorlevel%==0 ( - echo Build succeeded, output folder: CardGame\x64\Debug + echo Build succeeded, output folder: .\x64\Debug ) else ( echo Build failed ) diff --git a/CardGame.Test.GamePlay1/CardGame.Test.GamePlay1.cpp b/CardGame.Test.GamePlay1/CardGame.Test.GamePlay1.cpp new file mode 100644 index 0000000..c515421 --- /dev/null +++ b/CardGame.Test.GamePlay1/CardGame.Test.GamePlay1.cpp @@ -0,0 +1,156 @@ +#include +using namespace std; +#include "SqList.h" +#include "SqQueue.h" +#include "SqStack.h" +#include "Functions.h" + +// 发牌 +void DealCards(int N, SqList*& PA, SqList*& 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& PA, SqList& PB, int N) +{ + int winner = 0; + SqStack* Desk = new SqStack; // 牌桌上的牌 + InitStack(*Desk, N * 2 + 1); + // 玩法1初始化 + // 对于玩法1,双方均为先抓到的牌先出,赢的牌加在牌尾 + SqQueue* QA = new SqQueue; + InitQueue(*QA, N * 2 + 1); + SqQueue* QB = new SqQueue; + 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* PA = new SqList; // PA的手牌 + InitList(*PA, N); + SqList* PB = new SqList; // 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; +} \ No newline at end of file diff --git a/CardGame.Test.GamePlay1/CardGame.Test.GamePlay1.vcxproj b/CardGame.Test.GamePlay1/CardGame.Test.GamePlay1.vcxproj new file mode 100644 index 0000000..d02b97d --- /dev/null +++ b/CardGame.Test.GamePlay1/CardGame.Test.GamePlay1.vcxproj @@ -0,0 +1,144 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + 17.0 + Win32Proj + {6dcad147-f66e-4988-bac6-d76544975120} + CardGameTestGamePlay1 + 10.0 + + + + Application + true + v143 + Unicode + + + Application + false + v143 + true + Unicode + + + Application + true + v143 + Unicode + + + Application + false + v143 + true + Unicode + + + + + + + + + + + + + + + + + + + + + ..\include;$(IncludePath) + + + + Level3 + true + WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + true + true + true + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + Level3 + true + _DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + true + true + true + NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/CardGame.Test.GamePlay1/CardGame.Test.GamePlay1.vcxproj.filters b/CardGame.Test.GamePlay1/CardGame.Test.GamePlay1.vcxproj.filters new file mode 100644 index 0000000..a0506c8 --- /dev/null +++ b/CardGame.Test.GamePlay1/CardGame.Test.GamePlay1.vcxproj.filters @@ -0,0 +1,36 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + 源文件 + + + + + 头文件 + + + 头文件 + + + 头文件 + + + 头文件 + + + \ No newline at end of file diff --git a/CardGame.Test.GamePlay2/CardGame.Test.GamePlay2.cpp b/CardGame.Test.GamePlay2/CardGame.Test.GamePlay2.cpp new file mode 100644 index 0000000..ea52fe7 --- /dev/null +++ b/CardGame.Test.GamePlay2/CardGame.Test.GamePlay2.cpp @@ -0,0 +1,200 @@ +#include +using namespace std; +#include "SqList.h" +#include "SqQueue.h" +#include "SqStack.h" +#include "Functions.h" + +// 发牌 +void DealCards(int N, SqList*& PA, SqList*& 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& 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& PA, SqList& PB, int N) +{ + int winner = 0; + SqStack* Desk = new SqStack; // 牌桌上的牌 + InitStack(*Desk, N * 2 + 1); + // 玩法2初始化 + // 对于玩法2,PA为先抓到的牌先出,赢的牌加在牌尾;PB为任意出牌 + SqQueue* QA = new SqQueue; + 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* PA = new SqList; // PA的手牌 + InitList(*PA, N); + SqList* PB = new SqList; // 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; +} \ No newline at end of file diff --git a/CardGame.Test.GamePlay2/CardGame.Test.GamePlay2.vcxproj b/CardGame.Test.GamePlay2/CardGame.Test.GamePlay2.vcxproj new file mode 100644 index 0000000..a359c73 --- /dev/null +++ b/CardGame.Test.GamePlay2/CardGame.Test.GamePlay2.vcxproj @@ -0,0 +1,144 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + 17.0 + Win32Proj + {6383c4b6-1282-4f20-864a-7208ec5805ca} + CardGameTestGamePlay2 + 10.0 + + + + Application + true + v143 + Unicode + + + Application + false + v143 + true + Unicode + + + Application + true + v143 + Unicode + + + Application + false + v143 + true + Unicode + + + + + + + + + + + + + + + + + + + + + ..\include;$(IncludePath) + + + + Level3 + true + WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + true + true + true + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + Level3 + true + _DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + true + true + true + NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/CardGame.Test.GamePlay2/CardGame.Test.GamePlay2.vcxproj.filters b/CardGame.Test.GamePlay2/CardGame.Test.GamePlay2.vcxproj.filters new file mode 100644 index 0000000..d8b4a43 --- /dev/null +++ b/CardGame.Test.GamePlay2/CardGame.Test.GamePlay2.vcxproj.filters @@ -0,0 +1,36 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + 源文件 + + + + + 头文件 + + + 头文件 + + + 头文件 + + + 头文件 + + + \ No newline at end of file diff --git a/CardGame.sln b/CardGame.sln new file mode 100644 index 0000000..3743fb0 --- /dev/null +++ b/CardGame.sln @@ -0,0 +1,51 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.10.34928.147 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "CardGame", "CardGame\CardGame.vcxproj", "{4AE7BB62-ADF7-4452-8E58-21B8F9972CE2}" +EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "CardGame.Test.GamePlay1", "CardGame.Test.GamePlay1\CardGame.Test.GamePlay1.vcxproj", "{6DCAD147-F66E-4988-BAC6-D76544975120}" +EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "CardGame.Test.GamePlay2", "CardGame.Test.GamePlay2\CardGame.Test.GamePlay2.vcxproj", "{6383C4B6-1282-4F20-864A-7208EC5805CA}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|x64 = Debug|x64 + Debug|x86 = Debug|x86 + Release|x64 = Release|x64 + Release|x86 = Release|x86 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {4AE7BB62-ADF7-4452-8E58-21B8F9972CE2}.Debug|x64.ActiveCfg = Debug|x64 + {4AE7BB62-ADF7-4452-8E58-21B8F9972CE2}.Debug|x64.Build.0 = Debug|x64 + {4AE7BB62-ADF7-4452-8E58-21B8F9972CE2}.Debug|x86.ActiveCfg = Debug|Win32 + {4AE7BB62-ADF7-4452-8E58-21B8F9972CE2}.Debug|x86.Build.0 = Debug|Win32 + {4AE7BB62-ADF7-4452-8E58-21B8F9972CE2}.Release|x64.ActiveCfg = Release|x64 + {4AE7BB62-ADF7-4452-8E58-21B8F9972CE2}.Release|x64.Build.0 = Release|x64 + {4AE7BB62-ADF7-4452-8E58-21B8F9972CE2}.Release|x86.ActiveCfg = Release|Win32 + {4AE7BB62-ADF7-4452-8E58-21B8F9972CE2}.Release|x86.Build.0 = Release|Win32 + {6DCAD147-F66E-4988-BAC6-D76544975120}.Debug|x64.ActiveCfg = Debug|x64 + {6DCAD147-F66E-4988-BAC6-D76544975120}.Debug|x64.Build.0 = Debug|x64 + {6DCAD147-F66E-4988-BAC6-D76544975120}.Debug|x86.ActiveCfg = Debug|Win32 + {6DCAD147-F66E-4988-BAC6-D76544975120}.Debug|x86.Build.0 = Debug|Win32 + {6DCAD147-F66E-4988-BAC6-D76544975120}.Release|x64.ActiveCfg = Release|x64 + {6DCAD147-F66E-4988-BAC6-D76544975120}.Release|x64.Build.0 = Release|x64 + {6DCAD147-F66E-4988-BAC6-D76544975120}.Release|x86.ActiveCfg = Release|Win32 + {6DCAD147-F66E-4988-BAC6-D76544975120}.Release|x86.Build.0 = Release|Win32 + {6383C4B6-1282-4F20-864A-7208EC5805CA}.Debug|x64.ActiveCfg = Debug|x64 + {6383C4B6-1282-4F20-864A-7208EC5805CA}.Debug|x64.Build.0 = Debug|x64 + {6383C4B6-1282-4F20-864A-7208EC5805CA}.Debug|x86.ActiveCfg = Debug|Win32 + {6383C4B6-1282-4F20-864A-7208EC5805CA}.Debug|x86.Build.0 = Debug|Win32 + {6383C4B6-1282-4F20-864A-7208EC5805CA}.Release|x64.ActiveCfg = Release|x64 + {6383C4B6-1282-4F20-864A-7208EC5805CA}.Release|x64.Build.0 = Release|x64 + {6383C4B6-1282-4F20-864A-7208EC5805CA}.Release|x86.ActiveCfg = Release|Win32 + {6383C4B6-1282-4F20-864A-7208EC5805CA}.Release|x86.Build.0 = Release|Win32 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {66B7D78E-C24E-4C45-BD6A-844C0787B646} + EndGlobalSection +EndGlobal diff --git a/CardGame/CardGame.cpp b/CardGame/CardGame.cpp index f7dc53f..db291ac 100644 --- a/CardGame/CardGame.cpp +++ b/CardGame/CardGame.cpp @@ -1,6 +1,6 @@ #include using namespace std; -#include "include/SqList.h" +#include "SqList.h" #include "Functions.h" #include "FuncRef.h" @@ -32,4 +32,6 @@ int main() DestroyList(*PA); DestroyList(*PB); + delete PA; + delete PB; } \ No newline at end of file diff --git a/CardGame/CardGame.sln b/CardGame/CardGame.sln deleted file mode 100644 index a85d4e9..0000000 --- a/CardGame/CardGame.sln +++ /dev/null @@ -1,31 +0,0 @@ - -Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio Version 17 -VisualStudioVersion = 17.10.34928.147 -MinimumVisualStudioVersion = 10.0.40219.1 -Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "CardGame", "CardGame.vcxproj", "{55230CD0-3E7C-4259-8E35-015B016B78C0}" -EndProject -Global - GlobalSection(SolutionConfigurationPlatforms) = preSolution - Debug|x64 = Debug|x64 - Debug|x86 = Debug|x86 - Release|x64 = Release|x64 - Release|x86 = Release|x86 - EndGlobalSection - GlobalSection(ProjectConfigurationPlatforms) = postSolution - {55230CD0-3E7C-4259-8E35-015B016B78C0}.Debug|x64.ActiveCfg = Debug|x64 - {55230CD0-3E7C-4259-8E35-015B016B78C0}.Debug|x64.Build.0 = Debug|x64 - {55230CD0-3E7C-4259-8E35-015B016B78C0}.Debug|x86.ActiveCfg = Debug|Win32 - {55230CD0-3E7C-4259-8E35-015B016B78C0}.Debug|x86.Build.0 = Debug|Win32 - {55230CD0-3E7C-4259-8E35-015B016B78C0}.Release|x64.ActiveCfg = Release|x64 - {55230CD0-3E7C-4259-8E35-015B016B78C0}.Release|x64.Build.0 = Release|x64 - {55230CD0-3E7C-4259-8E35-015B016B78C0}.Release|x86.ActiveCfg = Release|Win32 - {55230CD0-3E7C-4259-8E35-015B016B78C0}.Release|x86.Build.0 = Release|Win32 - EndGlobalSection - GlobalSection(SolutionProperties) = preSolution - HideSolutionNode = FALSE - EndGlobalSection - GlobalSection(ExtensibilityGlobals) = postSolution - SolutionGuid = {19A11810-2A53-472C-97A8-51E7B18A28D3} - EndGlobalSection -EndGlobal diff --git a/CardGame/CardGame.vcxproj b/CardGame/CardGame.vcxproj index 5869263..6024606 100644 --- a/CardGame/CardGame.vcxproj +++ b/CardGame/CardGame.vcxproj @@ -21,7 +21,7 @@ 17.0 Win32Proj - {55230cd0-3e7c-4259-8e35-015b016b78c0} + {4ae7bb62-adf7-4452-8e58-21b8f9972ce2} CardGame 10.0 @@ -70,6 +70,9 @@ + + ..\include;$(IncludePath) + Level3 @@ -132,15 +135,11 @@ + + + + - - - - - - - - diff --git a/CardGame/CardGame.vcxproj.filters b/CardGame/CardGame.vcxproj.filters index 240ee54..51b4249 100644 --- a/CardGame/CardGame.vcxproj.filters +++ b/CardGame/CardGame.vcxproj.filters @@ -26,24 +26,20 @@ - - 头文件 - - - 头文件 - - - 头文件 - - - 头文件 - 头文件 - - - - + + 头文件 + + + 头文件 + + + 头文件 + + + 头文件 + \ No newline at end of file diff --git a/CardGame/FuncRef.h b/CardGame/FuncRef.h index 3790a73..7ef3192 100644 --- a/CardGame/FuncRef.h +++ b/CardGame/FuncRef.h @@ -3,7 +3,7 @@ #ifndef _FUNCREF_H #define _FUNCREF_H -void GamePlay1(SqList PA, SqList PB, int N); -void GamePlay2(SqList PA, SqList PB, int N); +void GamePlay1(SqList& PA, SqList& PB, int N); +void GamePlay2(SqList& PA, SqList& PB, int N); #endif \ No newline at end of file diff --git a/CardGame/GamePlay1.cpp b/CardGame/GamePlay1.cpp index 6fea6eb..b3a1f95 100644 --- a/CardGame/GamePlay1.cpp +++ b/CardGame/GamePlay1.cpp @@ -1,8 +1,8 @@ #include using namespace std; -#include "include/SqList.h" -#include "include/SqQueue.h" -#include "include/SqStack.h" +#include "SqList.h" +#include "SqQueue.h" +#include "SqStack.h" #include "FuncRef.h" #include "Functions.h" @@ -18,7 +18,7 @@ void DisplayStatus(SqQueue& QA, SqQueue& QB, SqStack& Desk, int i DispStack(Desk); } -void GamePlay1(SqList PA, SqList PB, int N) +void GamePlay1(SqList& PA, SqList& PB, int N) { SqStack* Desk = new SqStack; // 牌桌上的牌 InitStack(*Desk, N * 2 + 1); @@ -98,4 +98,7 @@ void GamePlay1(SqList PA, SqList PB, int N) DestroyStack(*Desk); DestroyQueue(*QA); DestroyQueue(*QB); + delete Desk; + delete QA; + delete QB; } \ No newline at end of file diff --git a/CardGame/GamePlay2.cpp b/CardGame/GamePlay2.cpp index d9d56ac..6f906f4 100644 --- a/CardGame/GamePlay2.cpp +++ b/CardGame/GamePlay2.cpp @@ -1,12 +1,12 @@ #include using namespace std; -#include "include/SqList.h" -#include "include/SqQueue.h" -#include "include/SqStack.h" +#include "SqList.h" +#include "SqQueue.h" +#include "SqStack.h" #include "FuncRef.h" #include "Functions.h" -void DisplayPlayerBCards(int* QB, int N) +void DisplayPlayerBCards(int*& QB, int N) { for (int i = 0; i < N; i++) for (int j = 0; j < QB[i]; j++) @@ -14,7 +14,7 @@ void DisplayPlayerBCards(int* QB, int N) cout << endl; } -void DisplayStatus(SqQueue& QA, int* QB, SqStack& Desk, int i, int N) +void DisplayStatus(SqQueue& QA, int*& QB, SqStack& Desk, int i, int N) { cout << "------------------------------" << endl; cout << "Round " << i << endl; @@ -26,7 +26,7 @@ void DisplayStatus(SqQueue& QA, int* QB, SqStack& Desk, int i, int N) DispStack(Desk); } -bool IsPlayerBWin(int* QB, int N) +bool IsPlayerBWin(int*& QB, int N) { for (int i = 0; i < N; i++) if (QB[i] > 0) return false; @@ -34,7 +34,7 @@ bool IsPlayerBWin(int* QB, int N) } // 玩家B出牌 -int PlayerBPlay(int* QB, SqStack& Desk, int N) +int PlayerBPlay(int*& QB, SqStack& Desk, int N) { // 如果栈为空,且B有两张相同面值的牌,则出该面值的牌 if (StackEmpty(Desk)) @@ -56,7 +56,7 @@ int PlayerBPlay(int* QB, SqStack& Desk, int N) return card; } -void GamePlay2(SqList PA, SqList PB, int N) +void GamePlay2(SqList& PA, SqList& PB, int N) { SqStack* Desk = new SqStack; // 牌桌上的牌 InitStack(*Desk, N * 2 + 1); @@ -142,5 +142,7 @@ void GamePlay2(SqList PA, SqList PB, int N) } DestroyStack(*Desk); DestroyQueue(*QA); + delete Desk; + delete QA; delete[] QB; } \ No newline at end of file diff --git a/ProjectStructure.md b/ProjectStructure.md index d93f183..b3b84ce 100644 --- a/ProjectStructure.md +++ b/ProjectStructure.md @@ -1,12 +1,18 @@ +- .git _Git related_ +_ .idea _JetBrains IDE (Rider) related_ +- .vs _Visual Studio related_ - **CardGame** - - .vs _Visual Studio related_ - - CardGame _Files generated while compiling_ - - **include** _外部头文件_ - - x64 (or any other architecture) _Binary file and debugging info_ + - x64 (or any other architecture) _Files generated while compiling_ - **CardGame.cpp** _主代码文件,包含 main 函数(程序入口)_ - - CardGame.sln _Solution file_ - CardGame.vcxproj.\* _VC++ project file_ - FuncRef.h _用于在 CardGame.cpp 的主函数下调用另外两个 cpp 中的函数_ - **GamePlay1.cpp** _玩法 1_ - **GamePlay2.cpp** _玩法 2_ +- **CardGame.Test.GamePlay\*** _测试用代码_ +- **include** _外部头文件_ - **Functions.h** _部分函数的定义_ + - **SqList.h** _顺序表_ + - **SqQueue.h** _顺序队列_ + - **SqStack.h** _顺序栈_ +- x64 (or any other architecture) _Binary files and debugging info_ +- CardGame.sln _Solution file_ \ No newline at end of file diff --git a/CardGame/Functions.h b/include/Functions.h similarity index 95% rename from CardGame/Functions.h rename to include/Functions.h index 479c11c..13fbf5c 100644 --- a/CardGame/Functions.h +++ b/include/Functions.h @@ -1,8 +1,8 @@ #pragma once #include #include -#include "include/SqList.h" -#include "include/SqStack.h" +#include "SqList.h" +#include "SqStack.h" // 发牌 inline void DealCards(int N, SqList*& PA, SqList*& PB) diff --git a/CardGame/include/SqList.h b/include/SqList.h similarity index 100% rename from CardGame/include/SqList.h rename to include/SqList.h diff --git a/CardGame/include/SqQueue.h b/include/SqQueue.h similarity index 100% rename from CardGame/include/SqQueue.h rename to include/SqQueue.h diff --git a/CardGame/include/SqStack.h b/include/SqStack.h similarity index 100% rename from CardGame/include/SqStack.h rename to include/SqStack.h