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...
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
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)...
C# (Tic-Tac-Toe) Create class TicTacToe that will enable you to write a complete app to play...
C# (Tic-Tac-Toe) Create class TicTacToe that will enable you to write a complete app to play the game of Tic-Tac-Toe. The class contains a private 3-by-3 rectangular array of integers. The constructor should initialize the empty board to all 0s. Allow two human players. Wherever the first player moves, place a 1 in the specified square, and place a 2 wherever the second player moves. Each move must be to an empty square. After each move, determine whether the game...
QUESTION: This is a tic-tac-toe game in react.js. Covert the following class-based components in React.js to...
QUESTION: This is a tic-tac-toe game in react.js. Covert the following class-based components in React.js to Functional Based component in React. //CODE import React from 'react'; import ReactDOM from 'react-dom'; import './index.css'; function calculateWinner(squares) { const lines = [ [0, 1, 2], [3, 4, 5], [6, 7, 8], [0, 3, 6], [1, 4, 7], [2, 5, 8], [0, 4, 8], [2, 4, 6] ]; for (let i = 0; i < lines.length; i++) { const [a, b, c] = lines[i];...
Using Java please implement a 4x4 Tic-Tac-Toe with Minimax with alpha-beta pruning. PLease comment code heavily...
Using Java please implement a 4x4 Tic-Tac-Toe with Minimax with alpha-beta pruning. PLease comment code heavily so I can follow along and understand the steps. Thank you
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT