带有链表C++的TC TAC游戏 在我正在阅读的C++书籍中,我遇到了一个练习,它建议我使用链表和数组来做一个TIC Tac趾游戏。我决定先尝试使用链表,因为使用数组显然更容易。然而,我被困在如何检查是否有人赢了这场比赛。以下是我目前掌握的情况: 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)\n\n"; 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!\n\n"; } 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; }

带有链表C++的TC TAC游戏 在我正在阅读的C++书籍中,我遇到了一个练习,它建议我使用链表和数组来做一个TIC Tac趾游戏。我决定先尝试使用链表,因为使用数组显然更容易。然而,我被困在如何检查是否有人赢了这场比赛。以下是我目前掌握的情况: 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)\n\n"; 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!\n\n"; } 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; },c++,tic-tac-toe,C++,Tic Tac Toe,如您所见,在我的结构板上,我有一个名为pos的int,我创建它是为了跟踪整个板。到目前为止,我能想到的唯一解决办法是检查每个位置。例:将位置8与位置9、7、5和2进行比较,将位置9与位置8、7、6、3、5和1进行比较。但我认为这太广泛了,可能也是硬编码?。你认为我还有别的选择吗 提前感谢,, Felipe我需要更多的空间来解释多重列表的概念 董事会: _ _ _ |_|_|_| |_|_|_| |_|_|_| 代表可能解决方案的列表 D1 A B C D2 _ _ _ E |_

如您所见,在我的结构板上,我有一个名为pos的int,我创建它是为了跟踪整个板。到目前为止,我能想到的唯一解决办法是检查每个位置。例:将位置8与位置9、7、5和2进行比较,将位置9与位置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大致相同,但限制了您每次必须迭代的列表

请注意,在所有情况下,如果您将指针添加到对角线,那么我们只处理列表,类似的结构不再是列表。只是:

有许多清单。 有单元格列表,也有单元格列表。
首先,你只需要比较可能的解决方案,包括最后一个改变的位置如果你把X放在位置8,你不需要检查[1,2,3],我不知道它是否符合实践的想法,但是你不能有多个链表吗?如果电路板的每个位置都包含在一个表示其所在行/列/对角线的链表中,那么只需检查所有列表就可能更简单,但你不必移动它们。但那样的话,我还需要更多的指针来存储列和对角线右侧的下一个和上一个位置?你的确切意思是什么:如果你将指针添加到对角线和类似的位置,结构就不再是列表了?我认为链表需要指针,就像我在struct中使用的一样:struct board{bool square,circle,empty;int pos;board*next;};getDiagonalCell不是列表中定义的操作。如果你想用一个列表来实现游戏,你可以使用为链表定义的操作,比如firstElement,next,add和delete。您可以声明并实现一个函数goToCellXY,但这不是列表契约的一部分。哦!我理解!我想我有足够的信息来完成这个游戏了。但我仍然认为使用数组更容易、更快,我认为这是练习的一部分。。无论如何,你帮了我很多忙!谢谢