Question

How to write a (c++ program) tic tac toe code where its the computer against a...

How to write a (c++ program) tic tac toe code where its the computer against a human. and the computer always wins.

a basic c++ code program
using gaming theory
arrays
<stdio.h>

computer plays tic tac toe against a user.
computer always when or the game is a draw.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

/* Program in c++ */

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

using namespace std;


bool TurnPlayer;
int gameWin = 3;
int ComputerRandomPosition; //random pick computer position
int computerPick; //decide term
int Choice_Of_The_player;


//board for tic tac toe arrays
char block1 = '1';
char block2 = '2';
char block3 = '3';
char block4 = '4';
char block5 = '5';
char block6 = '6';
char block7 = '7';
char block8 = '8';
char block9 = '9';
int Firstturn; //variable to decide whoever goes first
//check the all codition for win
int CheckComputerWin()
{
    if(block1 == 'O' && block5 == 'O' && block9 == 'O' && TurnPlayer == false)
        gameWin = 2;
    if(block3 == 'O' && block5 == 'O' && block7 == 'O' && TurnPlayer == false)
        gameWin = 2;
    if(block1 == 'O' && block2 == 'O' && block3 == 'O' && TurnPlayer == false)
        gameWin = 2;
    if(block4 == 'O' && block5 == 'O' && block6 == 'O' && TurnPlayer == false)
        gameWin = 2;
    if(block7 == 'O' && block8 == 'O' && block9 == 'O' && TurnPlayer == false)
        gameWin = 2;
    if(block1 == 'O' && block4 == 'O' && block7 == 'O' && TurnPlayer == false)
        gameWin = 2;
    if(block2 == 'O' && block5 == 'O' && block8 == 'O' && TurnPlayer == false)
        gameWin = 2;
    if(block3 == 'O' && block6 == 'O' && block9 == 'O' && TurnPlayer == false)
        gameWin = 2;
}
int checkWinPlayer()
{
    if(block1 == 'X' && block5 == 'X' && block9 == 'X' && TurnPlayer == true) //diagonal, 1 - 5 = 9
        gameWin = 1; //This will make player win.
    if(block3 == 'X' && block5 == 'X' && block7 == 'X' && TurnPlayer == true) //diagonal, 3 - 5 = 7
        gameWin = 1;
    if(block1 == 'X' && block2 == 'X' && block3 == 'X' && TurnPlayer == true) //horizontal 1 - 2 = 3
        gameWin = 1;
    if(block4 == 'X' && block5 == 'X' && block6 == 'X' && TurnPlayer == true) //horizontal 4 - 5 = 6
        gameWin = 1;
    if(block7 == 'X' && block8 == 'X' && block9 == 'X' && TurnPlayer == true) //horizontal 7 - 8 = 9
        gameWin = 1;
    if(block1 == 'X' && block4 == 'X' && block7 == 'X' && TurnPlayer == true) //vertical 1 - 4 = 7
        gameWin = 1;
    if(block2 == 'X' && block5 == 'X' && block8 == 'X' && TurnPlayer == true) //vertical 2 - 5 = 8
        gameWin = 1;
    if(block3 == 'X' && block6 == 'X' && block9 == 'X' && TurnPlayer == true) //vertical 3 - 6 = 9
        gameWin = 1;
}
int ArtificalComputer()
{
    TurnPlayer == false;

    if(block1 == 'O' && block2 == 'O' && TurnPlayer == false && block3 == '3')
    {
        block3 = 'O';
        TurnPlayer = true;
    }

    if(block4 == 'O' && block5 == 'O' && TurnPlayer == false && block6 == '6')
    {
        block6 = 'O';
        TurnPlayer = true;
    }

    if(block7 == 'O' && block8 == 'O' && TurnPlayer == false && block9 == '9')
    {
        block9 = 'O';
        TurnPlayer = true;
    }

    if(block1 == 'O' && block4 == 'O' && TurnPlayer == false && block7 == '7')
    {
        block7 = 'O';
        TurnPlayer = true;
    }

    if(block2 == 'O' && block5 == 'O' && TurnPlayer == false && block8 == '8')
    {
        block8 = 'O';
        TurnPlayer = true;
    }

    if(block3 == 'O' && block6 == 'O' && TurnPlayer == false && block9 == '9')
    {
        block9 = 'O';
        TurnPlayer = true;
    }

    if(block1 == 'O' && block5 == 'O' && TurnPlayer == false && block9 == '9')
    {
        block9 = 'O';
        TurnPlayer = true;
    }

    if(block3 == 'O' && block5 == 'O' && TurnPlayer == false && block7 == '7')
    {
        block7 = 'O';
        TurnPlayer == true;
    }

    if((Choice_Of_The_player == 1 || Choice_Of_The_player == 5 || Choice_Of_The_player == 9) && TurnPlayer == false)
    {
        if((block1 == 'X' && block5 == 'X') && TurnPlayer == false && block9 == '9')
        {
            block9 = 'O';
            TurnPlayer = true;
        }

        if((block1 == 'X' && block9 == 'X') && TurnPlayer == false && block5 == '5')
        {
            block5 = 'O';
            TurnPlayer = true;
        }

        if((block5 == 'X' && block9 == 'X') && TurnPlayer == false && block1 == '1')
        {
            block1 = 'O';
            TurnPlayer = true;
        }
    }

    if((Choice_Of_The_player == 3 || Choice_Of_The_player == 5 || Choice_Of_The_player == 7) && TurnPlayer == false)
    {
        if((block7 == 'X' && block5 == 'X') && TurnPlayer == false && block3 == '3')
        {
            block3 = 'O';
            TurnPlayer = true;
        }

        if((block7 == 'X' && block3 == 'X') && TurnPlayer == false && block5 == '5')
        {
            block5 = 'O';
            TurnPlayer = true;
        }

        if((block5 == 'X' && block3 == 'X') && TurnPlayer == false && block7 == '7')
        {
            block7 = 'O';
            TurnPlayer = true;
        }
    }

    if((Choice_Of_The_player == 1 || Choice_Of_The_player == 2 || Choice_Of_The_player == 3) && TurnPlayer == false)
    {
        if((block1 == 'X' && block2 == 'X') && TurnPlayer == false && block3 == '3')
        {
            block3 = 'O';
            TurnPlayer = true;
        }

        if((block1 == 'X' && block3 == 'X') && TurnPlayer == false && block2 == '2')
        {
            block2 = 'O';
            TurnPlayer = true;
        }

        if((block2 == 'X' && block3 == 'X') && TurnPlayer == false && block1 == '1')
        {
            block1 = 'O';
            TurnPlayer = true;
        }
    }

    if((Choice_Of_The_player == 4 || Choice_Of_The_player == 5 || Choice_Of_The_player == 6) && TurnPlayer == false)
    {
        if((block4 == 'X' && block5 == 'X') && TurnPlayer == false && block6 == '6')
        {
            block6 = 'O';
            TurnPlayer = true;
        }

        if((block4 == 'X' && block6 == 'X') && TurnPlayer == false && block5 == '5')
        {
            block5 = 'O';
            TurnPlayer = true;
        }

        if((block5 == 'X' && block6 == 'X') && TurnPlayer == false && block4 == '4')
        {
            block4 = 'O';
            TurnPlayer = true;
        }
    }

    if((Choice_Of_The_player == 7 || Choice_Of_The_player == 8 || Choice_Of_The_player == 9) && TurnPlayer == false)
    {
        if((block7 == 'X' && block8 == 'X') && TurnPlayer == false && block9 == '9')
        {
            block9 = 'O';
            TurnPlayer = true;
        }

        if((block7 == 'X' && block9 == 'X') && TurnPlayer == false && block8 == '8')
        {
            block8 = 'O';
            TurnPlayer = true;
        }

        if((block8 == 'X' && block9 == 'X') && TurnPlayer == false && block7 == '7')
        {
            block7 = 'O';
            TurnPlayer = true;
        }
    }

    if((Choice_Of_The_player == 1 || Choice_Of_The_player == 4 || Choice_Of_The_player == 7) && TurnPlayer == false)
    {
        if((block1 == 'X' && block4 == 'X') && TurnPlayer == false && block7 == '7')
        {
            block7 = 'O';
            TurnPlayer = true;
        }

        if((block1 == 'X' && block7 == 'X') && TurnPlayer == false && block4 == '4')
        {
            block4 = 'O';
            TurnPlayer = true;
        }

        if((block4 == 'X' && block7 == 'X') && TurnPlayer == false && block1 == '1')
        {
            block1 = 'O';
            TurnPlayer = true;
        }

    }

    if((Choice_Of_The_player == 2 || Choice_Of_The_player == 5 || Choice_Of_The_player == 8) && TurnPlayer == false)
    {
        if((block2 == 'X' && block5 == 'X') && TurnPlayer == false && block8 == '8')
        {
            block8 = 'O';
            TurnPlayer = true;
        }

        if((block2 == 'X' && block8 == 'X') && TurnPlayer == false && block5 == '5')
        {
            block5 = 'O';
            TurnPlayer = true;
        }

        if((block5 == 'X' && block8 == 'X') && TurnPlayer == false && block2 == '2')
        {
            block2 = 'O';
            TurnPlayer = true;
        }
    }

    if((Choice_Of_The_player == 3 || Choice_Of_The_player == 6 || Choice_Of_The_player == 9) && TurnPlayer == false)
    {
        if((block3 == 'X' && block6 == 'X') && TurnPlayer == false && block9 == '9')
        {
            block9 = 'O';
            TurnPlayer = true;
        }

        if((block3 == 'X' && block9 == 'X') && TurnPlayer == false && block6 == '6')
        {
            block6 = 'O';
            TurnPlayer = true;
        }

        if((block6 == 'X' && block9 == 'X') && TurnPlayer == false && block3 == '3')
        {
            block3 = 'O';
            TurnPlayer = true;
        }
    }
    else
    {
        do
        {

            if(block1 == '1' && TurnPlayer == false)
            {
                TurnPlayer = true;
                block1 = 'O';
            }
            if(block2 == '2' && TurnPlayer == false)
            {
                TurnPlayer = true;
                block2 = 'O';
            }
            if(block3 == '3' && TurnPlayer == false)
            {
                TurnPlayer = true;
                block3 = 'O';
            }
            if(block4 == '4' && TurnPlayer == false)
            {
                TurnPlayer = true;
                block4 = 'O';
            }
            if(block5 == '5' && TurnPlayer == false)
            {
                TurnPlayer = true;
                block5 = 'O';
            }
            if(block6 == '6' && TurnPlayer == false)
            {
                TurnPlayer = true;
                block6 = 'O';
            }
            if(block7 == '7' && TurnPlayer == false)
            {
                TurnPlayer = true;
                block7 = 'O';
            }
            if(block8 == '8' && TurnPlayer == false)
            {
                TurnPlayer = true;
                block8 = 'O';
            }
            if(block9 == '9' && TurnPlayer == false)
            {
                TurnPlayer = true;
                block9 = 'O';
            }
        }
        while(TurnPlayer = false);
    }
    return 0;
}

int checkPlayerInput()
{
    if(Choice_Of_The_player == 1 && block1 == '1')
        block1 = 'X';
    if(Choice_Of_The_player == 2 && block2 == '2')
        block2 = 'X';
    if(Choice_Of_The_player == 3 && block3 == '3')
        block3 = 'X';
    if(Choice_Of_The_player == 4 && block4 == '4')
        block4 = 'X';
    if(Choice_Of_The_player == 5 && block5 == '5')
        block5 = 'X';
    if(Choice_Of_The_player == 6 && block6 == '6')
        block6 = 'X';
    if(Choice_Of_The_player == 7 && block7 == '7')
        block7 = 'X';
    if(Choice_Of_The_player == 8 && block8 == '8')
        block8 = 'X';
    if(Choice_Of_The_player == 9 && block9 == '9')
        block9 = 'X';


    return 0;
}

int checkComputerInput()
{
    if(computerPick == 1 && block1 == '1')
        block1 = 'O';
    if(computerPick == 2 && block2 == '2')
        block2 = 'O';
    if(computerPick == 3 && block3 == '3')
        block3 = 'O';
    if(computerPick == 4 && block4 == '4')
        block4 = 'O';
    if(computerPick == 5 && block5 == '5')
        block5 = 'O';
    if(computerPick == 6 && block6 == '6')
        block6 = 'O';
    if(computerPick == 7 && block7 == '7')
        block7 = 'O';
    if(computerPick == 8 && block8 == '8')
        block8 = 'O';
    if(computerPick == 9 && block9 == '9')
        block9 = 'O';

    return 0;
}

void Borad_of_GAME()
{
    cout << "+-----+-----+-----+" << endl;
    cout << "| " <<block1 << " | " << block2 << " | " << block3 << " |" << endl;
    cout << "+-----+-----+-----+" << endl;
    cout << "| " <<block4 << " | " << block5 << " | " << block6 << " |" << endl;
    cout << "+-----+-----+-----+" << endl;
    cout << "| " <<block7 << " | " << block8 << " | " << block9 << " |\n";
    cout << "+-----+-----+-----+" << endl;
}

int checkTie()
{
    if(block1 != '1' && block2 != '2' && block3 != '3' && block4 != '4' && block5 != '5' && block6 != '6' && block7 != '7' && block8 != '8' && block9 != '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");
        Firstturn = rand()% (2 - 1 + 1)+1;//generates starting person.
        ComputerRandomPosition = rand()% (9 - 1 + 1)+1;//computer first pick - random
        gameWin = 3;
        block1 = '1';
        block2 = '2';
        block3 = '3';
        block4 = '4';
        block5 = '5';
        block6 = '6';
        block7 = '7';
        block8 = '8';
        block9 = '9';

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

        if(Firstturn == 1)//player first
        {
            cout << "Please Select your a grid to place (X): "<<endl<<endl;
            Borad_of_GAME();//Borad_of_GAME for tic tac toe
            while (!(cin >> Choice_Of_The_player)) //error traps letters and words
            {
                cout << endl;
                cout << "Numbers only." << endl;
               // cin.clear();
                //cin.ignore(10000,'\n');
            }
            checkPlayerInput();
           // system("CLS");
            Borad_of_GAME();
            TurnPlayer = false; //switches to computer
        }

        if(Firstturn == 2)//Computer first
        {
            ComputerRandomPosition;
            computerPick = ComputerRandomPosition;
            checkComputerInput();
            cout << "The computer is choosing...\n" << endl;
            TurnPlayer = true;
            Borad_of_GAME();
        }


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

            if(TurnPlayer == false) //computer loop
            {
                ArtificalComputer();
                Borad_of_GAME();
                CheckComputerWin();
                checkTie();
                TurnPlayer = 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?\n1. Yes\n2. No\n" << endl;

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


    }
    while(playAgain == 1);

    return 0;
}

/*

Welcome to Tic Tac Toe Game

Player: 0 Computer: 0 Ties: 0
The computer is choosing...

+-----+-----+-----+
| 1 | 2 | O |
+-----+-----+-----+
| 4 | 5 | 6 |
+-----+-----+-----+
| 7 | 8 | 9 |
+-----+-----+-----+
Please choose a grid to place (X):

1
+-----+-----+-----+
| X | O | O |
+-----+-----+-----+
| 4 | 5 | 6 |
+-----+-----+-----+
| 7 | 8 | 9 |
+-----+-----+-----+
Please choose a grid to place (X):

3
+-----+-----+-----+
| X | O | O |
+-----+-----+-----+
| 4 | 5 | 6 |
+-----+-----+-----+
| 7 | 8 | 9 |
+-----+-----+-----+
Please choose a grid to place (X):

5
+-----+-----+-----+
| X | O | O |
+-----+-----+-----+
| 4 | X | 6 |
+-----+-----+-----+
| 7 | 8 | O |
+-----+-----+-----+
Please choose a grid to place (X):

9
+-----+-----+-----+
| X | O | O |
+-----+-----+-----+
| 4 | X | 6 |
+-----+-----+-----+
| 7 | 8 | O |
+-----+-----+-----+
Please choose a grid to place (X):

6
+-----+-----+-----+
| X | O | O |
+-----+-----+-----+
| O | X | X |
+-----+-----+-----+
| 7 | 8 | O |
+-----+-----+-----+
Please choose a grid to place (X):

7
+-----+-----+-----+
| X | O | O |
+-----+-----+-----+
| O | X | X |
+-----+-----+-----+
| X | O | O |
+-----+-----+-----+
It's a tie!
The game is a Tie!
Player: 0 Computer: 0 Ties: 1
Play again?
1. Yes
2. No

*/

Add a comment
Know the answer?
Add Answer to:
How to write a (c++ program) tic tac toe code where its the computer against a...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Tic-Tac-Toe (arrays and multidimensional arrays) C++ Write an interactive program that plays tic-tac-toe. Represent the board...

    Tic-Tac-Toe (arrays and multidimensional arrays) C++ Write an interactive program that plays tic-tac-toe. Represent the board as a 3 X 3 character array. Initialize the array to blanks and ask each player in turn to input a position. The first player's position is marked on the board with a O and the second player's position is marked with an X. Continue the process until a player wins or the game is a draw. To win, a player must have 3...

  • In Java create a program where a user plays Tic Tac Toe against the computer. Create...

    In Java create a program where a user plays Tic Tac Toe against the computer. Create a class TicTacToe that will enable you write a program to play Tic-Tac-Toe. The class contains a private 3x3 two-dimensional array. Use an enum type to represent the value in each cell of the array. The enum’s constants should be named X, O, and EMPTY (for a position that does not contain an X or O). The constructor should initialize the board elements to...

  • I need to create a Tic Tac Toe program in C++. These are the requirements Write...

    I need to create a Tic Tac Toe program in C++. These are the requirements Write a program that allows the computer to play TicTacToe against a human player or allow two human players to play one another. Implement the following conditions: The player that wins the current game goes first in the next round, and their symbol is X. The other player will be O. Keep track of the number of games played, wins, and draws for each player....

  • Hey guys, I need help writing a Tic-Tac-Toe game programmed in Java. The game has to...

    Hey guys, I need help writing a Tic-Tac-Toe game programmed in Java. The game has to be a 1D (One-Dimension) array board NOT 2D. It has to be a human plays against the computer type of Tic-Tac-Toe. Here is the requirements for the program. PLEASE NO COPY AND PASTE ANSWER. Thank you in advance! You will develop a program in which a human plays against the computer. 1. Validate user input at every opportunity. a. Do not allow number entries...

  • 1. Use Turtle Graphics to create a tic tac toe game in Python. Write a Python program that allows for one player vs computer to play tic tac toe game, without using turtle.turtle

    1. Use Turtle Graphics to create a tic tac toe game in Python. Write a Python program that allows for one player vs computer to play tic tac toe game, without using turtle.turtle

  • (Tic-Tac-Toe) Create a class Tic-Tac-Toe that will enable you to write a program to play Tic-Tac-Toe....

    (Tic-Tac-Toe) Create a class Tic-Tac-Toe that will enable you to write a program to play Tic-Tac-Toe. The class contains a private 3-by-3 two-dimensional array. Use an enumeration to represent the value in each cell of the array. The enumeration’s constants should be named X, O and EMPTY (for a position that does not contain an X or an O). The constructor should initialize the board elements to EMPTY. Allow two human players. Wherever the first player moves, place an X...

  • EXTRA CREDIT 1: Tic Tac Toe (Please use c++ code) also please verify it works because...

    EXTRA CREDIT 1: Tic Tac Toe (Please use c++ code) also please verify it works because I've received wrong answers in the past. Thank You 99 17 30 12 EXTRA CREDIT 1: Tic Tac Toe (10 points) Write a console program that plays Tic Tac Toe with the user. The program displays a 3 by 3 grid, which is initially blank. When the player takes a turn, an X is added to the grid. When the computer takes a turn,...

  • 18. Tic-Tac-Toe Game rite a program that allows two players to play a game of tic-tac-toe. Use di...

    18. Tic-Tac-Toe Game rite a program that allows two players to play a game of tic-tac-toe. Use dimensional char array with three rows and three columns as the game board. Each element of the array should be initialized with an asterisk (*). The program should run a loop that does the following: Write . Displays the contents of the board array. . Allows player 1 to select a location on the board for an X. The program should ask the...

  • Assignment C++: Rock-Scissor-Paper & Tic-Tac-Toe i need you to write two different program for RSP and...

    Assignment C++: Rock-Scissor-Paper & Tic-Tac-Toe i need you to write two different program for RSP and TTT: R-S-P Requirement: - write one program that mimics the Rock-Scissor-Paper game. This program will ask user to enter an input (out of R-S-P), and the computer will randomly pick one and print out the result (user wins / computer wins). - User's input along with computer's random pick will be encoded in the following format: -- user's rock: 10 -- user's scissor: 20...

  • Please do this in Java (Java FX) Tic-Tac-Toe game (aka X's and O's) in which the...

    Please do this in Java (Java FX) Tic-Tac-Toe game (aka X's and O's) in which the player, who is "X", plays against the application, which is "O". The game should be built using the basic window-with-canvas code used for the various tutorials and course assignments. The user plays by dragging "X" tokens onto the Tic-Tac-Toe board area after which the application will automatically play by moving an "O" onto the board area. When a game is over the app should...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT