In: Computer Science
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]);
How it should look:
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!
// 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.
C++ Program:
#include <iostream>
using namespace std;
// input prompts the user for a board and inputs it
// into the given array
void input (char board [3][3])
{
int i, j;
char ch;
//Prompting user
cout << "\n Enter your board as characters
(x, o or .): \n";
//Outer loop for rows
for(i=0; i<3; i++)
{
//Inner loop for
columns
for(j=0; j<3;
j++)
{
//Reading character
cin >> ch;
//Adding character to board
if(ch == '.')
{
board[i][j] = ' ';
}
else
{
board[i][j] = ch;
}
}
}
}
// print prints the board in nice format
void print (char board [3][3])
{
int i, j;
cout << "\n\n The board is: \n";
cout << "\n |---|---|---| \n";
//Outer loop for rows
for(i=0; i<3; i++)
{
//Inner loop for
columns
for(j=0; j<3;
j++)
{
//Printing character
cout << " | " << board[i][j];
}
cout << " |";
//Printing
footer
cout << "\n
|---|---|---| \n";
}
}
// 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)
{
return (board[0][0] == player &&
board[0][1] == player && board[0][2] == player) ||
(board[1][0] ==
player && board[1][1] == player && board[1][2] ==
player) ||
(board[2][0] ==
player && board[2][1] == player && board[2][2] ==
player) ||
(board[0][0] ==
player && board[1][0] == player && board[2][0] ==
player) ||
(board[0][1] ==
player && board[1][1] == player && board[2][1] ==
player) ||
(board[0][2] ==
player && board[1][2] == player && board[2][2] ==
player) ||
(board[0][0] ==
player && board[1][1] == player && board[2][2] ==
player) ||
(board[0][2] ==
player && board[1][1] == player && board[2][0] ==
player);
}
//Main function
int main()
{
char userChoice;
//Board
char board[3][3];
//Loop till user wants to quit
do
{
//Reading board
input(board);
//Printing
board
print(board);
//Deciding
winner
if(win(board,
'x'))
{
cout << "\n X wins! \n";
}
else if(win(board,
'o'))
{
cout << "\n o wins! \n";
}
else
{
cout << "\n No Winner! \n";
}
//Prompting
user
cout << "\n\n
Would you like to do another (y or n)? ";
cin >>
userChoice;
}while(userChoice == 'y' || userChoice == 'Y');
cout << "\n Thanks for playing!
\n";
return 0;
}
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Sample Output:

