Question

In: Computer Science

C++ Make a Tic Tac Toe game for 2 players to play using 2D arrays and...

C++ Make a Tic Tac Toe game for 2 players to play using 2D arrays and classes. Do not add more #include functions other than the ones listed.

I never said what type of code I needed in a previous question. I apologize and I can't go back and change it so here is the same question with more information


Using the tictactoeGame class, write a main program that uses a tictactoeGame to implement a game in which two players (you and a friend) take turns placing X’s and O’s onto the board. After each turn, the current board configuration should be displayed, and once a player connects three of a kind, the game should end declaring that player the winner.

#include
#include
using namespace std;

class tictactoeGame
{
    public:

        char boardConfig[3][3];   // two dimensional array stores current board configuration  

    // Constructor:
        // set boardConfig[i][j] to be ' ' (the space character)
        // for 0<= i <= 2, 0<= j <= 2
        tictactoeGame()
        {
           //fill this in
        }

   //put an 'X' character at the given location        
        bool placeX(int x, int y)
        {
           //fill this in
        }

   //put an 'O' character at the given location       
        bool placeO(int x, int y)
        {
           //fill this in
        }

   //set all positions to character ' '.
        void clear()
        {
           //fill this in
        }

        // Return true if there are 3 'X' marks placed in a single
        // column, row, or diagnol. Return false otherwise.
        bool xWins()
        {
           //fill this in
        }

        // Return true if there are 3 'O' marks placed in a single
        // column, row, or diagnol. Return false otherwise.    
        bool oWins()
        {
           //fill this in
        }

        // Return true if there are either 3 'X' marks or 3 'O' marks
        // placed in a single column, row, or diagnol, or if the board is full.
    // Return false otherwise.
        bool gameOver()
        {
           //fill this in
        }

        // cout a nice looking picture of the board configuration
        void display()
        {
           //fill this in
        }


};

int main()
{
return 0;
}

Solutions

Expert Solution

The code has been kept as simpler it could be for better understanding, please use the only integer for the row value(x) and column value(y) asked in the program. If the entered configuration of row and column already filled with X or O in that case code will say invalid input and ask to enter again, but it is not handled for values other than an integer ( for ex. if you enter "asd" in the place of row and column the program will terminate, if you want me to handle this it will be a little complex if you want, mention in the comment section, I will provide it too). The explanation of the code has been added to the comment lines and a sample screenshot of the running program has been added in the last as well.

CODE:

#include<iostream>
using namespace std;

class tictactoeGame
{
    public:

        char boardConfig[3][3];   // two dimensional array stores current board configuration

    // Constructor:
        // set boardConfig[i][j] to be ' ' (the space character)
        // for 0<= i <= 2, 0<= j <= 2
        tictactoeGame()
        {
           //fill this in
           for(int i=0;i<=2;i++)
            for(int j=0;j<=2;j++)
                boardConfig[i][j] = ' ';            // initialized all the values with space
        }

   //put an 'X' character at the given location
        bool placeX(int x, int y)
        {
           //fill this in
           if (x>=0 && x<=2 && y>=0 && y<=2 && boardConfig[x][y]==' '){   // check if it is valid, if it is place x on the position else return false
                boardConfig[x][y] = 'X';
                return true;
           }
           return false;

        }

   //put an 'O' character at the given location
        bool placeO(int x, int y)
        {
           //fill this in
           if (x>=0 && x<=2 && y>=0 && y<=2 && boardConfig[x][y]==' '){      // same for player O
                boardConfig[x][y] = 'O';
                return true;
           }
           return false;
        }

   //set all positions to character ' '.
        void clear()                                                       // to clear the board, assign space ' ' to all the places in board
        {
           //fill this in
           for(int i=0;i<=2;i++)
            for(int j=0;j<=2;j++)
                boardConfig[i][j] = ' ';

        }

        // Return true if there are 3 'X' marks placed in a single
        // column, row, or diagnol. Return false otherwise.
        bool xWins()
        {
           //fill this in

           // check for row win
           bool win = true;
           for(int i=0;i<=2;i++){
                win = true;
                for(int j=0;j<=2;j++){                   // checking for all the row values if it is equa to X
                    if (boardConfig[i][j]!='X'){
                        win = false;
                        continue;
                    }

                }
                if (win)
                    return win;

           }
           // check for col win
           win = true;
           for(int i=0;i<=2;i++){
                win = true;
                for(int j=0;j<=2;j++){
                    if (boardConfig[j][i]!='X'){          // checking for all the column values if any column value consists 3 x
                        win = false;
                        continue;
                    }

                }
                if (win)
                    return win;

           }
           // check for diagonal win
           win = true;
           for(int i=0;i<=2;i++){
                if (boardConfig[i][i]!='X')              // checking for main diagonal ( i==j for main diagonal)
                        win = false;
           }
           if (win)
                return win;

            win = true;
            for(int i=0;i<=2;i++){
                int j = 2-i;
                 if (boardConfig[i][j]!='X')            // checking for other diagonal
                        win = false;

            }
            return win;

        }

        // Return true if there are 3 'O' marks placed in a single
        // column, row, or diagnol. Return false otherwise.
        bool oWins()                                        // same as xWins()
        {
           //fill this in

           // check for row win
           bool win = true;
           for(int i=0;i<=2;i++){
                win = true;
                for(int j=0;j<=2;j++){
                    if (boardConfig[i][j]!='O'){
                        win = false;
                        continue;
                    }

                }
                if (win)
                    return win;

           }
           // check for col win
           win = true;
           for(int i=0;i<=2;i++){
                win = true;
                for(int j=0;j<=2;j++){
                    if (boardConfig[j][i]!='O'){
                        win = false;
                        continue;
                    }

                }
                if (win)
                    return win;

           }
           // check for diagonal win
           win = true;
           for(int i=0;i<=2;i++){
                if (boardConfig[i][i]!='O')
                        win = false;
           }
           if (win)
                return win;

            win = true;
            for(int i=0;i<=2;i++){
                int j = 2-i;
                 if (boardConfig[i][j]!='O')
                        win = false;

            }
            return win;
        }

        // Return true if there are either 3 'X' marks or 3 'O' marks
        // placed in a single column, row, or diagnol, or if the board is full.
    // Return false otherwise.
        bool gameOver()
        {
           //fill this in
           if (xWins() || oWins())               // if any one of player (x or o) won return true else false
                return true;
           return false;
        }

        // cout a nice looking picture of the board configuration
        void display()                           // for displaying the board
        {
           //fill this in
            cout<<"\n\n";

            cout<<"\t\t\t"<<boardConfig[0][0]<<" | "<<boardConfig[0][1]<<" | "<<boardConfig[0][2]<<endl;
            cout<<"\t\t\t---------"<<endl;
            cout<<"\t\t\t"<<boardConfig[1][0]<<" | "<<boardConfig[1][1]<<" | "<<boardConfig[1][2]<<endl;
            cout<<"\t\t\t---------"<<endl;
            cout<<"\t\t\t"<<boardConfig[2][0]<<" | "<<boardConfig[2][1]<<" | "<<boardConfig[2][2]<<endl;

    return;

        }


};

int main()
{
    tictactoeGame game;
    int i = 0,x,y;
    game.display();
    while(!game.gameOver()){            // loop until the game over
        if (i%2==0){                    // i is a counter if i is even it denotes its x's turn else its y's turn
            cout<<"X's turn: "<<endl;
            cout<<"Enter integer values of x and y to place X (follow 0-indexing): ";  // ask the user to enter the position values for the board
            cin>>x>>y;
            while(!game.placeX(x,y)){
                cout<<"Invalid input! please enter again: ";    // if placeX return false keep asking for the valid input
                cin>>x>>y;
            }
            game.display();        // after placing x display the board
            if (game.xWins()){     // check if x wins or not and declare if won.
                cout<<"Player X won!"<<endl;
            }
        }
        else{
            cout<<"O's turn: "<<endl;      // same for O's turn
            cout<<"Enter integer values of x and y to place O (follow 0-indexing): ";
            cin>>x>>y;
            while(!game.placeO(x,y)){
                cout<<"Invalid input! please enter again: ";
                cin>>x>>y;
            }
            game.display();
            if (game.oWins()){
                cout<<"Player O won!"<<endl;
            }
        }
        i++;

    }
    game.clear();           // in last clear the boardconfig.
    return 0;
}

