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

Chapter 12 PP 2 Please the other code for the same question in Q & A desn\'t com

ID: 3784585 • Letter: C

Question

Chapter 12 PP 2

Please the other code for the same question in Q & A desn't compile, and run.

So plesae comment the code,

A software company that develops games has just hired you! Before working on the next version of Medieval Menace they have given you the task of implementing the tictac-toe game in C++. Tic-tac-toe consists of a 3 × 3 game board of squares where each square is either empty, has an X marker, or has a O marker. Two players represented by an X or an O play the game. The objective is for one player to get three Xs or three Os in a row first. Design, implement, and test classes that represent a tic-tac-toe game board and X and O markers. Your classes should provide suitable observer and mutator methods for modifying the game board and displaying the state of the game. Use your classes to create a game that prompts for player X and player O to place markers at specified locations on the game board. After each move your program should display the current game board to the console. Your program should also check after each move if there is a winning configuration of the game board. If so, the game should complete indicating which player won.

Explanation / Answer

#include<iostream>
#include<stdlib.h>
#include<vector>
using namespace std;

char square[10] = {'o','a','b','c','d','e','f','g','h','i'};
char symbol[3];
string playername[3];
const bool CLEAR_SCREEN = true;
class TTTBoard {

public:
    TTTBoard(){}
    void clearScreen();
    void drawBoard(string playername[]) const;
    void getPlay(string playername[]);
    int gameWon() const;
};
void TTTBoard :: clearScreen()
{
        if (CLEAR_SCREEN) {

                cout << "c";
        }
        cout<<" **** Initial state ********* ";
        square[1] = 'a'; square[2] = 'b'; square[3] = 'c';
        square[4] = 'd'; square[5] = 'e'; square[6] = 'f';
        square[7] = 'g'; square[8] = 'h'; square[9] = 'i';
        getPlay(playername);

}
void TTTBoard :: getPlay(string playername[]){


        int     player = 1,i;
        char    position;
        char mark;
        do
        {
                drawBoard(playername);
                player=(player%2)?1:2;

                cout <<" "<<playername[player] << ", enter a position: ";
                cin >> position;

                //mark=(player == 1) ? 'X' : 'O';
                if(symbol[player] == 'X')
                        mark = 'X';
                if(symbol[player] == 'O')
                        mark = 'O';
                if ( position == 'a' && square[1] == 'a')

                        square[1] = mark;
                else if (position == 'b' && square[2] == 'b')

                        square[2] = mark;
                else if (position == 'c' && square[3] == 'c')

                        square[3] = mark;
                else if (position =='d' && square[4] == 'd')

                        square[4] = mark;
                else if (position == 'e' && square[5] == 'e')

                        square[5] = mark;
                else if (position == 'f' && square[6] == 'f')

                        square[6] = mark;
                else if (position == 'g' && square[7] == 'g')
                        square[7] = mark;
                else if (position == 'h' && square[8] == 'h')

                        square[8] = mark;
                else if (position == 'i' && square[9] == 'i')

                        square[9] = mark;
                else
                {
                        cout<<" Invalid move ";

                        player--;
                        cin.ignore();
                        cin.get();
                }
                i = gameWon();

                player++;
        }while(i==-1);
        drawBoard(playername);
        if(i==1)

                cout<<" Player "<<playername[--player]<<" win ";
        else
                cout<<" Game Draw";
        cin.ignore();
        cin.get();
}

/*********************************************
*
*      FUNCTION TO RETURN GAME STATUS
*      1 FOR GAME IS OVER WITH RESULT
*     -1 FOR GAME IS IN PROGRESS
*      O GAME IS OVER AND NO RESULT
***********************************************/

int TTTBoard :: gameWon() const
{
        enum status {PLAYING, X_WINS=1, Y_WINS=1, TIE=-1, UNDEF};

        if (square[1] == square[2] && square[2] == square[3])

                return X_WINS;
        else if (square[4] == square[5] && square[5] == square[6])

                return X_WINS;
        else if (square[7] == square[8] && square[8] == square[9])

                return X_WINS;
        else if (square[1] == square[4] && square[4] == square[7])

                return X_WINS;
        else if (square[2] == square[5] && square[5] == square[8])

                return Y_WINS;
        else if (square[3] == square[6] && square[6] == square[9])

                return Y_WINS;
        else if (square[1] == square[5] && square[5] == square[9])

                return Y_WINS;
        else if (square[3] == square[5] && square[5] == square[7])

                return Y_WINS;
        else if (square[1] != 'a' && square[2] != 'b' && square[3] != 'c'
                    && square[4] != 'd' && square[5] != 'e' && square[6] != 'f'
                  && square[7] != 'g' && square[8] != 'h' && square[9] != 'i')

                return PLAYING;
        else
                return TIE;
}


/*******************************************************************
*      FUNCTION TO DRAW BOARD OF TIC TAC TOE WITH PLAYERS MARK
*********************************************************************/

void TTTBoard :: drawBoard(string playername[]) const
{
        cout << "    Tic Tac Toe ";

        cout << "      |     |     " << endl;
        cout << " " << square[1] << " | " << square[2] << " | " << square[3] << endl;

        cout << " _____|_____|_____" << endl;
        cout << "      |     |     " << endl;

        cout << " " << square[4] << " | " << square[5] << " | " << square[6] << endl;

        cout << " _____|_____|_____" << endl;
        cout << "      |     |     " << endl;

        cout << " " << square[7] << " | " << square[8] << " | " << square[9] << endl;

        cout << "      |     |     " << endl << endl;

        cout<<" "<<playername[1]<<" ("<<symbol[1]<<"), "<<playername[2]<<" ("<<symbol[2]<<") ";
        cout << endl;
}
int main()
{
        TTTBoard T;
        cout<<" *** WelCome *** Enter player name 1. ";    cin>>playername[1];
        playername[2] = "Computer";
        cout<<" 2. "<<playername[2];
        cout<<" "<<playername[1]<<", Chose your Symbol : X or O ";
        cin>>symbol[1];
        if(symbol[1] == 'X')
                symbol[2] = 'O';
        if(symbol[1] == 'O')
                symbol[2] = 'X';

        cout<<" "<<playername[2]<<", Your's Symbol : "<<symbol[2]<<" ";
        T.getPlay(playername);
        char ch;
        do{
                cout<<"Do you want play more (Y/y)? : ";
                cin>>ch;
                if(ch!= 'Y' && ch!= 'y')
                        return 0;
                T.clearScreen();
        }while(ch == 'Y' || ch == 'y');
}
   

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote