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

main.cpp #include \"tic.h\" #include <iostream> #include <string> using namespac

ID: 3831541 • Letter: M

Question

main.cpp

#include "tic.h"

#include <iostream>

#include <string>

using namespace std;

int main()

{

    tBoard myGame;

    cout << "Hello! Welcome to Tic-Tac-Toe" << endl;

   

    string player[2];

   

    cout << "What is the name of the player going first (X)?" << endl;

    getline(cin,player[0]);

    cout << player[0] << " is X " << endl;

   

    cout << "What is the name of the player going second (O)?" << endl;

    getline(cin,player[1]);

    cout << player[1] << " is O " << endl;

   

    int current_player=0;

    symbol piece = X;

    while(!myGame.game_over())

    {

        cout << myGame;

        cout << player[current_player] << " enter space to place ";

        cout << piece;

        cout << " as ROW COL then press enter" << endl;

        int row;

        int col;

        cin >> row;

        cin >> col;

        if( myGame.move(piece,row,col) )

        {

            cout << "Move Successful." << endl;

            current_player=(current_player + 1 )%2;

            piece++;

        }else

        {

            cout << "Move Failed, try again." << endl;

        }

    }

    cout << "Game Over" << endl;

    cout << myGame;

    switch(myGame.winner())

    {

        case(X):

            cout << "Congratulations " << player[0] << endl;

            break;

        case(O):

            cout << "Congratulations " << player[1] << endl;

            break;

        case(BLANK):

            cout << "TIE: Everyone Wins" << endl;

            break;

    }

    return 0;

}

symbol.cpp

#include "symbol.h"

#include <cassert>

std::ostream & operator<<(std::ostream& os, const symbol& my_symbol)

{

    switch(my_symbol)

    {

        case(X):

            os << "X";

            break;

        case(O):

            os << "O";

            break;

        default:

            os << " ";

            break;

    }

    return os;

}

symbol& operator++(symbol& my_symbol)

{

    switch(my_symbol)

    {

        case(X):

            my_symbol=O;

            break;

        case(O):

            my_symbol=X;

            break;

        default:

            assert(my_symbol==X || my_symbol==O);

            break;

    }

    return my_symbol;

}

symbol operator++(symbol & my_symbol,int)

{

    symbol old_symbol=my_symbol;

    ++my_symbol;

    return old_symbol;

}

symbol.h

#ifndef _TIC_SYMBOL_

#define _TIC_SYMBOL_

#include <iostream>

enum symbol {X,O,BLANK};

std::ostream & operator<<(std::ostream& os, const symbol& my_symbol);

symbol& operator++(symbol& my_symbol);

symbol operator++(symbol & my_symbol,int);

#endif

tic.h

#ifndef _TIC_TAC_TOE_

#define _TIC_TAC_TOE_

#include <vector>

#include <iostream>

#include <string>

#include "symbol.h"

using namespace std;

class tBoard

{

    private:

        //You can pick your own data structure

    public:

        //Default Constructor

        //Makes a board with all blank spaces

        tBoard();

        //Makes a move on the board

        //X is the row and y is the column

        //m is the symbol to place (either X or O)

        //It returns true if the move was made

        //If the move is illegal, return false and do not change the table

        bool move(symbol m, int x, int y);

        //Returns true if the game is over

        //This could be because of a winner or because of a tie

        bool game_over();

        //Returns who won X or O.

        //If the game was a tie, return BLANK

        symbol winner();

};

//Overload the output operator

ostream & operator<<(ostream& os, const tBoard& myTable);

#endif

In this problem you have no control over how the input/output with the user works. That has already been decided. You just need to figure out how to implement the back end to make design work.

Inputs:

Both Players enter their names so that the game can refer to them and declare a winner.

Moves are entered as two integers with a space between them on the same line. The first is the row number. The second is the column number. For example, 0 2 means row 0 column 2 should be marked.

Outputs:

Which symbol is assigned to each user is displayed.

After each move is entered the board is redrawn to show the move took place. The board must be drawn EXACTLY as shown in the sample run below.

If a players move was invalid, then they may attempt another move (see next section).

Error Checking:

If a user makes an illegal move (either space already taken or out of range) the move should fail and the user should be asked for another move, etc.., until a valid move is selected.

Game Mechanics:

The game only allows legal moves.

A winner should be declared immediately after the winning move is made.

A tie should be declared after the final move is made.

Full Rules of Tic-Tac-Toe can be found https://en.wikipedia.org/wiki/Tic-tac-toe

Programming Assignment:

You are provided with the following code. You may not change any of these files.

main.cpp - Handles printing output and getting input

symbol.cpp - Gives and Enum for the three symbols possible in spaces (blank, X, O)

symbol.h - H File for Enum

You are given an incomplete tic.h file. It includes the methods that are required by the main.cpp file.

You must complete the Tic-Tac-Toe program by implementing tic.cpp and completing tic.h. You may add new methods and attibutes to the class, but you may not change any of the methods given. You cannot change main.cpp, so any method used there must work as expected.

What to Submit:

main.cpp - Main Game Program

tic.h - Header for Game

tic.cpp - Implementation for Game

symbol.cpp - Enum for Symbols

symbol.h - Header for Symbols

Explanation / Answer

#include "tic.h"

#include <iostream>

#include <string>

using namespace std;

int main()

{

    tBoard myGame;

    cout << "Hello! Welcome to Tic-Tac-Toe" << endl;

   

    string player[2];

   

    cout << "What is the name of the player going first (X)?" << endl;

    getline(cin,player[0]);

    cout << player[0] << " is X " << endl;

   

    cout << "What is the name of the player going second (O)?" << endl;

    getline(cin,player[1]);

    cout << player[1] << " is O " << endl;

   

    int current_player=0;

    symbol piece = X;

    while(!myGame.game_over())

    {

        cout << myGame;

        cout << player[current_player] << " enter space to place ";

        cout << piece;

        cout << " as ROW COL then press enter" << endl;

        int row;

        int col;

        cin >> row;

        cin >> col;

        if( myGame.move(piece,row,col) )

        {

            cout << "Move Successful." << endl;

            current_player=(current_player + 1 )%2;

            piece++;

        }else

        {

            cout << "Move Failed, try again." << endl;

        }

    }

    cout << "Game Over" << endl;

    cout << myGame;

    switch(myGame.winner())

    {

        case(X):

            cout << "Congratulations " << player[0] << endl;

            break;

        case(O):

            cout << "Congratulations " << player[1] << endl;

            break;

        case(BLANK):

            cout << "TIE: Everyone Wins" << endl;

            break;

    }

    return 0;

}

symbol.cpp

#include "symbol.h"

#include <cassert>

std::ostream & operator<<(std::ostream& os, const symbol& my_symbol)

{

    switch(my_symbol)

    {

        case(X):

            os << "X";

            break;

        case(O):

            os << "O";

            break;

        default:

            os << " ";

            break;

    }

    return os;

}

symbol& operator++(symbol& my_symbol)

{

    switch(my_symbol)

    {

        case(X):

            my_symbol=O;

            break;

        case(O):

            my_symbol=X;

            break;

        default:

            assert(my_symbol==X || my_symbol==O);

            break;

    }

    return my_symbol;

}

symbol operator++(symbol & my_symbol,int)

{

    symbol old_symbol=my_symbol;

    ++my_symbol;

    return old_symbol;

}