Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

please pleas help me : ( Write a C++ program that plays the tic-tac-toe game 1D

ID: 3582670 • Letter: P

Question

please pleas help me : (

Write a C++ program that plays the tic-tac-toe game 1D ARRAY single player vs computer . It starts by displaying the board and numbering the cells from 1 to 9 as shown below

| 1 | 2 | 3 |

| 4 | 5 | 6 |

| 7 | 8 | 9 |

Enter you selection: 5

The user selection should be a number between 1 and 9.

Your program should check and mark the user selection using the X character.

The program should then make its move by choosing one of the empty spots and marking it with the O character. After the first move, the board should be displayed as shown below,

assuming that the program has randomly selected 3 as its move.

| 1 | 2 | O |

| 4 | X | 6 |

| 7 | 8 | 9 |

Enter you selection: 3 Sorry, 3 is used Enter your selection: 1

| X | 2 | O |

| O | X | 6 |

| 7 | 8 | 9 |

Enter your selection: 9

| X | 2 | O |

| 4 | X | 6 |

| 7 | 8 | X |

- You Win! Congratulations Press to exit..

Your program should be able to check whether a user selection is valid or not. It should also determine if there is a winner, or there is a draw (nobody won). The program asks the user if he wants to play again. If the user answers ‘yes’, the program displays a new board and restarts the game. Implement the program using a 1 dimensional array of 9 characters to keep track of the state of the board. (Do *not* use 2D arrays.) Initially, the array is initialized from ‘1’ to ‘9’. The user’s selection is stored as ‘X’; and the program’s selection is stored as ‘O’. In addition to delaying input/output to the screen, also save it to a file (the whole game trace). Your program should follow the concepts of modular programming. You will need to implement all functionalities into separate modules (functions). Your main() function will call and coordinate all the functions you implement. You will at least need functions to do the following: 1. Display the board using the state information available in the array of 9 characters. You must use a *loop* to display the board (function display()). 2. Check whether s move is valid or not (validate()) 3. Check for a sequence of ‘X’ or ‘O’ in any row, column, or diagonal, to detect if the user or program has won (check_win()). 4. Check weather to terminate the program because there are no more moves possible (full_board()) 5. Generate a new move randomly; however, your program must as well make smart moves (next_move()) to try to defend and win. 6. Saves the whole game trace (input and output) into a file (save())

Explanation / Answer

Here goes the required code of the tic-tac-toe program for player vs computer.

This is a running code so you can compile it and execute it directly.

Code:

#include <iostream>
#include <ctime>
#include <cstdio>
#include <cstdlib>

using namespace std;

int computerRandomPick; //random location which computer selects if first.
int computerPick; //used to decide computer vs player AI moves.
//player interaction
int playerChoice;
bool playerTurn;
int gameWin = 3;
//gameWin is integer because it will allow program to explicitly say who wins instead of bool which will allow you to only declare game state.

//board arrays
char blockOne = '1';
char blockTwo = '2';
char blockThree = '3';
char blockFour = '4';
char blockFive = '5';
char blockSix = '6';
char blockSeven = '7';
char blockEight = '8';
char blockNine = '9';
//random starting turn chooser
int turnFirst; //variable to decide whoever goes first

int checkWinComputer()
{
    if(blockOne == 'O' && blockFive == 'O' && blockNine == 'O' && playerTurn == false) //diagonal, 1 - 5 = 9
        gameWin = 2; //This will make computer win.
    if(blockThree == 'O' && blockFive == 'O' && blockSeven == 'O' && playerTurn == false) //diagonal, 3 - 5 = 7
        gameWin = 2;
    if(blockOne == 'O' && blockTwo == 'O' && blockThree == 'O' && playerTurn == false) //horizontal 1 - 2 = 3
        gameWin = 2;
    if(blockFour == 'O' && blockFive == 'O' && blockSix == 'O' && playerTurn == false) //horizontal 4 - 5 = 6
        gameWin = 2;
    if(blockSeven == 'O' && blockEight == 'O' && blockNine == 'O' && playerTurn == false) //horizontal 7 - 8 = 9
        gameWin = 2;
    if(blockOne == 'O' && blockFour == 'O' && blockSeven == 'O' && playerTurn == false) //vertical 1 - 4 = 7
        gameWin = 2;
    if(blockTwo == 'O' && blockFive == 'O' && blockEight == 'O' && playerTurn == false) //vertical 2 - 5 = 8
        gameWin = 2;
    if(blockThree == 'O' && blockSix == 'O' && blockNine == 'O' && playerTurn == false) //vertical 3 - 6 = 9
        gameWin = 2;
}
int checkWinPlayer()
{
    if(blockOne == 'X' && blockFive == 'X' && blockNine == 'X' && playerTurn == true) //diagonal, 1 - 5 = 9
        gameWin = 1; //This will make player win.
    if(blockThree == 'X' && blockFive == 'X' && blockSeven == 'X' && playerTurn == true) //diagonal, 3 - 5 = 7
        gameWin = 1;
    if(blockOne == 'X' && blockTwo == 'X' && blockThree == 'X' && playerTurn == true) //horizontal 1 - 2 = 3
        gameWin = 1;
    if(blockFour == 'X' && blockFive == 'X' && blockSix == 'X' && playerTurn == true) //horizontal 4 - 5 = 6
        gameWin = 1;
    if(blockSeven == 'X' && blockEight == 'X' && blockNine == 'X' && playerTurn == true) //horizontal 7 - 8 = 9
        gameWin = 1;
    if(blockOne == 'X' && blockFour == 'X' && blockSeven == 'X' && playerTurn == true) //vertical 1 - 4 = 7
        gameWin = 1;
    if(blockTwo == 'X' && blockFive == 'X' && blockEight == 'X' && playerTurn == true) //vertical 2 - 5 = 8
        gameWin = 1;
    if(blockThree == 'X' && blockSix == 'X' && blockNine == 'X' && playerTurn == true) //vertical 3 - 6 = 9
        gameWin = 1;
}
int computerAI() //work on computerAI part where the computer tries to win before countering.
{
    playerTurn == false;

    if(blockOne == 'O' && blockTwo == 'O' && playerTurn == false && blockThree == '3') //1 - 2 = 3 win
    {
        blockThree = 'O';
        playerTurn = true;
    }

    if(blockFour == 'O' && blockFive == 'O' && playerTurn == false && blockSix == '6') //4 - 5 = 6 win
    {
        blockSix = 'O';
        playerTurn = true;
    }

    if(blockSeven == 'O' && blockEight == 'O' && playerTurn == false && blockNine == '9') //7 - 8 = 9 win
    {
        blockNine = 'O';
        playerTurn = true;
    }

    if(blockOne == 'O' && blockFour == 'O' && playerTurn == false && blockSeven == '7') //1 - 4 = 7 win
    {
        blockSeven = 'O';
        playerTurn = true;
    }

    if(blockTwo == 'O' && blockFive == 'O' && playerTurn == false && blockEight == '8') //2 - 5 = 8 win
    {
        blockEight = 'O';
        playerTurn = true;
    }

    if(blockThree == 'O' && blockSix == 'O' && playerTurn == false && blockNine == '9') //3 - 6 = 9 win
    {
        blockNine = 'O';
        playerTurn = true;
    }

    if(blockOne == 'O' && blockFive == 'O' && playerTurn == false && blockNine == '9') //1 - 5 = 9 win
    {
        blockNine = 'O';
        playerTurn = true;
    }

    if(blockThree == 'O' && blockFive == 'O' && playerTurn == false && blockSeven == '7') //3 - 5 = 7 win
    {
        blockSeven = 'O';
        playerTurn == true;
    }

    if((playerChoice == 1 || playerChoice == 5 || playerChoice == 9) && playerTurn == false)
    {
        if((blockOne == 'X' && blockFive == 'X') && playerTurn == false && blockNine == '9') //1 - 5 = 9 diagonal
        {
            blockNine = 'O';
            playerTurn = true;
        }

        if((blockOne == 'X' && blockNine == 'X') && playerTurn == false && blockFive == '5') //1 - 9 = 5 diagonal
        {
            blockFive = 'O';
            playerTurn = true;
        }

        if((blockFive == 'X' && blockNine == 'X') && playerTurn == false && blockOne == '1') //5 - 9 = 1 diagonal
        {
            blockOne = 'O';
            playerTurn = true;
        }
    }

    if((playerChoice == 3 || playerChoice == 5 || playerChoice == 7) && playerTurn == false)
    {
        if((blockSeven == 'X' && blockFive == 'X') && playerTurn == false && blockThree == '3') //7 - 5 = 3 diagonal
        {
            blockThree = 'O';
            playerTurn = true;
        }

        if((blockSeven == 'X' && blockThree == 'X') && playerTurn == false && blockFive == '5') //7 - 3 = 5 diagonal
        {
            blockFive = 'O';
            playerTurn = true;
        }

        if((blockFive == 'X' && blockThree == 'X') && playerTurn == false && blockSeven == '7') //5 - 3 = 7 diagonal
        {
            blockSeven = 'O';
            playerTurn = true;
        }
    }

    if((playerChoice == 1 || playerChoice == 2 || playerChoice == 3) && playerTurn == false)
    {
        if((blockOne == 'X' && blockTwo == 'X') && playerTurn == false && blockThree == '3') // 1 - 2 = 3 horizontal
        {
            blockThree = 'O';
            playerTurn = true;
        }

        if((blockOne == 'X' && blockThree == 'X') && playerTurn == false && blockTwo == '2') //1 - 3 = 2 horizontal
        {
            blockTwo = 'O';
            playerTurn = true;
        }

        if((blockTwo == 'X' && blockThree == 'X') && playerTurn == false && blockOne == '1') //2 - 3 = 1 horizontal
        {
            blockOne = 'O';
            playerTurn = true;
        }
    }

    if((playerChoice == 4 || playerChoice == 5 || playerChoice == 6) && playerTurn == false)
    {
        if((blockFour == 'X' && blockFive == 'X') && playerTurn == false && blockSix == '6') //4 - 5 = 6 horizontal line 2
        {
            blockSix = 'O';
            playerTurn = true;
        }

        if((blockFour == 'X' && blockSix == 'X') && playerTurn == false && blockFive == '5') //4 - 6 = 5 horizontal line 2
        {
            blockFive = 'O';
            playerTurn = true;
        }

        if((blockFive == 'X' && blockSix == 'X') && playerTurn == false && blockFour == '4') //5 - 6 = 4 horizontal line 2
        {
            blockFour = 'O';
            playerTurn = true;
        }
    }

    if((playerChoice == 7 || playerChoice == 8 || playerChoice == 9) && playerTurn == false)
    {
        if((blockSeven == 'X' && blockEight == 'X') && playerTurn == false && blockNine == '9') //7 - 8 = 9 horizontal line 3
        {
            blockNine = 'O';
            playerTurn = true;
        }

        if((blockSeven == 'X' && blockNine == 'X') && playerTurn == false && blockEight == '8') //7 - 9 = 8 horizontal line 3
        {
            blockEight = 'O';
            playerTurn = true;
        }

        if((blockEight == 'X' && blockNine == 'X') && playerTurn == false && blockSeven == '7') //8 - 9 = 7 horizontal line 3
        {
            blockSeven = 'O';
            playerTurn = true;
        }
    }

    if((playerChoice == 1 || playerChoice == 4 || playerChoice == 7) && playerTurn == false)
    {
        if((blockOne == 'X' && blockFour == 'X') && playerTurn == false && blockSeven == '7') //1 - 4 = 7 vertical row 1
        {
            blockSeven = 'O';
            playerTurn = true;
        }

        if((blockOne == 'X' && blockSeven == 'X') && playerTurn == false && blockFour == '4') //1 - 7 = 4 vertical row 1
        {
            blockFour = 'O';
            playerTurn = true;
        }

        if((blockFour == 'X' && blockSeven == 'X') && playerTurn == false && blockOne == '1') //4 - 7 = 1 vertical row 1
        {
            blockOne = 'O';
            playerTurn = true;
        }

    }

    if((playerChoice == 2 || playerChoice == 5 || playerChoice == 8) && playerTurn == false)
    {
        if((blockTwo == 'X' && blockFive == 'X') && playerTurn == false && blockEight == '8') //2 - 5 = 8 vertical row 2
        {
            blockEight = 'O';
            playerTurn = true;
        }

        if((blockTwo == 'X' && blockEight == 'X') && playerTurn == false && blockFive == '5') //2 - 8 = 5 vertical row 2
        {
            blockFive = 'O';
            playerTurn = true;
        }

        if((blockFive == 'X' && blockEight == 'X') && playerTurn == false && blockTwo == '2') //5 - 8 = 2 vertical row 2
        {
            blockTwo = 'O';
            playerTurn = true;
        }
    }

    if((playerChoice == 3 || playerChoice == 6 || playerChoice == 9) && playerTurn == false)
    {
        if((blockThree == 'X' && blockSix == 'X') && playerTurn == false && blockNine == '9') //3 - 6 = 9 vertical row 3
        {
            blockNine = 'O';
            playerTurn = true;
        }

        if((blockThree == 'X' && blockNine == 'X') && playerTurn == false && blockSix == '6') //3 - 9 = 6 vertical row 3
        {
            blockSix = 'O';
            playerTurn = true;
        }

        if((blockSix == 'X' && blockNine == 'X') && playerTurn == false && blockThree == '3') //6 - 9 = 3 vertical row 3
        {
            blockThree = 'O';
            playerTurn = true;
        }
    }
    else
    {
        do
        {

            if(blockOne == '1' && playerTurn == false)
            {
                playerTurn = true;
                blockOne = 'O';
            }
            if(blockTwo == '2' && playerTurn == false)
            {
                playerTurn = true;
                blockTwo = 'O';
            }
            if(blockThree == '3' && playerTurn == false)
            {
                playerTurn = true;
                blockThree = 'O';
            }
            if(blockFour == '4' && playerTurn == false)
            {
                playerTurn = true;
                blockFour = 'O';
            }
            if(blockFive == '5' && playerTurn == false)
            {
                playerTurn = true;
                blockFive = 'O';
            }
            if(blockSix == '6' && playerTurn == false)
            {
                playerTurn = true;
                blockSix = 'O';
            }
            if(blockSeven == '7' && playerTurn == false)
            {
                playerTurn = true;
                blockSeven = 'O';
            }
            if(blockEight == '8' && playerTurn == false)
            {
                playerTurn = true;
                blockEight = 'O';
            }
            if(blockNine == '9' && playerTurn == false)
            {
                playerTurn = true;
                blockNine = 'O';
            }
        }
        while(playerTurn = false);
    }
    return 0;
}

int checkPlayerInput()
{
    if(playerChoice == 1 && blockOne == '1')
        blockOne = 'X';
    if(playerChoice == 2 && blockTwo == '2')
        blockTwo = 'X';
    if(playerChoice == 3 && blockThree == '3')
        blockThree = 'X';
    if(playerChoice == 4 && blockFour == '4')
        blockFour = 'X';
    if(playerChoice == 5 && blockFive == '5')
        blockFive = 'X';
    if(playerChoice == 6 && blockSix == '6')
        blockSix = 'X';
    if(playerChoice == 7 && blockSeven == '7')
        blockSeven = 'X';
    if(playerChoice == 8 && blockEight == '8')
        blockEight = 'X';
    if(playerChoice == 9 && blockNine == '9')
        blockNine = 'X';


    return 0;
}

int checkComputerInput()
{
    if(computerPick == 1 && blockOne == '1')
        blockOne = 'O';
    if(computerPick == 2 && blockTwo == '2')
        blockTwo = 'O';
    if(computerPick == 3 && blockThree == '3')
        blockThree = 'O';
    if(computerPick == 4 && blockFour == '4')
        blockFour = 'O';
    if(computerPick == 5 && blockFive == '5')
        blockFive = 'O';
    if(computerPick == 6 && blockSix == '6')
        blockSix = 'O';
    if(computerPick == 7 && blockSeven == '7')
        blockSeven = 'O';
    if(computerPick == 8 && blockEight == '8')
        blockEight = 'O';
    if(computerPick == 9 && blockNine == '9')
        blockNine = 'O';

    return 0;
}

void gameBoard()
{
    cout << "+-----+-----+-----+" << endl;
    cout << "| " <<blockOne << " | " << blockTwo << " | " << blockThree << " |" << endl;
    cout << "+-----+-----+-----+" << endl;
    cout << "| " <<blockFour << " | " << blockFive << " | " << blockSix << " |" << endl;
    cout << "+-----+-----+-----+" << endl;
    cout << "| " <<blockSeven << " | " << blockEight << " | " << blockNine << " | ";
    cout << "+-----+-----+-----+" << endl;
}

int checkTie()
{
    if(blockOne != '1' && blockTwo != '2' && blockThree != '3' && blockFour != '4' && blockFive != '5' && blockSix != '6' && blockSeven != '7' && blockEight != '8' && blockNine != '9')
    {
        cout << "It's a tie!" << endl;
        gameWin = 0;
    }
//Check on this line since it doesnt work..

}

int main()
{
    //RNGs
    srand(time(0));
    int playAgain;

    int playerScore = 0;
    int computerScore = 0;
    int ties = 0;

    do
    {
        system("CLS");
        turnFirst = rand()% (2 - 1 + 1)+1;//generates starting person.
        computerRandomPick = rand()% (9 - 1 + 1)+1;//computer first pick - random
        gameWin = 3;
        blockOne = '1';
        blockTwo = '2';
        blockThree = '3';
        blockFour = '4';
        blockFive = '5';
        blockSix = '6';
        blockSeven = '7';
        blockEight = '8';
        blockNine = '9';

        //BEGIN OF PROGRAM
        cout << "Welcome to Tic Tac Toe!" <<endl<< endl;
        cout << "Player: " << playerScore << " Computer: "<< computerScore << " Ties: " << ties << endl;

        if(turnFirst == 1)//player first
        {
            cout << "Please choose a grid to place (X): "<<endl<<endl;
            gameBoard();//Gameboard for tic tac toe
            while (!(cin >> playerChoice)) //error traps letters and words
            {
                cout << endl;
                cout << "Numbers only." << endl;
                cin.clear();
                cin.ignore(10000,' ');
            }
            checkPlayerInput();
            system("CLS");
            gameBoard();
            playerTurn = false; //switches to computer
        }

        if(turnFirst == 2)//Computer first
        {
            computerRandomPick;
            computerPick = computerRandomPick;
            checkComputerInput();
            cout << "The computer is choosing... " << endl;
            playerTurn = true;
            gameBoard();
        }


        do
        {
            if(playerTurn == true) //player loop
            {
                cout << "Please choose a grid to place (X): "<<endl<<endl;
                while (!(cin >> playerChoice)) //error traps letters and words
                {
                    cout << endl;
                    cout << "Numbers only." << endl;
                    cin.clear();
                    cin.ignore(10000,' ');
                }
                checkPlayerInput();
                checkWinPlayer();
                checkTie();
                playerTurn = false;
            }

            if(playerTurn == false) //computer loop
            {
                computerAI();
                system("CLS");
                gameBoard();
                checkWinComputer();
                checkTie();
                playerTurn = true;
            }

        }
        while(gameWin > 2);

        if(gameWin == 0)
        {
            cout << "The game is a Tie!" << endl;
            ++ties;
        }


        if(gameWin == 1)
        {
            cout << "The player wins!" << endl;
            ++playerScore;
        }

        if(gameWin == 2)
        {
            cout << "The computer wins!" << endl;
            ++computerScore;
        }


        cout << "Player: " << playerScore << " "<< "Computer: " << computerScore << " Ties: "<< ties << endl;


        cout << "Play again? 1. Yes 2. No " << endl;

        while (!(cin >> playAgain)) //error traps letters and words
        {
            cout << endl;
            cout << "Play again? 1. Yes 2. No " << endl;
            cin.clear();
            cin.ignore(10000,' ');
        }


    }
    while(playAgain == 1);

    return 0;
}