Write a C++ program, tictac.cpp , that repeatedly reads in tic-tac-toe boards an
ID: 3794400 • Letter: W
Question
Write a C++ program, tictac.cpp, that repeatedly reads in tic-tac-toe boards and for each one indicates if there is a winner and who won.
The board will be read in as 3x3 array of characters (x, o or . for a blank spot).
You are required to write the following functions to help solve the problem:
// input prompts the user for a board and inputs it
// into the given array
void input (char board [3][3]);
// print prints the board in nice format
void print (char board [3][3]);
// win returns true if the given player has won on the
// given board, else it returns false
bool win (char board [3][3], char player);
You are required to use nested loops to write the functions input and print.
The back of this page shows a sample run of the program (input in bold). Your output should look at least as nice as that shown. You may assume that the board will be legal and there will be at most one winner.
Enter your board as characters (x, o or .):
o.x
..o
.x.
The board is:
o | | x
-----------
| | o
-----------
| x |
No winner!
Would you like to do another (y or n)? y
Enter your board as characters (x, o or .):
oxo
ox.
.xo
The board is:
o | x | o
-----------
o | x |
-----------
| x | o
X wins!
Would you like to do another (y or n)? y
Enter your board as characters (x, o or .):
xoo
xox
ox.
The board is:
x | o | o
-----------
x | o | x
-----------
o | x |
O wins!
Would you like to do another (y or n)? n
Thanks for playing!
Explanation / Answer
#include #include using namespace std; class TicTacToe{ public: void print(); void play(); char determine(); TicTacToe(); private: int p1x,p1y; int p2x,p2y; char TicTacToeBoard[3][3]; }; TicTacToe::TicTacToe(){ char TicTacToeBoard[3][3] = {{'!','!','!'},{'!','!','!'},{'!','!','!'} }; // fill board TicTacToeBoard[p1x][p1y] = 'X'; // put x where p1 has coordinates TicTacToeBoard[p2x][p2y] = 'O'; // put y where p1 has coordinates } int main(){ TicTacToe playgame,winner; //create objects of type TicTacToe playgame.play(); // play game winner.determine(); // determine winner system("pause"); return 0; } void TicTacToe::print(){ // for printing board int row; int col; coutRelated Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.