In this problem you have no control over how the input/output with the user work
ID: 3834131 • Letter: I
Question
In this problem you have no control over how the input/output with the user works 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:
tic.h - Header for Game
tic.cpp - Implementation for Game
______________________________________
symbol.h
#ifndef _TIC_SYMBOL_
#define _TIC_SYMBOL_
//You may NOT change this file.
#include
//Each Space is marked with a symbol or blank
enum symbol {X,O,BLANK};
//Output Operator
std::ostream & operator<<(std::ostream& os, const symbol& my_symbol);
//Prefix ++a increment
symbol& operator++(symbol& my_symbol);
//Postfix a++ increment
symbol operator++(symbol & my_symbol,int);
#endif
______________________________________
tic.h
//You may add new methods (public or private) and new private attributes.
//You may NOT remove or change any methods given.
#ifndef _TIC_TAC_TOE_
#define _TIC_TAC_TOE_
#include
#include
#include
#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
______________________________________
main.cpp
//You may NOT change this file.
//All coding MUST be done in the tic.h and tic.cpp.
#include "tic.h"
#include
#include
using namespace std;
int main()
{
//Use the default constructor to create a new empty game.
tBoard myGame;
cout << "Hello! Welcome to Tic-Tac-Toe" << endl;
//Create an array to hold my 2 values
string player[2];
//Get the Payer Names
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;
//Player 1 goes first
//They are playing X
int current_player=0;
symbol piece = X;
//Keep Going Until the Game is over
while(!myGame.game_over())
{
//Draw the Board
cout << myGame;
//Ask the user for their move
cout << player[current_player] << " enter space to place ";
cout << piece;
cout << " as ROW COL then press enter" << endl;
//Read in the move
int row;
int col;
cin >> row;
cin >> col;
//Check if the move is valid
//Only go to the next player on valid moves
if( myGame.move(piece,row,col) )
{
cout << "Move Successful." << endl;
current_player=(current_player + 1 )%2;
piece++;
}else
{
cout << "Move Failed, try again." << endl;
}
}
//The Game is over. Find out who won.
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 to Exit
return 0;
}
______________________________________
symbols.cpp
//You may NOT change this file.
//All coding MUST be done in the tic.h and tic.cpp.
#include "symbol.h"
#include
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;
}
//Change the value and return the updated value
symbol& operator++(symbol& my_symbol)
{
switch(my_symbol)
{
case(X):
my_symbol=O;
break;
case(O):
my_symbol=X;
break;
//You can't increment to a blank!
default:
assert(my_symbol==X || my_symbol==O);
break;
}
return my_symbol;
}
//Post a++ increment
symbol operator++(symbol & my_symbol,int)
{
//The postfix a++ operator returns
//the old value (before increment happened)
symbol old_symbol=my_symbol;
++my_symbol;
return old_symbol;
}
Explanation / Answer
Here are the completed tic.h and tic.cpp files for the game. Please don't forget to rate the answer if it helped. Thank you very much.
tic.h
//You may add new methods (public or private) and new private attributes.
//You may NOT remove or change any methods given.
#ifndef _TIC_TAC_TOE_
#define _TIC_TAC_TOE_
//#include
//#include
#include <iostream>
#include "symbol.h"
using namespace std;
class tBoard
{
private:
//You can pick your own data structure
enum symbol board[3][3];
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();
symbol get(int x, int y) const;
};
//Overload the output operator
ostream & operator<<(ostream& os, const tBoard& myTable);
#endif
tic.cpp
#include "tic.h"
tBoard::tBoard()
{
for(int i=0 ;i<3; i++)
{
for(int j=0; j<3; j++)
board[i][j] = BLANK;
}
}
bool tBoard::move(symbol m, int x, int y)
{
if(x>=0 && x<=3 && y>=0 && y<=3 && board[x][y]==BLANK)
{
board[x][y]=m;
return true;
}
else
return false;
}
bool tBoard::game_over()
{
if(winner() == BLANK)
return false;
else
return true;
}
symbol tBoard::winner()
{
if(board[0][0]== board[0][1] && board[0][0]==board[0][2]) //check top horizontal
return board[0][0];
else if(board[1][0]== board[1][1] && board[1][0]==board[1][2]) //check middle horizontal
return board[1][0];
else if(board[2][0]== board[2][1] && board[2][0]==board[2][2]) //check bottom horizontal
return board[2][0];
if(board[0][0]== board[1][0] && board[0][0]==board[2][0]) //check left vertical
return board[0][0];
else if(board[0][1]== board[1][1] && board[0][1]==board[2][1]) //check middle vertical
return board[0][1];
else if(board[0][2]== board[1][2] && board[0][2]==board[2][2]) //check right vertictal
return board[0][2];
else if(board[0][0]== board[1][1] && board[0][0]==board[2][2]) //check diagonal
return board[0][0];
else if(board[0][2]== board[1][1] && board[0][2]==board[2][0]) //check diagonal
return board[0][2];
return BLANK;
}
symbol tBoard::get(int x, int y) const
{
return board[x][y];
}
ostream & operator<<(ostream& os, const tBoard& myTable)
{
for(int i=0; i <3; i++)
{
os<<" ";
for(int j=0; j<3; j++)
os<<" "<<"["<<myTable.get(i,j)<<"]";
}
os<<" ";
return os;
}
output
Hello! Welcome to Tic-Tac-Toe
What is the name of the player going first (X)?
bob
bob is X
What is the name of the player going second (O)?
alice
alice is O
[ ] [ ] [ ]
[ ] [ ] [ ]
[ ] [ ] [ ]
bob enter space to place X as ROW COL then press enter
0 0
Move Successful.
[X] [ ] [ ]
[ ] [ ] [ ]
[ ] [ ] [ ]
alice enter space to place O as ROW COL then press enter
1 1
Move Successful.
[X] [ ] [ ]
[ ] [O] [ ]
[ ] [ ] [ ]
bob enter space to place X as ROW COL then press enter
0 5
Move Failed, try again.
[X] [ ] [ ]
[ ] [O] [ ]
[ ] [ ] [ ]
bob enter space to place X as ROW COL then press enter
1 2
Move Successful.
[X] [ ] [ ]
[ ] [O] [X]
[ ] [ ] [ ]
alice enter space to place O as ROW COL then press enter
2 0
Move Successful.
[X] [ ] [ ]
[ ] [O] [X]
[O] [ ] [ ]
bob enter space to place X as ROW COL then press enter
1 0
Move Successful.
[X] [ ] [ ]
[X] [O] [X]
[O] [ ] [ ]
alice enter space to place O as ROW COL then press enter
0 2
Move Successful.
Game Over
[X] [ ] [O]
[X] [O] [X]
[O] [ ] [ ]
Congratulations alice
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.