From ab7a5c516994c24982cffedfa789433a05c9f060 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=B6=BE=E7=80=AC=E6=A1=83=E6=A1=83?= Date: Mon, 17 Jun 2024 00:59:30 +0800 Subject: [PATCH] feat: card play 1 --- Build.bat | 12 +++++ CardGame/CardGame.cpp | 2 +- CardGame/CardGame.vcxproj | 3 +- CardGame/CardGame.vcxproj.filters | 3 +- CardGame/FuncRef.h | 2 - CardGame/{Utils.h => Functions.h} | 20 ++++++- CardGame/GamePlay1.cpp | 90 +++++++++++++++++++++++++++++-- CardGame/GamePlay2.cpp | 19 ++++++- CardGame/include/LinkList.h | 2 +- CardGame/include/SqList.h | 2 +- CardGame/include/SqStack.h | 2 +- ProjectStructure.md | 12 +++++ 12 files changed, 154 insertions(+), 15 deletions(-) create mode 100644 Build.bat rename CardGame/{Utils.h => Functions.h} (68%) create mode 100644 ProjectStructure.md diff --git a/Build.bat b/Build.bat new file mode 100644 index 0000000..5ba598d --- /dev/null +++ b/Build.bat @@ -0,0 +1,12 @@ +@echo off +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 +if %errorlevel%==0 ( + echo Build succeeded, output folder: CardGame\x64\Debug +) else ( + echo Build failed +) +timeout /t 5 \ No newline at end of file diff --git a/CardGame/CardGame.cpp b/CardGame/CardGame.cpp index b7fdfed..3f47372 100644 --- a/CardGame/CardGame.cpp +++ b/CardGame/CardGame.cpp @@ -1,7 +1,7 @@ #include using namespace std; #include "include/SqList.h" -#include "Utils.h" +#include "Functions.h" #include "FuncRef.h" int main() diff --git a/CardGame/CardGame.vcxproj b/CardGame/CardGame.vcxproj index 1d7f882..7b34f64 100644 --- a/CardGame/CardGame.vcxproj +++ b/CardGame/CardGame.vcxproj @@ -137,9 +137,10 @@ - + + diff --git a/CardGame/CardGame.vcxproj.filters b/CardGame/CardGame.vcxproj.filters index c726a83..7ee0dd6 100644 --- a/CardGame/CardGame.vcxproj.filters +++ b/CardGame/CardGame.vcxproj.filters @@ -32,7 +32,7 @@ 头文件 - + 头文件 @@ -47,5 +47,6 @@ + \ No newline at end of file diff --git a/CardGame/FuncRef.h b/CardGame/FuncRef.h index 3cd379f..3790a73 100644 --- a/CardGame/FuncRef.h +++ b/CardGame/FuncRef.h @@ -1,7 +1,5 @@ #pragma once -// 用于在CardGame.cpp的主函数下调用另外两个cpp中的函数 - #ifndef _FUNCREF_H #define _FUNCREF_H diff --git a/CardGame/Utils.h b/CardGame/Functions.h similarity index 68% rename from CardGame/Utils.h rename to CardGame/Functions.h index eda46f7..7ad582e 100644 --- a/CardGame/Utils.h +++ b/CardGame/Functions.h @@ -2,9 +2,10 @@ #include #include #include "include/SqList.h" +#include "include/SqStack.h" // 发牌 -void DealCards(int N, SqList*& PA, SqList*& PB) +inline void DealCards(int N, SqList*& PA, SqList*& PB) { int* CardPile = new int[N]; // 发牌堆 srand((int)time(nullptr)); // 基于当前时间设置随机数种子 @@ -34,3 +35,20 @@ void DealCards(int N, SqList*& PA, SqList*& PB) } delete[] CardPile; // 释放发牌堆占用的内存 } + +// 寻找栈内元素并返回其位置 +inline int FindElem(SqStack& S, int e) +{ + int i = S.top; // 从栈顶开始查找 + for (; i >= 0; i--) + { + if (S.base[i] == e) return S.top - i; // 返回距离 + } + return -1; +} + +// 判断谁的回合 +inline bool IsPlayerA(int i) +{ + return i % 2 == 1; +} \ No newline at end of file diff --git a/CardGame/GamePlay1.cpp b/CardGame/GamePlay1.cpp index bfdbee3..66e444b 100644 --- a/CardGame/GamePlay1.cpp +++ b/CardGame/GamePlay1.cpp @@ -4,16 +4,98 @@ using namespace std; #include "include/SqQueue.h" #include "include/SqStack.h" #include "FuncRef.h" +#include "Functions.h" + +void DisplayStatus(SqQueue& QA, SqQueue& QB, SqStack& 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 PA, SqList PB, int N) { SqStack* Desk = new SqStack; // 牌桌上的牌 - InitStack(*Desk, N * 2); + InitStack(*Desk, N * 2 + 1); // 玩法1初始化 // 对于玩法1,双方均为先抓到的牌先出,赢的牌加在牌尾 SqQueue* QA = new SqQueue; - InitQueue(*QA, N); + InitQueue(*QA, 2 * N + 1); SqQueue* QB = new SqQueue; - InitQueue(*QB, N); - // TODO + InitQueue(*QB, 2 * N + 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); + } + } + } + DisplayStatus(*QA, *QB, *Desk, i); + // 判断是否有一方获胜 + if (QueueEmpty(*QA)) + { + cout << "Player B wins!" << endl; + break; + } + else if (QueueEmpty(*QB)) + { + cout << "Player A wins!" << endl; + break; + } + } + delete Desk; + DestroyQueue(*QA); + DestroyQueue(*QB); } \ No newline at end of file diff --git a/CardGame/GamePlay2.cpp b/CardGame/GamePlay2.cpp index cd48525..753eda4 100644 --- a/CardGame/GamePlay2.cpp +++ b/CardGame/GamePlay2.cpp @@ -13,8 +13,23 @@ void GamePlay2(SqList PA, SqList PB, int N) // 玩法2初始化 // 对于玩法2,PA为先抓到的牌先出,赢的牌加在牌尾;PB为任意出牌 SqQueue* QA = new SqQueue; - InitQueue(*QA, N); + InitQueue(*QA, N + 1); LNode* QB; InitList(QB); - // TODO + // 将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); + InsertElem_i(QB, i, e); + } + DispQueue(*QA); + DispList(QB); } \ No newline at end of file diff --git a/CardGame/include/LinkList.h b/CardGame/include/LinkList.h index 2d5b5a1..a5833a6 100644 --- a/CardGame/include/LinkList.h +++ b/CardGame/include/LinkList.h @@ -247,7 +247,7 @@ void DispList(LNode
* L) // 显示表内容 p = L; // 从首元结点开始遍历 while (p->next) // 2.依次输出各结点值 { - p = p->next; cout << p->data << "\t"; + p = p->next; cout << p->data << " "; } diff --git a/CardGame/include/SqList.h b/CardGame/include/SqList.h index 9bc49ba..15cd9ca 100644 --- a/CardGame/include/SqList.h +++ b/CardGame/include/SqList.h @@ -169,7 +169,7 @@ void DispList(SqList
L) int i; for (i = 1; i <= L.length; i++) // 依位序输出元素值 { - cout << L.elem[i - 1] << "\t"; + cout << L.elem[i - 1] << " "; } cout << endl; diff --git a/CardGame/include/SqStack.h b/CardGame/include/SqStack.h index 273e06d..e3fbe36 100644 --- a/CardGame/include/SqStack.h +++ b/CardGame/include/SqStack.h @@ -84,7 +84,7 @@ void DispStack(SqStack
S) int i = S.top; while (i >= 0) { - cout << S.base[i--] << "\t"; + cout << S.base[i--] << " "; } cout << endl; } diff --git a/ProjectStructure.md b/ProjectStructure.md new file mode 100644 index 0000000..d93f183 --- /dev/null +++ b/ProjectStructure.md @@ -0,0 +1,12 @@ +- **CardGame** + - .vs _Visual Studio related_ + - CardGame _Files generated while compiling_ + - **include** _外部头文件_ + - x64 (or any other architecture) _Binary file and debugging info_ + - **CardGame.cpp** _主代码文件,包含 main 函数(程序入口)_ + - CardGame.sln _Solution file_ + - CardGame.vcxproj.\* _VC++ project file_ + - FuncRef.h _用于在 CardGame.cpp 的主函数下调用另外两个 cpp 中的函数_ + - **GamePlay1.cpp** _玩法 1_ + - **GamePlay2.cpp** _玩法 2_ + - **Functions.h** _部分函数的定义_