diff --git a/CardGame/CardGame.cpp b/CardGame/CardGame.cpp new file mode 100644 index 0000000..ee11f8a --- /dev/null +++ b/CardGame/CardGame.cpp @@ -0,0 +1,25 @@ +#include +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* PA = new SqList; + SqList* PB = new SqList; + InitList(*PA, N); + InitList(*PB, N); + DealCards(N, PA, PB); + DispList(*PA); + DispList(*PB); +} \ No newline at end of file diff --git a/CardGame/CardGame.sln b/CardGame/CardGame.sln new file mode 100644 index 0000000..a85d4e9 --- /dev/null +++ b/CardGame/CardGame.sln @@ -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 diff --git a/CardGame/CardGame.vcxproj b/CardGame/CardGame.vcxproj new file mode 100644 index 0000000..6242a69 --- /dev/null +++ b/CardGame/CardGame.vcxproj @@ -0,0 +1,144 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + 17.0 + Win32Proj + {55230cd0-3e7c-4259-8e35-015b016b78c0} + CardGame + 10.0 + + + + Application + true + v143 + Unicode + + + Application + false + v143 + true + Unicode + + + Application + true + v143 + Unicode + + + Application + false + v143 + true + Unicode + + + + + + + + + + + + + + + + + + + + + + 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/CardGame.vcxproj.filters b/CardGame/CardGame.vcxproj.filters new file mode 100644 index 0000000..0b0adcc --- /dev/null +++ b/CardGame/CardGame.vcxproj.filters @@ -0,0 +1,39 @@ + + + + + {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/Utils.h b/CardGame/Utils.h new file mode 100644 index 0000000..dfb8290 --- /dev/null +++ b/CardGame/Utils.h @@ -0,0 +1,35 @@ +#pragma once +#include +#include +#include "include/SqList.h" + +void DealCards(int N, SqList*& PA, SqList*& 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; // 释放牌堆占用的内存 +} diff --git a/CardGame/include/SqList.h b/CardGame/include/SqList.h new file mode 100644 index 0000000..9bc49ba --- /dev/null +++ b/CardGame/include/SqList.h @@ -0,0 +1,178 @@ +#pragma once +#include + +template +struct SqList // 顺序表 +{ + DT* elem; // 表首址 + int length; // 表长 + int size; // 表容量 +}; + +//算法2.1 +template +bool PriorElem_e(SqList
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 +bool InitList(SqList
& 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 +bool CreateList(SqList
& 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 +void DestroyList(SqList
& L) +{ + delete[] L.elem; // 1.释放表空间 + L.length = 0; // 2.属性赋值 + L.size = 0; +} + +//【算法2.5】 // 获取第i个元素值 +template +bool GetElem_i(SqList
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 +int LocateElem_e(SqList
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 +bool InsertElem_i(SqList
& 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 +bool DeleElem_i(SqList
& 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 +bool PutElem(SqList
& 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 +void ClearList(SqList
& L) +{ + L.length = 0; // 空表,表长为0 +} + + +// 测表长 +template +int ListLength(SqList
L) +{ + return L.length; +} + + +template +bool ListEmpty(SqList
L) // 测表空 +{ + if (L.length == 0) // 空表,返回true + return true; + else + return false; // 非空表,返回false +} + +template +bool ListFull(SqList
L) +{ + if (L.length == L.size) // 表满,返回true + return true; + else + return false; // 表不满,返回false +} + +//【算法2.10】 // 遍历输出 +template +void DispList(SqList
L) +{ + int i; + for (i = 1; i <= L.length; i++) // 依位序输出元素值 + { + cout << L.elem[i - 1] << "\t"; + + } + cout << endl; +} + + diff --git a/CardGame/include/SqQueue.h b/CardGame/include/SqQueue.h new file mode 100644 index 0000000..88c351c --- /dev/null +++ b/CardGame/include/SqQueue.h @@ -0,0 +1,102 @@ +#pragma once + +template +struct SqQueue +{ + DT* base; // 存储空间基地址 + int front; // 队头指针,指向队首元素 + int rear; // 队尾指针,指向队尾元素的后面 + int queuesize; // 队列容量 +}; + +template // 算法3.14 初始化队列 +bool InitQueue(SqQueue
& 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 // 算法3.15 销毁队列 +void DestroyQueue(SqQueue
& Q) // 销毁循环队列 +{ + delete[] Q.base; // 1. 释放循环队列占用的内存空间 + Q.front = Q.rear = 0; // 2. 为队列属性赋值 + Q.queuesize = 0; +} + +template // 算法3.16 入队 +bool EnQueue(SqQueue
& 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 // 算法3.17 出队 +bool DeQueue(SqQueue
& 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 // 算法3.18 取队头元素 +bool GetHead(SqQueue
Q, DT& e) // 取队头元素 +{ + if (Q.front == Q.rear) // 1. 队空的情况 + return false; // 无队头元素,返回false + e = Q.base[Q.front]; // 2. 取队头元素,赋值给e + return true; // 3. 返回true +} + +template +bool GetTail(SqQueue
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 +int QueueLength(SqQueue
Q) // 测表长 +{ + return (Q.rear - Q.front + Q.queuesize) % Q.queuesize; +} + +template +bool QueueEmpty(SqQueue
Q) // 判队空 +{ + return Q.front == Q.rear; +} + +template +bool QueueFull(SqQueue
Q) // 判队满 +{ + return (Q.rear + 1) % Q.queuesize == Q.front; +} + +template +void ClearQueue(SqQueue
& Q) // 清空队 +{ + Q.front = Q.rear = 0; +} + +template +void DispQueue(SqQueue
Q) // 显示队 +{ + int i = Q.front; + while (i != Q.rear) + { + cout << Q.base[i] << " "; + i = (i + 1) % Q.queuesize; + } + cout << endl; +} \ No newline at end of file diff --git a/CardGame/include/SqStack.h b/CardGame/include/SqStack.h new file mode 100644 index 0000000..273e06d --- /dev/null +++ b/CardGame/include/SqStack.h @@ -0,0 +1,109 @@ +#pragma once + +template +struct SqStack // 顺序栈 +{ + DT* base; // 栈首址 + int top; // 栈顶指针 + int stacksize; // 栈容量 +}; + +//基本操作的实现 +//【算法3.1】 // 初始化栈 +template +void InitStack(SqStack
& S, int m) +{ + S.base = new DT[m]; // 申请栈空间 + if (S.base == NULL) // 申请失败,退出 + { + cout << "未创建成功!"; + exit(1); + } + S.top = -1; // 设置空栈属性 + S.stacksize = m; +} + + +//【算法3.2】 // 销毁栈 +template +void DestroyStack(SqStack
& S) +{ + delete[] S.base; // 释放栈空间 + S.top = -1; + S.stacksize = 0; // 设置栈属性 +} + + +//【算法3.3】 // 入栈 +template +bool Push(SqStack
& 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 +bool Pop(SqStack
& S, DT& e) +{ + if (S.top == -1) // 栈空 + return false; // 返回false + e = S.base[S.top]; // 取栈顶元素 + S.top--; // 栈顶指针下移 + return true; // 出栈成功,返回true +} + + +//【算法3.5】 // 获取栈顶元素 +template +bool GetTop(SqStack
S, DT& e) +{ + if (S.top == -1) // 栈空 + return false; // 返回false + e = S.base[S.top]; // 栈非空,取栈顶元素 + return true; // 返回true +} + +//测栈空 +template +bool StackEmpty(SqStack
S) +{ + if (S.top == -1) // 空栈,返回true + return true; + else // 空栈,返回false + return false; +} + +//显示栈内容 +template +void DispStack(SqStack
S) +{ + int i = S.top; + while (i >= 0) + { + cout << S.base[i--] << "\t"; + } + cout << endl; +} + +template +void ClearStack(SqStack
& S) +{ + S.top = -1; + +} + +template +int StackFull(SqStack
S) +{ + if (S.top == S.stacksize - 1) + return 1; + else + return 0; + +} + +