簡體   English   中英

帶有鏈接列表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)\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;


}

如您所見,在我的結構板上,我有一個名為pos的int,它是為了跟蹤整個板而創建的。 到目前為止,我唯一能想到的解決方案就是檢查每個位置。 例如:比較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大致相同,但限制了您必須遍歷每個列表的列表時間)。

請注意,在所有情況下,我們僅處理列表(如果您將“對角線指針添加”,類似的結構將不再是列表)。 只有這樣:

  • 有很多清單。
  • 有單元格列表和單元格列表。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM