Question

In: Computer Science

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 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
        }


};


//Here is an example main program to give an idea how the above class is supposed to work.

int main()
{
    tictactoeGame mygame;
    
    mygame.placeX(1,1);
    mygame.placeO(2,1);
    mygame.placeX(0,2);
    mygame.placeO(2,2);
    
    mygame.display();
}

Solutions

Expert Solution

Solution:

Here is the complete implemented code with main() function.

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()
        {
            int i,j;
            for(i=0;i<2;i++)
            {
                for(j=0;j<2;j++)
                {
                        boardConfig[i][j]=' ';
                                }
                        }
        }   

        //put an 'X' character at the given location          
        bool placeX(int x, int y)
        {
                boardConfig[x][y] ='X';
        }

        //put an 'O' character at the given location         
        bool placeO(int x, int y)
        {
                boardConfig[x][y] ='O';
        }

        //set all positions to character ' '.
        void clear()
        {
            int i,j;
            for(i=0;i<2;i++)
            {
                for(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()
        {
                //Checking rows for X
            if(boardConfig[0][0] == 'X' && boardConfig[0][1] == 'X' && boardConfig[0][3] == 'X' )
                return true;
                        if(boardConfig[1][0] == 'X' && boardConfig[1][1] == 'X' && boardConfig[1][3] == 'X' )
                return true;
                        if(boardConfig[2][0] == 'X' && boardConfig[2][1] == 'X' && boardConfig[2][3] == 'X' )
                return true;
                
                //Checking columns for X
            if(boardConfig[0][0] == 'X' && boardConfig[1][0] == 'X' && boardConfig[2][0] == 'X' )
                return true;
            if(boardConfig[0][1] == 'X' && boardConfig[1][1] == 'X' && boardConfig[2][1] == 'X' )
                return true;
                        if(boardConfig[0][2] == 'X' && boardConfig[1][2] == 'X' && boardConfig[2][2] == 'X' )
                return true;

                //Checking diagonals for X
                        if(boardConfig[0][0] == 'X' && boardConfig[1][1] == 'X' && boardConfig[2][2] == 'X' )
                return true;
            if(boardConfig[0][2] == 'X' && boardConfig[1][1] == 'X' && boardConfig[2][0] == 'X' )
                return true;
                
                return false;
        }

        // Return true if there are 3 'O' marks placed in a single
        // column, row, or diagnol.  Return false otherwise.      
        bool oWins()
        {
            //Checking rows for O
            if(boardConfig[0][0] == 'O' && boardConfig[0][1] == 'O' && boardConfig[0][3] == 'O' )
                return true;
                        if(boardConfig[1][0] == 'O' && boardConfig[1][1] == 'O' && boardConfig[1][3] == 'O' )
                return true;
                        if(boardConfig[2][0] == 'O' && boardConfig[2][1] == 'O' && boardConfig[2][3] == 'O' )
                return true;
                
                //Checking columns for X
            if(boardConfig[0][0] == 'O' && boardConfig[1][0] == 'O' && boardConfig[2][0] == 'O' )
                return true;
            if(boardConfig[0][1] == 'O' && boardConfig[1][1] == 'O' && boardConfig[2][1] == 'O' )
                return true;
                        if(boardConfig[0][2] == 'O' && boardConfig[1][2] == 'O' && boardConfig[2][2] == 'O' )
                return true;

                //Checking diagonals for X
                        if(boardConfig[0][0] == 'O' && boardConfig[1][1] == 'O' && boardConfig[2][2] == 'O' )
                return true;
            if(boardConfig[0][2] == 'O' && boardConfig[1][1] == 'O' && boardConfig[2][0] == 'O' )
                return true;
                
                return false;
        }

        // 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()
        {
            //Checking rows 
            if(boardConfig[0][0] == boardConfig[0][1] && boardConfig[0][1] == boardConfig[0][3] && boardConfig[0][0]!=' ')
                return true;
                        if(boardConfig[1][0] == boardConfig[1][1] && boardConfig[1][1] == boardConfig[1][3] && boardConfig[1][0]!=' ')
                return true;
                        if(boardConfig[2][0] == boardConfig[2][1] && boardConfig[2][1] == boardConfig[2][3] && boardConfig[2][0]!=' ')
                return true;
                
                //Checking columns for X
            if(boardConfig[0][0] == boardConfig[1][0] && boardConfig[1][0] == boardConfig[2][0] && boardConfig[0][0]!=' ')
                return true;
            if(boardConfig[0][1] == boardConfig[1][1] && boardConfig[1][1] == boardConfig[2][1] && boardConfig[0][1]!=' ')
                return true;
                        if(boardConfig[0][2] == boardConfig[1][2] && boardConfig[1][2] == boardConfig[2][2] && boardConfig[0][2]!=' ')
                return true;

                //Checking diagonals for X
                        if(boardConfig[0][0] == boardConfig[1][1] && boardConfig[1][1] == boardConfig[2][2] && boardConfig[0][0]!=' ')
                return true;
            if(boardConfig[0][2] == boardConfig[1][1] && boardConfig[1][1] == boardConfig[2][0] && boardConfig[0][2]!=' ')
                return true;
              
                        return false;             
        }

        // cout a nice looking picture of the board configuration 
        void display()
        {
            cout << "-------------" << endl;
            int i,j;
                for (i = 0; i < 3; i++) 
                        {
                cout << "| ";
                for (j = 0; j < 3; j++) 
                                {
                        cout << boardConfig[i][j] << " | ";
                }
                cout << endl;
                cout << "-------------" << endl;
                }
        }


};


//Here is an example main program to give an idea how the above class is supposed to work.

int main()
{
    tictactoeGame mygame;
    
    mygame.placeX(1,1);
    mygame.placeO(2,1);
    mygame.placeX(0,2);
    mygame.placeO(2,2);
    
    mygame.display();
}

Output obtained:


Related Solutions

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...
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...
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...
How to make a 2D array Tic Tac Toe game in C?
How to make a 2D array Tic Tac Toe game in C?
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!
explain a pseudocode for tic tac toe in c++ between a computer and a player in...
explain a pseudocode for tic tac toe in c++ between a computer and a player in words
In Python: Please complete the following two tasks in Tic Tac Toe: 1. Allow the user...
In Python: Please complete the following two tasks in Tic Tac Toe: 1. Allow the user to choose heads/tails to see if the user goes first or the computer and alter your code accordingly. 2. Allow the user to play again. Add a simple strategy for the AI to play against the user.
Implement the Tic-tac-toe game for variable board sizes, you may assume: 2 < s < 11,...
Implement the Tic-tac-toe game for variable board sizes, you may assume: 2 < s < 11, where s is the board size. Before the game starts the program will prompt for the board size. Note: the winning conditions are the same as the original Tic-tac-toe game in that you need to fill the entire row/column/diagonal to win. Here are a few sample runs. The output is a bit different so that we can handle two-digit coordinates consistently. We expect (but...
Game of Tic Tac Toe with the following conditions A point system where a  move that leads...
Game of Tic Tac Toe with the following conditions A point system where a  move that leads to a winning game is given 1 point, a move that leads to a tie is given 0 point, and a  lead to a losing game will get -1 point. Grid size of 5x5 A timer that can be set for how long a game can be played 5 symbols in a row to get a point Connected lines cannot be crossed (No diagonal lines)...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT