使用链表C++的抖音游戏
Tic-tac-toe game with Linked List C++
在我正在读的一本c++书中,我遇到了一个练习,建议我使用链表和数组来做一个井字游戏。我决定先尝试链表,因为数组显然更容易。然而,我一直纠结于如何检查是否有人赢得了比赛。到目前为止,我拥有的是:
struct board
{
bool square, circle, empty;
int pos;
board* next;
};
void startGame(int first, board* fullBoard);
board* getBoard(board* fullBoard);
int main()
{
int dice, first;
board* fullBoard = NULL;
cout << "Welcome to Tic-tac-toe DOS Game. (2 Player version)nn";
cout << "X is Player 1 and O is Player 2.nI will decide who is starting in the first match...n ";
srand(time(NULL));
dice = 1;//rand() % 6 + 1;
cout << dice;
if(dice <= 3)
{
first = 1;
cout << "Player 1 is the first!n";
}
else
{
first = 2;
cout << "Player 2 is the first!nn";
}
system("pause");
system("cls");
startGame(first, fullBoard);
}
void startGame(int first, board* fullBoard)
{
int choice;
bool isPlaying;
for(int i = 1; i <= 9; i++)
fullBoard = getBoard(fullBoard);
bool isGameOn = true;
while(isGameOn == true)
{
board* current = fullBoard;
while(current != NULL)
{
if(current->empty == true)
cout << " " << current->pos;
else if(current->circle == true)
cout << " " << "O";
else
cout << " " << "X";
if( current->pos == 4 || current->pos == 7)
{
cout << "n";
cout << "-----------------------n";
}
else if (current->pos == 1)
cout << "n";
else
cout << " |";
current = current->next;
}
if(first == 1)
{
isPlaying = true;
while(isPlaying == true)
{
cout << "Player 1, please put the number corresponding to the area you want to fill: ";
cin >> choice;
while(choice < 1 || choice > 9)
{
cout << "Invalid choice. Please choose a valid option: ";
cin >> choice;
}
current = fullBoard;
while(current != NULL && current->pos != choice)
current = current->next;
if(current->empty == true)
{
current->empty = false;
current->square = true;
isPlaying = false;
first = 2;
}
else
cout << "The field that you chose is already used...n";
}
}
else
{
isPlaying = true;
while(isPlaying == true)
{
cout << "Player 2, please put the number corresponding to the area you want to fill: ";
cin >> choice;
while(choice < 1 || choice > 9)
{
cout << "Invalid choice. Please choose a valid option: ";
cin >> choice;
}
current = fullBoard;
while(current != NULL && current->pos != choice)
current = current->next;
if(current->empty == true)
{
current->empty = false;
current->circle = true;
isPlaying = false;
first = 1;
}
else
cout << "The field that you chose is already used...n";
}
}
system("cls");
}
}
board* getBoard(board* fullBoard)
{
board* newBoard = new board;
newBoard->empty = true;
newBoard->circle = false;
newBoard->square = false;
newBoard->next = fullBoard;
if(newBoard->next != NULL)
newBoard->pos = newBoard->next->pos + 1;
else
newBoard->pos = 1;
return newBoard;
}
正如你所看到的,在我的结构Board上,我有一个名为pos的int,我创建它是为了跟踪整个Board。到目前为止,我能想到的唯一解决方案是检查每个位置。例如:将pos 8与pos 9、7、5和2进行比较,将pos 9与pos 8、7、6、3、5和1进行比较。但我认为这太宽泛了(也许它也很难编码?)。你认为我还有什么选择?
提前感谢,Felipe
我需要更多的空间来解释"多个列表"的概念。
您的董事会:
_ _ _
|_|_|_|
|_|_|_|
|_|_|_|
代表可能解决方案的列表
D1 A B C D2
_ _ _
E |_|_|_|
F |_|_|_|
G |_|_|_|
请注意,每个单元格将至少在2个列表中。
选项1)
您将这些列表存储在单个"列表列表"中。当移动完成时,您迭代该列表列表,对于每个子列表(上图中的a、B等),您迭代单元格。如果子列表中的所有单元格都来自移动的玩家,则您找到了赢家。
选项2)
每个单元格都有一个成员,该成员是"列表列表"。该"列表列表"包含在该单元格中移动时要检查的列表(例如,对于单元格1,您将有列表a、E和D1)。当在一个单元格中移动时,您可以从该单元格中获取该列表,并检查子列表以查看是否获得了赢家(这与选项1大致相同,但限制了每次必须迭代的列表)。
请注意,在所有情况下,我们只处理列表(如果添加"指向对角线的指针"和类似的内容,则结构不再是列表)。仅此而已:
- 有很多列表
- 有单元格列表和单元格列表
相关文章:
- 在C++游戏中与库存系统作斗争
- 换位表导致测试失败(但在游戏中运行良好)
- Craps游戏问题,忽略if语句
- 矢量下标超出SFML游戏中的范围
- 我是 c++ 的新手.学习基本知识后,我想做井字游戏.对于印刷板,我在下面写代码,但它显示错误
- 我在贪吃蛇游戏中收到了错误代码 -1073741571
- 我应该在简单的策略游戏中为各个派系使用类吗 - C++
- 游戏体验和升级
- 我的代码运行良好,但在游戏循环中中断
- 如何使用 SFML 在贪吃蛇游戏中定义游戏结束?
- 我可以使用任何好的逻辑来阻止计算机将 O 放在井字游戏中的现有 X 上
- 将鼠标和键盘输入发送到 unity3d 游戏 (Rust)
- 在猜谜游戏 c++ 中不计算尝试次数
- 在C++不适用于猜数字游戏的情况下再次播放选项
- 在为我的基于文本的 RPG 游戏制作库存时遇到困难
- 在定义字符数组(井字游戏)的 for 循环中应用输入限制
- Frank Luna 在他的书"使用 DirectX12 进行 3D 游戏编程"的介绍中盒子示例的问题
- 如何在Linux上正确发布C++软件(游戏)
- 提高基于组件的游戏引擎的效率
- 文本冒险游戏 - 如何区分一种项目类型与另一种项目类型以及如何构建项目类/子类