使用链表C++的抖音游戏

Tic-tac-toe game with Linked List C++

本文关键字:游戏 C++ 链表      更新时间:2023-10-16

在我正在读的一本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大致相同,但限制了每次必须迭代的列表)。

请注意,在所有情况下,我们只处理列表(如果添加"指向对角线的指针"和类似的内容,则结构不再是列表)。仅此而已:

  • 有很多列表
  • 有单元格列表和单元格列表