井字游戏 c++ Agrid 不会重置

Tic Tac Toe c++ Agrid doesn't reset

本文关键字:Agrid 游戏 c++      更新时间:2023-10-16

我是一个初级程序员。我用 c++ 构建了一个井字游戏。游戏运行正常,直到提示用户重复。这就是问题所在。程序未正确循环。任何帮助将不胜感激。谢谢。

#include<iostream>
#include<string>
#include<cstdlib>
using namespace std;
char matrix[10] = {'0','1','2','3','4','5','6','7','8','9' };
void display();
int checkwin();
int restarter();
int main(){
    char repeat;
    do {

    string playername, player1, player2;
    int winner = 0;
    char mark = 0;
    int number = 0;
    int player = 1;
    char choice = 0;
    cout << "Player 1 please enter your name: ";
    getline(cin, player1);
    cout << "Player 2 please enter your name: ";
    getline(cin, player2);
    while (winner == 0)
    {
        display();
        if (player % 2)
        {
            playername = player1;
        }
        else
            playername = player2;
        cout << playername << " " << "Please choose a number you want" << endl;
        cin >> number;
        if (player % 2)
        {
            mark = 'X';
        }
        else
            mark = 'O';
        if (number == 1 && matrix[1] == '1')
        {
            matrix[1] = mark;
        }
        else if (number == 2 && matrix[2] == '2')
        {
            matrix[2] = mark;
        }
        else if (number == 3 && matrix[3] == '3')
        {
            matrix[3] = mark;
        }
        else if (number == 4 && matrix[4] == '4')
        {
            matrix[4] = mark;
        }
        else if (number == 5 && matrix[5] == '5')
        {
            matrix[5] = mark;
        }
        else if (number == 6 && matrix[6] == '6')
        {
            matrix[6] = mark;
        }
        else if (number == 7 && matrix[7] == '7')
        {
            matrix[7] = mark;
        }
        else if (number == 8 && matrix[8] == '8')
        {
            matrix[8] = mark;
        }
        else if (number == 9 && matrix[9] == '9')
        {
            matrix[9] = mark;
        }
        else
        {
            cout << "WRONG MOVE!";
            player--;
            cin.ignore();
            cin.get();
        }
        winner = checkwin();
        player++;
        display();
        if (winner == 1)
        {
            cout << playername << " " << "WON!" << endl;
        }
        else
            cout << "Its a draw!" << endl;
    }
    cout << "do u wana repeat?" << endl;
    cin >> repeat;
} while (repeat == 'Y');
    system("pause");
    return 0;
} 


void display()
{
    system("CLS");
    cout << "======= Welcome to You Tic and I Tac Your Toe =======" << endl;
    cout << "=======               Ivan                    =======" << endl;
    cout << "=======                &                      =======" << endl;
    cout << "=======              Mostafa                  =======" << endl;
    cout << "=====================================================" << endl;
    cout << "n" << endl;
    cout << "PLAYER 1 [X]    PLAYER 2 [O]" << endl;
    cout << "   |   |  " << endl;
    cout << " " << matrix[1] <<" | "<<matrix[2]<<" | "<< matrix[3] << endl;
    cout << "___|___|____" << endl;
    cout << "   |   |" << endl;
    cout << " " << matrix[4] <<" | "<<matrix[5]<<" | "<< matrix[6] << endl;
    cout << "___|___|____" << endl;
    cout << "   |   |" << endl;
    cout << " " << matrix[7] <<" | "<<matrix[8]<<" | " << matrix[9] << endl;
    cout << "   |   |" << endl;
}
int checkwin()
{
    if (matrix[1] == matrix[2] && matrix[2] == matrix[3])
    {
        return 1;
    }
    else if (matrix[4] == matrix[5] && matrix[5] == matrix[6])
    {
        return 1;
    }
    else if (matrix[7] == matrix[8] && matrix[8] == matrix[9])
    {
        return 1;
    }
    else if (matrix[1] == matrix[4] && matrix[4] == matrix[7])
    {
        return 1;
    }
    else if (matrix[2] == matrix[5] && matrix[5] == matrix[8])
    {
        return 1;
    }
    else if (matrix[3] == matrix[6] && matrix[6] == matrix[9])
    {
        return 1;
    }
    else if (matrix[2] == matrix[5] && matrix[5] == matrix[8])
    {
        return 1;
    }
    else if (matrix[3] == matrix[5] && matrix[5] == matrix[7])
    {
        return 1;
    }
    else if (matrix[1] == matrix[5] && matrix[5] == matrix[9])
    {
        return 1;
    }
    else if (matrix[1] != '1' && matrix[2] != '2' && matrix[3] != '3' && matrix[4] != '4' && matrix[5] != '5' && matrix[6] != '6' && matrix[7] != '7' && matrix[8] != '8' && matrix[9] != '9')
        return 2;
    return 0;
}

问题是你在赢得游戏后没有清除你的矩阵。矩阵是一个全局变量,因此它是在程序开始时创建的,并且在程序停止之前不会被销毁。循环时,将销毁在循环中创建的所有局部变量,但不会销毁全局变量。您需要手动清除此阵列。像这样的函数:

void clearMatrix()
{
        for(int i = 0; i<10; i++)
                matrix[i] = '0'+i;
}

如果在适当的位置执行矩阵,将清除矩阵。

int main(){
    char repeat;
    do {
    clearMatrix();
    string playername, player1, player2;
    int winner = 0;
    char mark = 0;
    ...
}

如果你在 do-while 循环开始时调用这个 clearMatrix(( 函数,你的矩阵每次都会被重置。

cout << "Player 1 please enter your name: ";
    cin >> player1;
    cout << "Player 2 please enter your name: ";
    cin >> player2;

请注意,您可以使用"cin"而不是"getline"来避免再次键入名称时出错。

还要这样做:

else
            cout << "Its a draw!" << endl;
    }
     clearMatrix();
    cout << "do u wanna repeat?" << endl;
    cin >> repeat;

}

由于您必须清除矩阵,因此按照上述定义在底部调用函数clearMatrix();,同时保留在 do-while 循环中是一种很好的编程习惯。