简体   繁体   中英

Restart Program So My C++ game is replayable

I am writing a c++ program for a tic tac toe game. The game must have a game counter for the number of wins each player has as long as the number of ties. The issue I am having is that I can't seem to get the board to clear, so when it reaches the end of one game, the board clears the X's and O's. Any help I can get, I would appreciate greatly!


#include <iostream>

using namespace std;

void display_board();
void player_turn();
bool gameover();
int xwins, owins, ties;

char turn;
bool draw = false;
char board[3][3] = {{'1', '2', '3'}, {'4', '5', '6'}, {'7', '8', '9'}};

int main()
{
    cout << "Tic Tac Toe Game\n";
    cout << "Player 1 [X] --- Player 2 [O]\n";
    turn = 'X';
    char board[3][3] = {{'1', '2', '3'}, {'4', '5', '6'}, {'7', '8', '9'}};

    while (!gameover())
    {
        display_board();
        player_turn();
        gameover();
    }

    if (turn == 'O' && !draw)
    {
        display_board();
        cout << endl << endl << "Player 1 [X] Wins! Game Over!\n";
        xwins++;
    }
    else if (turn == 'X' && !draw)
    {
        display_board();
        cout << endl << endl << "Player 2 [O] Wins! Game Over!\n";
        owins++;
    }
    else
    {
        display_board();
        cout << endl << endl << "It's a draw! Game Over!\n";
        ties++;
    }


}

void display_board()
{
    cout << "---------------------" << endl << endl;
    cout << "     |     |     " << endl;
    cout << "  " << board[0][0] << "  |  " << board[0][1] << "  |  " << board[0][2] <<"      Player 1 wins "<<xwins<< endl;
    cout << "_____|_____|_____" <<endl;
    cout << "     |     |     " << endl;
    cout << "  " << board[1][0] << "  |  " << board[1][1] << "  |  " << board[1][2] <<"      Player 2 wins "<<owins<< endl;
    cout << "_____|_____|_____" << endl;
    cout << "     |     |     " << endl;
    cout << "  " << board[2][0] << "  |  " << board[2][1] << "  |  " << board[2][2] <<"      Ties "<<ties<< endl;
    cout << "     |     |     " << endl;
}

void player_turn()
{
    int choice;
    int row = 0, column = 0;

    if (turn == 'X')
    {
        cout << "Player 1 turn [X]: ";
    }
    else if (turn == 'O')
    {
        cout << "Player 2 turn [O]: ";
    }
    cin >> choice;

    switch (choice)
    {
    case 1: row = 0; column = 0; break;
    case 2: row = 0; column = 1; break;
    case 3: row = 0; column = 2; break;
    case 4: row = 1; column = 0; break;
    case 5: row = 1; column = 1; break;
    case 6: row = 1; column = 2; break;
    case 7: row = 2; column = 0; break;
    case 8: row = 2; column = 1; break;
    case 9: row = 2; column = 2; break;
    default:
        cout << "You didn't enter a correct number! Try again\n";
        player_turn();
    }

    if (turn == 'X' && board[row][column] != 'X' && board[row][column] != 'O')
    {
        board[row][column] = 'X';
        turn = 'O';
    }
    else if (turn == 'O' && board[row][column] != 'X' && board[row][column] != 'O')
    {
        board[row][column] = 'O';
        turn = 'X';
    }
    else
    {
        cout << "The cell you chose is used! Try again\n";
        player_turn();
    }

}

bool gameover()
{
    for (int i = 0; i < 3; i++)//Check for a win
    {
        if ((board[i][0] == board[i][1] && board[i][1] == board[i][2]) || (board[0][i] == board[1][i] && board[1][i] == board[2][i]) || (board[0][0] == board[1][1] && board[1][1] == board[2][2]) || (board[0][2] == board[1][1] && board[1][1] == board[2][0]))
        {
            return true;
        }
    }

    for (int i = 0; i < 3; i++)//Check for draw
    {
        for (int j = 0; j < 3; j++)
        {
            if (board[i][j] != 'X' && board[i][j] != 'O')
            {
                return false;
            }
        }
    }
    draw = true;
    return true;
}

I can see a couple of issues with your code :

while (!gameover())
{
    display_board();
    player_turn();
    gameover();
}

You do not need the gameover() inside the loop. You are already checking for it in the condition.

Also, you are not resetting your global variables after each instance of the game. Add another while / do- while loop which runs until a particular condition. Within this loop, add your main functionality. And reset your global variables (draw, board and turn ) for each instance of the loop.

Try This

do{
    cin >> choice;

    switch (choice)
    {
        case 1: row = 0; column = 0; break;
        case 2: row = 0; column = 1; break;
        case 3: row = 0; column = 2; break;
        case 4: row = 1; column = 0; break;
        case 5: row = 1; column = 1; break;
        case 6: row = 1; column = 2; break;
        case 7: row = 2; column = 0; break;
        case 8: row = 2; column = 1; break;
        case 9: row = 2; column = 2; break;
        default:
        cout << "You didn't enter a correct number! Try again\n";
    }
}
while(choice<1 || choice>9);

because if user enter any other value its calling player_turn(); again so that it will work recursively (Calling same function inside function).

No need to call gameover(); again inside the loop as you are checking while(!gameover()) .

After match is draw you need to set value if array to 1,2,3... and call funtion display_board(); .

If I have understood your problem correctly then there are 2 problems:

1) You need to find a place for win and tie counter.

bool gameover()
{
    for (int i = 0; i < 3; i++)//Check for a win
    {
        bool row_match    = (board[i][0] == board[i][1] && board[i][1] == board[i][2]);
        bool column_match = (board[0][i] == board[1][i] && board[1][i] == board[2][i]);
        bool diagonal_match = (board[0][2] == board[1][1] && board[1][1] == board[2][0]) || (board[0][0] == board[1][1] && board[1][1] == board[2][2]);

        if (row_match || column_match || diagonal_match)
        {
            bool x_wins = (row_match && board[i][0] == "X") || (column_match && board[0][i] == "X") || (diagonal_match && board[1][1] == "X")

            if (x_wins) xwins++;
            else owins++;
            return true;
        }
    }

    for (int i = 0; i < 3; i++)//Check for draw
    {
        for (int j = 0; j < 3; j++)
        {
            if (board[i][j] != 'X' && board[i][j] != 'O')
            {
                return false;
            }
        }
    }

    ties++;
    draw = true;
    return true;
}

2) You need to clear the board.

do
{
    display_board();
    player_turn();
} while (!gameover);
clearboard();

void clearboard()
{
    for (int row = 0; row < 3; row++)
    {
        for (int col = 0; col < 3; col++)
        {
            int val = 3 ** row + col + 1;
            board[row][col] = itoa (val);
        }
    }
}

Please let me know if does not solve your problem or if I have misunderstood your problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM