feat: card play 1
This commit is contained in:
12
Build.bat
Normal file
12
Build.bat
Normal file
@@ -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
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
using namespace std;
|
using namespace std;
|
||||||
#include "include/SqList.h"
|
#include "include/SqList.h"
|
||||||
#include "Utils.h"
|
#include "Functions.h"
|
||||||
#include "FuncRef.h"
|
#include "FuncRef.h"
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
|
|||||||
@@ -137,9 +137,10 @@
|
|||||||
<ClInclude Include="include\SqList.h" />
|
<ClInclude Include="include\SqList.h" />
|
||||||
<ClInclude Include="include\SqQueue.h" />
|
<ClInclude Include="include\SqQueue.h" />
|
||||||
<ClInclude Include="include\SqStack.h" />
|
<ClInclude Include="include\SqStack.h" />
|
||||||
<ClInclude Include="Utils.h" />
|
<ClInclude Include="Functions.h" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<None Include="..\ProjectStructure.md" />
|
||||||
<None Include="..\TaskInfo.md" />
|
<None Include="..\TaskInfo.md" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
||||||
|
|||||||
@@ -32,7 +32,7 @@
|
|||||||
<ClInclude Include="include\SqQueue.h">
|
<ClInclude Include="include\SqQueue.h">
|
||||||
<Filter>头文件</Filter>
|
<Filter>头文件</Filter>
|
||||||
</ClInclude>
|
</ClInclude>
|
||||||
<ClInclude Include="Utils.h">
|
<ClInclude Include="Functions.h">
|
||||||
<Filter>头文件</Filter>
|
<Filter>头文件</Filter>
|
||||||
</ClInclude>
|
</ClInclude>
|
||||||
<ClInclude Include="include\SqList.h">
|
<ClInclude Include="include\SqList.h">
|
||||||
@@ -47,5 +47,6 @@
|
|||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<None Include="..\TaskInfo.md" />
|
<None Include="..\TaskInfo.md" />
|
||||||
|
<None Include="..\ProjectStructure.md" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
</Project>
|
</Project>
|
||||||
@@ -1,7 +1,5 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
// 用于在CardGame.cpp的主函数下调用另外两个cpp中的函数
|
|
||||||
|
|
||||||
#ifndef _FUNCREF_H
|
#ifndef _FUNCREF_H
|
||||||
#define _FUNCREF_H
|
#define _FUNCREF_H
|
||||||
|
|
||||||
|
|||||||
@@ -2,9 +2,10 @@
|
|||||||
#include <cstdlib>
|
#include <cstdlib>
|
||||||
#include <ctime>
|
#include <ctime>
|
||||||
#include "include/SqList.h"
|
#include "include/SqList.h"
|
||||||
|
#include "include/SqStack.h"
|
||||||
|
|
||||||
// 发牌
|
// 发牌
|
||||||
void DealCards(int N, SqList<int>*& PA, SqList<int>*& PB)
|
inline void DealCards(int N, SqList<int>*& PA, SqList<int>*& PB)
|
||||||
{
|
{
|
||||||
int* CardPile = new int[N]; // 发牌堆
|
int* CardPile = new int[N]; // 发牌堆
|
||||||
srand((int)time(nullptr)); // 基于当前时间设置随机数种子
|
srand((int)time(nullptr)); // 基于当前时间设置随机数种子
|
||||||
@@ -34,3 +35,20 @@ void DealCards(int N, SqList<int>*& PA, SqList<int>*& PB)
|
|||||||
}
|
}
|
||||||
delete[] CardPile; // 释放发牌堆占用的内存
|
delete[] CardPile; // 释放发牌堆占用的内存
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 寻找栈内元素并返回其位置
|
||||||
|
inline int FindElem(SqStack<int>& 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;
|
||||||
|
}
|
||||||
@@ -4,16 +4,98 @@ using namespace std;
|
|||||||
#include "include/SqQueue.h"
|
#include "include/SqQueue.h"
|
||||||
#include "include/SqStack.h"
|
#include "include/SqStack.h"
|
||||||
#include "FuncRef.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)
|
void GamePlay1(SqList<int> PA, SqList<int> PB, int N)
|
||||||
{
|
{
|
||||||
SqStack<int>* Desk = new SqStack<int>; // 牌桌上的牌
|
SqStack<int>* Desk = new SqStack<int>; // 牌桌上的牌
|
||||||
InitStack(*Desk, N * 2);
|
InitStack(*Desk, N * 2 + 1);
|
||||||
// 玩法1初始化
|
// 玩法1初始化
|
||||||
// 对于玩法1,双方均为先抓到的牌先出,赢的牌加在牌尾
|
// 对于玩法1,双方均为先抓到的牌先出,赢的牌加在牌尾
|
||||||
SqQueue<int>* QA = new SqQueue<int>;
|
SqQueue<int>* QA = new SqQueue<int>;
|
||||||
InitQueue(*QA, N);
|
InitQueue(*QA, 2 * N + 1);
|
||||||
SqQueue<int>* QB = new SqQueue<int>;
|
SqQueue<int>* QB = new SqQueue<int>;
|
||||||
InitQueue(*QB, N);
|
InitQueue(*QB, 2 * N + 1);
|
||||||
// TODO
|
// 将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);
|
||||||
}
|
}
|
||||||
@@ -13,8 +13,23 @@ void GamePlay2(SqList<int> PA, SqList<int> PB, int N)
|
|||||||
// 玩法2初始化
|
// 玩法2初始化
|
||||||
// 对于玩法2,PA为先抓到的牌先出,赢的牌加在牌尾;PB为任意出牌
|
// 对于玩法2,PA为先抓到的牌先出,赢的牌加在牌尾;PB为任意出牌
|
||||||
SqQueue<int>* QA = new SqQueue<int>;
|
SqQueue<int>* QA = new SqQueue<int>;
|
||||||
InitQueue(*QA, N);
|
InitQueue(*QA, N + 1);
|
||||||
LNode<int>* QB;
|
LNode<int>* QB;
|
||||||
InitList(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);
|
||||||
}
|
}
|
||||||
@@ -247,7 +247,7 @@ void DispList(LNode<DT>* L) // 显示表内容
|
|||||||
p = L; // 从首元结点开始遍历
|
p = L; // 从首元结点开始遍历
|
||||||
while (p->next) // 2.依次输出各结点值
|
while (p->next) // 2.依次输出各结点值
|
||||||
{
|
{
|
||||||
p = p->next; cout << p->data << "\t";
|
p = p->next; cout << p->data << " ";
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -169,7 +169,7 @@ void DispList(SqList<DT> L)
|
|||||||
int i;
|
int i;
|
||||||
for (i = 1; i <= L.length; i++) // 依位序输出元素值
|
for (i = 1; i <= L.length; i++) // 依位序输出元素值
|
||||||
{
|
{
|
||||||
cout << L.elem[i - 1] << "\t";
|
cout << L.elem[i - 1] << " ";
|
||||||
|
|
||||||
}
|
}
|
||||||
cout << endl;
|
cout << endl;
|
||||||
|
|||||||
@@ -84,7 +84,7 @@ void DispStack(SqStack<DT> S)
|
|||||||
int i = S.top;
|
int i = S.top;
|
||||||
while (i >= 0)
|
while (i >= 0)
|
||||||
{
|
{
|
||||||
cout << S.base[i--] << "\t";
|
cout << S.base[i--] << " ";
|
||||||
}
|
}
|
||||||
cout << endl;
|
cout << endl;
|
||||||
}
|
}
|
||||||
|
|||||||
12
ProjectStructure.md
Normal file
12
ProjectStructure.md
Normal file
@@ -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** _部分函数的定义_
|
||||||
Reference in New Issue
Block a user