project rebuild, add test code, fix memory leak, etc
This commit is contained in:
58
include/Functions.h
Normal file
58
include/Functions.h
Normal file
@@ -0,0 +1,58 @@
|
||||
#pragma once
|
||||
#include <cstdlib>
|
||||
#include <ctime>
|
||||
#include "SqList.h"
|
||||
#include "SqStack.h"
|
||||
|
||||
// 发牌
|
||||
inline 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; // 释放发牌堆占用的内存
|
||||
}
|
||||
|
||||
// 寻找栈内元素并返回其位置
|
||||
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;
|
||||
}
|
||||
|
||||
// 返回栈元素数量
|
||||
inline int StackSize(SqStack<int>& S)
|
||||
{
|
||||
return S.top + 1;
|
||||
}
|
||||
Reference in New Issue
Block a user