Assignment Specifications implement a console version of the

Assignment Specifications

implement a console version of the game tic-tac-toe in c++. For this assignment, we are providing an initial source code file which contains skeleton code that you must complete. We also provide complete functions for you to utilize. You are not allowed to change the provided functions and you are not allowed to change the headers of the provided function stubs.

For the functions you must implement, we have provided only a stub. A stub is a function definition that compiles, but does not yet implement the complete specifications for that function. As you develop the program, you should implement each function one at a time and test each as you go.

Implementation Strategies

We provide some variables and two global constants for you to utilize.

We provide string literals for winning or tie game output in comments with provided file

We have also provided comments to help you develop the necessary algorithm for 2 users playing the game of tic-tac-toe on a computer. Use these comments along with the function descriptions below to help you develop your program. One or more lines of your code should exist below each comment. Remove the TODO part when you have completed that step.

DO NOT try to implement the entire game at once. Instead, implement one behavior at a time, only developing one particular function at a time. Functions are listed below.

We highly recommend you unit test the function you are currently developing. You should understand how to walk through your code by hand as well as executing it in unit tests.

Use one of the following statements when stating who won:

Player 1 (x\'s) wins!

Player 2 (o\'s) wins!

No one wins

Functions

/// @brief Fills vector with characters starting at lower case a.

///

///     If the vector is size 3 then it will have characters a to c.

///     If the vector is size 5 then it will have characters a to e.

///     If the vector is size 26 then it will have characters a to z.

///

/// @param v the vector to initialize

/// @pre-condition the vector size will never be over 26

void initVector(vector <char> &v)

/// @brief Converts a character representing a cell to associated vector index

/// @param the position to be converted to a vector index

/// @return the integer index in the vector, should be 0 to (vector size - 1)

int convertPosition(char position)

/// @brief Predicate function to determine if a spot in board is available.

/// @param board the current tic-tac-toe board

/// @param position is an index into vector to check if available

/// @return true if position\'s state is available (not marked) AND is in bounds

bool validPlacement(const vector <char> &board, int position)

/// @brief Predicate function to determine if the game has been won

///

///     Winning conditions in tic-tac-toe require three marks from same

///     player in a single row, column or diagonal.

///

/// @param board the current tic-tac-toe board

/// @return true if the game has been won, false otherwise

bool gameWon(const vector <char> &board)

/// @brief Predicate function to determine if the board is full

/// @param board the current tic-tac-toe board

/// @return true iff the board is full (no cell is available)

bool boardFull(const vector <char> &board)

/// @brief Acquires a play from the user as to where to put her mark

///

///     Utilizes convertPosition and validPlacement functions to convert the

///     user input and then determine if the converted input is a valid play.

///

/// @param board the current tic-tac-toe board

/// @return an integer index in board vector of a chosen available board spot

int getPlay(const vector <char> &board)

Tie Game Example (user input is bolded and underlined for emphasis)

c

a | b | c

-----|-----|-----

d | e | f

-----|-----|-----

g | h | i

Please choose a position: a

c

x | b | c

-----|-----|-----

d | e | f

-----|-----|-----

g | h | i

Please choose a position: e

c

x | b | c

-----|-----|-----

d | o | f

-----|-----|-----

g | h | i

Please choose a position: e

Please choose a position: b

c

x | x | c

-----|-----|-----

d | o | f

-----|-----|-----

g | h | i

Please choose a position: c

c

x | x | o

-----|-----|-----

d | o | f

-----|-----|-----

g | h | i

Please choose a position: g

c

x | x | o

-----|-----|-----

d | o | f

-----|-----|-----

x | h | i

Please choose a position: d

c

x | x | o

-----|-----|-----

o | o | f

-----|-----|-----

x | h | i

Please choose a position: f

c

x | x | o

-----|-----|-----

o | o | x

-----|-----|-----

x | h | i

Please choose a position: h

c

x | x | o

-----|-----|-----

o | o | x

-----|-----|-----

x | o | i

Please choose a position: i

c

x | x | o

-----|-----|-----

o | o | x

-----|-----|-----

x | o | x

No one wins

Player 1 (x\'s) wins!

Player 2 (o\'s) wins!

No one wins

Solution

#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 << \"\\033c\";
        }
        cout<<\"\ \\t\\t **** 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 <<\"\\t\\t \"<<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<<\"\\t\\t\\tInvalid move \";

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

                player++;
        }while(i==-1);
        drawBoard(playername);
        if(i==1)
                cout<<\"\\a\\t\\t\\tPlayer \"<<playername[--player]<<\" win \";
        else
                cout<<\"\\a\\t\\t\\tGame 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 << \"\ \ \\t\\t\\t   Tic Tac Toe\ \ \";

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

        cout << \"\\t\\t\\t_____|_____|_____\" << endl;
        cout << \"\\t\\t\\t     |     |     \" << endl;

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

        cout << \"\\t\\t\\t_____|_____|_____\" << endl;
        cout << \"\\t\\t\\t     |     |     \" << endl;

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

        cout << \"\\t\\t\\t     |     |     \" << endl << endl;

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

        cout<<\"\\t\\t\"<<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\');
}

Assignment Specifications implement a console version of the game tic-tac-toe in c++. For this assignment, we are providing an initial source code file which co
Assignment Specifications implement a console version of the game tic-tac-toe in c++. For this assignment, we are providing an initial source code file which co
Assignment Specifications implement a console version of the game tic-tac-toe in c++. For this assignment, we are providing an initial source code file which co
Assignment Specifications implement a console version of the game tic-tac-toe in c++. For this assignment, we are providing an initial source code file which co
Assignment Specifications implement a console version of the game tic-tac-toe in c++. For this assignment, we are providing an initial source code file which co
Assignment Specifications implement a console version of the game tic-tac-toe in c++. For this assignment, we are providing an initial source code file which co
Assignment Specifications implement a console version of the game tic-tac-toe in c++. For this assignment, we are providing an initial source code file which co
Assignment Specifications implement a console version of the game tic-tac-toe in c++. For this assignment, we are providing an initial source code file which co

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site