OUTPUT(SCREENSHOT)

NOTE: If you have any queries regarding the solution, you can menstion in the comment box. HAPPY LEARNING!!


Related Solutions

Make a Tic Tac Toe game for 2 players to play using 2D arrays and classes....
Make a Tic Tac Toe game for 2 players to play using 2D arrays and classes. Do not add more #include functions other than the ones listed (such as #include <stdio.h> etc). Using the tictactoeGame class, write a main program that uses a tictactoeGame to implement a game in which two players (you and a friend) take turns placing X’s and O’s onto the board. After each turn, the current board configuration should be displayed, and once a player connects...
How to make a 2D array Tic Tac Toe game in C?
How to make a 2D array Tic Tac Toe game in C?
Write a program that allows two players to play a game of tic-tac-toe. Use a two-dimensional...
Write a program that allows two players to play a game of tic-tac-toe. Use a two-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: 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 user to enter the row...
c++ //Tic tac toe lab. Topics: 2D arrays, classes. // Part 1 //Implement the following specifications...
c++ //Tic tac toe lab. Topics: 2D arrays, classes. // Part 1 //Implement the following specifications for a tic tac toe game object: class tictactoeGame { public: char boardConfig[3][3]; // two dimensional array stores current board configuration // Constructor: // set boardConfig[i][j] to be ' ' (the space character) // for 0<= i <= 2, 0<= j <= 2 tictactoeGame() { //fill this in } //put an 'X' character at the given location bool placeX(int x, int y) { //fill...
c++ //Tic tac toe lab. Topics: 2D arrays, classes. // Part 1 //Implement the following specifications...
c++ //Tic tac toe lab. Topics: 2D arrays, classes. // Part 1 //Implement the following specifications for a tic tac toe game object: class tictactoeGame { public: char boardConfig[3][3]; // two dimensional array stores current board configuration // Constructor: // set boardConfig[i][j] to be ' ' (the space character) // for 0<= i <= 2, 0<= j <= 2 tictactoeGame() { //fill this in } //put an 'X' character at the given location bool placeX(int x, int y) { //fill...
How to make tic tac toe game in javascript with vue.js
How to make tic tac toe game in javascript with vue.js
Write a program that plays tic-tac-toe. The tic-tac-toe game is played on a 3 × 3...
Write a program that plays tic-tac-toe. The tic-tac-toe game is played on a 3 × 3 grid as shown below: The game is played by two players, who take turns. The first player marks moves with a circle, the second with a cross. The player who has formed a horizontal, vertical, or diagonal sequence of three marks wins. Your program should draw the game board, ask the user for the coordinates of the next mark (their move), change the players...
The objective of this assignment is to implement the tic-tac-toe game with a C program. The...
The objective of this assignment is to implement the tic-tac-toe game with a C program. The game is played by two players on a board defined as a 5x5 grid (array). Each board position can contain one of two possible markers, either ‘X’ or ‘O’. The first player plays with ‘X’ while the second player plays with ‘O’. Players place their markers in an empty position of the board in turns. The objective is to place 5 consecutive markers of...
In C# A Tic Tac Toe program that uses functions and arrays. The program should start...
In C# A Tic Tac Toe program that uses functions and arrays. The program should start by asking the player one for their name and then player two for their name. Then should print the board and ask the user what column and then what row they would like to put their x (or o, depending on the player) . Use the clear function to make it look as though the board is never repeating.. Thanks!
- Make a 2-D array to represent a Tic-Tac-Toe game/grid. Place a couple of 'X's and...
- Make a 2-D array to represent a Tic-Tac-Toe game/grid. Place a couple of 'X's and 'O's in the grid and display it. Do some extra printing to make it look like a Tic-Tac-Toe board we are suppose to use rows and columns so just make it simple thanks!
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT