feat: deal cards

This commit is contained in:
2024-06-10 16:55:32 +08:00
parent c37a1dadac
commit 3c9e3debf2
8 changed files with 663 additions and 0 deletions

25
CardGame/CardGame.cpp Normal file
View File

@@ -0,0 +1,25 @@
#include <iostream>
using namespace std;
#include "include/SqList.h"
#include "include/SqStack.h"
#include "include/SqQueue.h"
#include "Utils.h"
int main()
{
int N; // 牌的面值数N<10
cout << "Enter N: ";
cin >> N;
if (N < 1 || N > 9)
{
cout << "Invalid input!" << endl;
exit(1);
}
SqList<int>* PA = new SqList<int>;
SqList<int>* PB = new SqList<int>;
InitList(*PA, N);
InitList(*PB, N);
DealCards(N, PA, PB);
DispList(*PA);
DispList(*PB);
}

31
CardGame/CardGame.sln Normal file
View File

@@ -0,0 +1,31 @@
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

144
CardGame/CardGame.vcxproj Normal file
View 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>{55230cd0-3e7c-4259-8e35-015b016b78c0}</ProjectGuid>
<RootNamespace>CardGame</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" />
<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.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="include\SqList.h" />
<ClInclude Include="include\SqQueue.h" />
<ClInclude Include="include\SqStack.h" />
<ClInclude Include="Utils.h" />
</ItemGroup>
<ItemGroup>
<None Include="..\TaskInfo.md" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>

View File

@@ -0,0 +1,39 @@
<?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.cpp">
<Filter>源文件</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="include\SqStack.h">
<Filter>头文件</Filter>
</ClInclude>
<ClInclude Include="include\SqQueue.h">
<Filter>头文件</Filter>
</ClInclude>
<ClInclude Include="Utils.h">
<Filter>头文件</Filter>
</ClInclude>
<ClInclude Include="include\SqList.h">
<Filter>头文件</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<None Include="..\TaskInfo.md" />
</ItemGroup>
</Project>

35
CardGame/Utils.h Normal file
View File

@@ -0,0 +1,35 @@
#pragma once
#include <cstdlib>
#include <ctime>
#include "include/SqList.h"
void DealCards(int N, SqList<int>*& PA, SqList<int>*& PB) // 发牌
{
int* CardPile = new int[N]; // 开始的牌堆
srand((int)time(nullptr)); // 基于当前时间设置随机数种子
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; // 释放牌堆占用的内存
}

178
CardGame/include/SqList.h Normal file
View File

@@ -0,0 +1,178 @@
#pragma once
#include <cstddef>
template <class DT>
struct SqList // 顺序表
{
DT* elem; // 表首址
int length; // 表长
int size; // 表容量
};
//算法2.1
template <class DT>
bool PriorElem_e(SqList<DT> L, DT e, DT& pre_e) // 求值为e的元素前驱
{
int k;
k = LocateElem_e(L, e);
if (k > 1)
{
GetElem_i(L, k - 1, pre_e);
return false;
}
else
return true;
}
//【算法2.2】 // 初始化
template <class DT>
bool InitList(SqList<DT>& L, int m)
{
L.elem = new DT[m]; // 申请表空间
if (L.elem == NULL)
{
cout << "未创建成功!"; // 申请不成功,退出
exit(1);
}
L.length = 0; // 申请成功属性赋值。空表表长为0
L.size = m; // 表容量为m
return true;
}
//【算法2.3】 // 创建表元素
template <class DT>
bool CreateList(SqList<DT>& L, int n)
{
int i;
if (n > L.size) // 1.元素个数大于表容量,不能创建。
{
cout << "元素个数大于表长,不能创建!" << endl;
return false;
}
cout << "请依次输入" << n << "个元素值:" << endl; // 2.依位序输入各元素值
for (i = 1; i <= n; i++)
cin >> L.elem[i - 1];
L.length = n; // 3.表长为创建的元素个数
return true;
}
//【算法2.4】 // 销毁顺序表
template <class DT>
void DestroyList(SqList<DT>& L)
{
delete[] L.elem; // 1.释放表空间
L.length = 0; // 2.属性赋值
L.size = 0;
}
//【算法2.5】 // 获取第i个元素值
template<class DT>
bool GetElem_i(SqList<DT> L, int i, DT& e)
{
if (i<1 || i>L.length) // 1.位序不合理返回false
{
cout << "该元素不存在!" << endl;
return false;
}
e = L.elem[i - 1]; // 2. 否则获取第i个元素值
return true; // 返回true
}
//【算法2.6】 // 按值查找
template<class DT>
int LocateElem_e(SqList<DT> L, DT e)
{
for (int i = 1; i <= L.length; i++) // 顺序查找
if (L.elem[i - 1] == e) // 1.找到
return i; // 返回元素位序
return 0; // 2.未找到返回0
}
//【算法2.7】
template<class DT>
bool InsertElem_i(SqList<DT>& L, int i, DT e)
{
if (L.length >= L.size) // 1.表满,不能插入
return false;
if (i<1 || i>L.length + 1) // 2.插入位置不合理,不能插入
return false;
for (int j = L.length; j >= i; j--) // 3. an~ai依次后移
L.elem[j] = L.elem[j - 1];
L.elem[i - 1] = e;
L.length++;
return true; // 插入成功返回true
}
//【算法2.8】 // 删除第i个元素
template<class DT>
bool DeleElem_i(SqList<DT>& L, int i)
{
if (L.length == 0) // 1.表空,不能删除
return false;
if (i<1 || i>L.length) // 2.删除位置不合理,不能插入
return false;
for (int j = i; j < L.length; j++) // 3. ai+1~an依次前移
L.elem[j - 1] = L.elem[j];
L.length--;
return true; // 删除成功返回true
}
//【算法2.9】
template<class DT>
bool PutElem(SqList<DT>& L, int i, DT e) // 修改第i个元素的值
{
if (i<1 || i>L.length) // 1.位序不合理,不能修改,
return false; // 返回false
L.elem[i - 1] = e; // 2.重置第i个元素值
return true; // 3.修改成功返回true
}
// 清空顺序表
template<class DT>
void ClearList(SqList<DT>& L)
{
L.length = 0; // 空表表长为0
}
// 测表长
template<class DT>
int ListLength(SqList<DT> L)
{
return L.length;
}
template<class DT>
bool ListEmpty(SqList<DT> L) // 测表空
{
if (L.length == 0) // 空表返回true
return true;
else
return false; // 非空表返回false
}
template<class DT>
bool ListFull(SqList<DT> L)
{
if (L.length == L.size) // 表满返回true
return true;
else
return false; // 表不满返回false
}
//【算法2.10】 // 遍历输出
template <class DT>
void DispList(SqList<DT> L)
{
int i;
for (i = 1; i <= L.length; i++) // 依位序输出元素值
{
cout << L.elem[i - 1] << "\t";
}
cout << endl;
}

102
CardGame/include/SqQueue.h Normal file
View File

@@ -0,0 +1,102 @@
#pragma once
template<class DT>
struct SqQueue
{
DT* base; // 存储空间基地址
int front; // 队头指针,指向队首元素
int rear; // 队尾指针,指向队尾元素的后面
int queuesize; // 队列容量
};
template<class DT> // 算法3.14 初始化队列
bool InitQueue(SqQueue<DT>& Q, int m) // 创建容量为m的空队列
{
Q.base = new DT[m]; // 1. 申请一组连续的内存空间
if (!Q.base) exit(1); // 申请失败,退出
Q.front = Q.rear = 0; // 2. 申请成功,为队列属性赋值
Q.queuesize = m;
return true; // 3. 返回true
}
template<class DT> // 算法3.15 销毁队列
void DestroyQueue(SqQueue<DT>& Q) // 销毁循环队列
{
delete[] Q.base; // 1. 释放循环队列占用的内存空间
Q.front = Q.rear = 0; // 2. 为队列属性赋值
Q.queuesize = 0;
}
template<class DT> // 算法3.16 入队
bool EnQueue(SqQueue<DT>& Q, DT e) // 在队尾插入一个新元素
{
if ((Q.rear + 1) % Q.queuesize == Q.front) // 1. 队满的情况
return false; // 无法入队返回false
Q.base[Q.rear] = e; // 2. 元素e放在队尾指针处
Q.rear = (Q.rear + 1) % Q.queuesize; // 队尾指针增1
return true; // 3. 返回true
}
template<class DT> // 算法3.17 出队
bool DeQueue(SqQueue<DT>& Q, DT& e) // 删除队头元素
{
if (Q.front == Q.rear) // 1. 队空的情况
return false; // 无法出队返回false
e = Q.base[Q.front]; // 2. 取队头元素赋值给e
Q.front = (Q.front + 1) % Q.queuesize; // 队头指针加1
return true; // 3. 返回true
}
template<class DT> // 算法3.18 取队头元素
bool GetHead(SqQueue<DT> Q, DT& e) // 取队头元素
{
if (Q.front == Q.rear) // 1. 队空的情况
return false; // 无队头元素返回false
e = Q.base[Q.front]; // 2. 取队头元素赋值给e
return true; // 3. 返回true
}
template<class DT>
bool GetTail(SqQueue<DT> Q, DT& e) // 取队尾元素
{
if (Q.front == Q.rear) // 1. 队空的情况
return false; // 无队尾元素返回false
e = Q.base[(Q.rear - 1 + Q.queuesize) % Q.queuesize]; // 2. 取队尾元素赋值给e
return true; // 3. 返回true
}
template<class DT>
int QueueLength(SqQueue<DT> Q) // 测表长
{
return (Q.rear - Q.front + Q.queuesize) % Q.queuesize;
}
template<class DT>
bool QueueEmpty(SqQueue<DT> Q) // 判队空
{
return Q.front == Q.rear;
}
template<class DT>
bool QueueFull(SqQueue<DT> Q) // 判队满
{
return (Q.rear + 1) % Q.queuesize == Q.front;
}
template<class DT>
void ClearQueue(SqQueue<DT>& Q) // 清空队
{
Q.front = Q.rear = 0;
}
template<class DT>
void DispQueue(SqQueue<DT> Q) // 显示队
{
int i = Q.front;
while (i != Q.rear)
{
cout << Q.base[i] << " ";
i = (i + 1) % Q.queuesize;
}
cout << endl;
}

109
CardGame/include/SqStack.h Normal file
View File

@@ -0,0 +1,109 @@
#pragma once
template <class DT>
struct SqStack // 顺序栈
{
DT* base; // 栈首址
int top; // 栈顶指针
int stacksize; // 栈容量
};
//基本操作的实现
//【算法3.1】 // 初始化栈
template <class DT>
void InitStack(SqStack<DT>& S, int m)
{
S.base = new DT[m]; // 申请栈空间
if (S.base == NULL) // 申请失败,退出
{
cout << "未创建成功!";
exit(1);
}
S.top = -1; // 设置空栈属性
S.stacksize = m;
}
//【算法3.2】 // 销毁栈
template <class DT>
void DestroyStack(SqStack<DT>& S)
{
delete[] S.base; // 释放栈空间
S.top = -1;
S.stacksize = 0; // 设置栈属性
}
//【算法3.3】 // 入栈
template<class DT>
bool Push(SqStack<DT>& S, DT e)
{
if (S.top == S.stacksize - 1) // 栈满,不能入栈
return false; // 返回false
S.top++;
S.base[S.top] = e;
return true; // 入栈成功返回true
}
//【算法3.4】 // 出栈
template<class DT>
bool Pop(SqStack<DT>& S, DT& e)
{
if (S.top == -1) // 栈空
return false; // 返回false
e = S.base[S.top]; // 取栈顶元素
S.top--; // 栈顶指针下移
return true; // 出栈成功返回true
}
//【算法3.5】 // 获取栈顶元素
template<class DT>
bool GetTop(SqStack<DT> S, DT& e)
{
if (S.top == -1) // 栈空
return false; // 返回false
e = S.base[S.top]; // 栈非空,取栈顶元素
return true; // 返回true
}
//测栈空
template<class DT>
bool StackEmpty(SqStack<DT> S)
{
if (S.top == -1) // 空栈返回true
return true;
else // 空栈返回false
return false;
}
//显示栈内容
template<class DT>
void DispStack(SqStack<DT> S)
{
int i = S.top;
while (i >= 0)
{
cout << S.base[i--] << "\t";
}
cout << endl;
}
template<class DT>
void ClearStack(SqStack<DT>& S)
{
S.top = -1;
}
template<class DT>
int StackFull(SqStack<DT> S)
{
if (S.top == S.stacksize - 1)
return 1;
else
return 0;
}