Search code examples
c++tic-tac-toe

How i can reset the values of my Tic Tac Toe Game


Hello i'm having problems on my program. Programming is new to me and i don't know where i will reset the values of the matrix because declaration of its values is in global.To understand more i have a comment below wherein if the game is already finish the program will ask the user if he or she wants to run the game again. Is there anyway how to fix this by adding some code or modifications to the given code. I tried to use the function goto Start; inside the if condition and in the main function there is a Start: written. but it didnt work. Please help me thanks in advance.

#include <iostream>
using namespace std;
char matrix[3][3] = { '1', '2', '3', '4', '5', '6', '7', '8', '9' };
char player = 'X';
int rear; 


void Draw()
{
    system("cls");
    cout << "\n\n\tTic Tac Toe Game \n\n";
    for (int i = 0; i < 3; i++)
    {
        cout<<" \t     "; 
        for (int j = 0; j < 3; j++)
        {
            cout << matrix[i][j] << " ";
        }
        cout << endl;
    }
}

void Input()
{
    int a;
    ++rear; 
    cout << "\n    Press the number of the field: ";
    cin >> a;

    if (a == 1)
        matrix[0][0] = player;
    else if (a == 2)
        matrix[0][1] = player;
    else if (a == 3)
        matrix[0][2] = player;
    else if (a == 4)
        matrix[1][0] = player;
    else if (a == 5)
        matrix[1][1] = player;
    else if (a == 6)
        matrix[1][2] = player;
    else if (a == 7)
        matrix[2][0] = player;
    else if (a == 8)
        matrix[2][1] = player;
    else if (a == 9)
        matrix[2][2] = player;
    else {
    cout<<"\n [ERROR: Invalid Range of Input is only 1-9 ]\n\n "; 
    system("pause"); 
    rear--; 
    }
}

void TogglePlayer()
{
    if (player == 'X')
        player = 'O';
    else
        player = 'X';
}
char Win()
{
    //first player
    if (matrix[0][0] == 'X' && matrix[0][1] == 'X' && matrix[0][2] == 'X')
        return 'X';
    if (matrix[1][0] == 'X' && matrix[1][1] == 'X' && matrix[1][2] == 'X')
        return 'X';
    if (matrix[2][0] == 'X' && matrix[2][1] == 'X' && matrix[2][2] == 'X')
        return 'X';

    if (matrix[0][0] == 'X' && matrix[1][0] == 'X' && matrix[2][0] == 'X')
        return 'X';
    if (matrix[0][1] == 'X' && matrix[1][1] == 'X' && matrix[2][1] == 'X')
        return 'X';
    if (matrix[0][2] == 'X' && matrix[1][2] == 'X' && matrix[2][2] == 'X')
        return 'X';

    if (matrix[0][0] == 'X' && matrix[1][1] == 'X' && matrix[2][2] == 'X')
        return 'X';
    if (matrix[2][0] == 'X' && matrix[1][1] == 'X' && matrix[0][2] == 'X')
        return 'X';

    //second player
    if (matrix[0][0] == 'O' && matrix[0][1] == 'O' && matrix[0][2] == 'O')
        return 'O';
    if (matrix[1][0] == 'O' && matrix[1][1] == 'O' && matrix[1][2] == 'O')
        return 'O';
    if (matrix[2][0] == 'O' && matrix[2][1] == 'O' && matrix[2][2] == 'O')
        return 'O';

    if (matrix[0][0] == 'O' && matrix[1][0] == 'O' && matrix[2][0] == 'O')
        return 'O';
    if (matrix[0][1] == 'O' && matrix[1][1] == 'O' && matrix[2][1] == 'O')
        return 'O';
    if (matrix[0][2] == 'O' && matrix[1][2] == 'O' && matrix[2][2] == 'O')
        return 'O';

    if (matrix[0][0] == 'O' && matrix[1][1] == 'O' && matrix[2][2] == 'O')
        return 'O';
    if (matrix[2][0] == 'O' && matrix[1][1] == 'O' && matrix[0][2] == 'O')
        return 'O';

    return '/';
}

int main()
{
    char choice; 
    Draw();
    Start: 
    while (1)
    {
        Input();
        Draw();

        if (Win() == 'X')
        {
            cout << "\n    [Message: Player 'X' Wins The Game]  ";
            break;
        }
        else if (Win() == 'O')
        {
            cout << "\n    [Message: Player 'O' Wins The Game ]" << endl;
            break;
        }
        else if(rear==9)
        {
            cout<<" Draw"<<endl; 
            break; 
        }
        TogglePlayer();
    }

    cout<<"\n\n   ";
    cout<<"Do you want to run the game again [Y/N]? ";
    cin>>choice;
    if(choice=='Y' || choice=='y')
    {
        // i don't know where i can reset the values again; 

    }
    else 
    {

    }
    return 0;
}

Solution

  • You have to reset your game variables. I think your game variables reset values are:

    int rear = 0;
    player = 'X';
    char matrix[3][3] = { '1', '2', '3', '4', '5', '6', '7', '8', '9' };
    

    While you can reset rear and player just by assigning, matrix will have to be "regenerated". One way to regerate matrix is (for more information, see how to assign an array from an initializer list):

        for (int i=0; i<3; i++) {
            for (int j=0; j<3; j++) {
                matrix[i][j] = i*3+(j+1) + 48;
            }
        }
    

    And finally, you have to go back to the beginning of you main function. You already put a Start: label here, so I'm assuming you were maybe trying to use goto? The label needs to be moved before Draw(). So here's your reset code:

    if(choice=='Y' || choice=='y')
    {
        // i don't know where i can restart the values again; 
        for (int i=0; i<3; i++) {
            for (int j=0; j<3; j++) {
                matrix[i][j] = i*3+(j+1) + 48;
            }
        }
        rear = 0;
        player = 'X';
        goto Start;
    }
    

    Some problems:

    • You shouldn't rely on goto, though in this case it's still all right.
    • system("cls") doesn't work on Linux