C++ tic tac toe help The main() and function headers are provided. Please implem
ID: 3909582 • Letter: C
Question
C++ tic tac toe help
The main() and function headers are provided. Please implement the functions, can add new ones, and do not change the main().
2. Your program must produce identical output: Part1_Run1.txt and Part1_Run2.txt
-Code
#include
using namespace std;
bool isWon(char, char[][3]);
bool isDraw(char[][3]);
void displayBoard(char[][3]);
void makeAMove(char[][3], char);
int main() {
//
// PLEASE DO NOT CHANGE main()
//
char board[3][3] = { { ' ', ' ', ' ' },{ ' ', ' ', ' ' },{ ' ', ' ', ' ' } };
displayBoard(board);
while (true) {
// The first player makes a move
makeAMove(board, 'X');
displayBoard(board);
if (isWon('X', board)) {
cout << "X player won" << endl;
exit(0);
}
else if (isDraw(board)) {
cout << "No winner" << endl;
exit(0);
}
// The second player makes a move
makeAMove(board, 'O');
displayBoard(board);
if (isWon('O', board)) {
cout << "O player won" << endl;
exit(0);
}
else if (isDraw(board)) {
cout << "No winner" << endl;
exit(0);
}
}
return 0;
}
- Output needed
-------------
| | | |
-------------
| | | |
-------------
| | | |
-------------
Enter a row (0, 1, 2) for player X : 0
Enter a column (0, 1, 2) for player X: 0
-------------
| X | | |
-------------
| | | |
-------------
| | | |
-------------
Enter a row (0, 1, 2) for player O : 0
Enter a column (0, 1, 2) for player O: 0
This cell is already occupied. Try a different cell
Enter a row (0, 1, 2) for player O : 0
Enter a column (0, 1, 2) for player O: 1
-------------
| X | O | |
-------------
| | | |
-------------
| | | |
-------------
Enter a row (0, 1, 2) for player X : 1
Enter a column (0, 1, 2) for player X: 1
-------------
| X | O | |
-------------
| | X | |
-------------
| | | |
-------------
Enter a row (0, 1, 2) for player O : 2
Enter a column (0, 1, 2) for player O: 2
-------------
| X | O | |
-------------
| | X | |
-------------
| | | O |
-------------
Enter a row (0, 1, 2) for player X : 1
Enter a column (0, 1, 2) for player X: 0
-------------
| X | O | |
-------------
| X | X | |
-------------
| | | O |
-------------
Enter a row (0, 1, 2) for player O : 2
Enter a column (0, 1, 2) for player O: 0
-------------
| X | O | |
-------------
| X | X | |
-------------
| O | | O |
-------------
Enter a row (0, 1, 2) for player X : 1
Enter a column (0, 1, 2) for player X: 2
-------------
| X | O | |
-------------
| X | X | X |
-------------
| O | | O |
-------------
X player won
Press any key to continue . . .
- Output 2
-------------
| | | |
-------------
| | | |
-------------
| | | |
-------------
Enter a row (0, 1, 2) for player X : 1
Enter a column (0, 1, 2) for player X: 1
-------------
| | | |
-------------
| | X | |
-------------
| | | |
-------------
Enter a row (0, 1, 2) for player O : 0
Enter a column (0, 1, 2) for player O: 2
-------------
| | | O |
-------------
| | X | |
-------------
| | | |
-------------
Enter a row (0, 1, 2) for player X : 2
Enter a column (0, 1, 2) for player X: 2
-------------
| | | O |
-------------
| | X | |
-------------
| | | X |
-------------
Enter a row (0, 1, 2) for player O : 0
Enter a column (0, 1, 2) for player O: 0
-------------
| O | | O |
-------------
| | X | |
-------------
| | | X |
-------------
Enter a row (0, 1, 2) for player X : 2
Enter a column (0, 1, 2) for player X: 0
-------------
| O | | O |
-------------
| | X | |
-------------
| X | | X |
-------------
Enter a row (0, 1, 2) for player O : 2
Enter a column (0, 1, 2) for player O: 1
-------------
| O | | O |
-------------
| | X | |
-------------
| X | O | X |
-------------
Enter a row (0, 1, 2) for player X : 1
Enter a column (0, 1, 2) for player X: 0
-------------
| O | | O |
-------------
| X | X | |
-------------
| X | O | X |
-------------
Enter a row (0, 1, 2) for player O : 1
Enter a column (0, 1, 2) for player O: 2
-------------
| O | | O |
-------------
| X | X | O |
-------------
| X | O | X |
-------------
Enter a row (0, 1, 2) for player X : 0
Enter a column (0, 1, 2) for player X: 1
-------------
| O | X | O |
-------------
| X | X | O |
-------------
| X | O | X |
-------------
No winner
Press any key to continue . . .
Explanation / Answer
#include<iostream>
#include<stdlib.h>
using namespace std;
bool isWon(char, char[][3]);
bool isDraw(char[][3]);
void displayBoard(char[][3]);
void makeAMove(char[][3], char);
int main() {
//
// PLEASE DO NOT CHANGE main()
//
char board[3][3] = { { ' ', ' ', ' ' },{ ' ', ' ', ' ' },{ ' ', ' ', ' ' } };
displayBoard(board);
while (true) {
// The first player makes a move
makeAMove(board, 'X');
displayBoard(board);
if (isWon('X', board)) {
cout << "X player won" << endl;
exit(0);
}
else if (isDraw(board)) {
cout << "No winner" << endl;
exit(0);
}
// The second player makes a move
makeAMove(board, 'O');
displayBoard(board);
if (isWon('O', board)) {
cout << "O player won" << endl;
exit(0);
}
else if (isDraw(board)) {
cout << "No winner" << endl;
exit(0);
}
}
return 0;
}
bool isWon(char a, char board[][3]){
for (int i = 0; i<3; i++){
if (board[i][0] == a && board[i][1] == a && board[i][2] == a)
return true;
if (board[0][i] == a && board[1][i] == a && board[2][i] == a)
return true;
}
if (board[0][0] == a && board[1][1] == a && board[2][2] == a)
return true;
if (board[0][2] == a && board[1][1] == a && board[2][0] == a)
return true;
return false;
}
bool isDraw(char board[][3]){
int count = 0;
for(int i = 0; i<3; i++){
for (int j = 0; j<3; j++){
if (board[i][j] == 'X' || board[i][j] == 'O')
count++;
}
}
if (count != 9)
return false;
if (!isWon('X', board) && !isWon('O',board))
return true;
}
void displayBoard(char board[][3]){
cout << "------------- ";
for (int i = 0; i<3; i++){
cout << "|";
for (int j = 0; j<3; j++){
cout << board[i][j] << "|";
}
cout << endl;
cout << "------------- ";
}
}
void makeAMove(char board[][3], char a){
while(true){
cout << "Enter a row(0,1,2) for player " << a << ":";
int b;
cin >> b;
int c;
cout << "Enter a column(0,1,2) for player " << a << ":";
cin >> c;
if ( b < 0 || b > 2 || c < 0 || c > 2){
cout << "Invalid Move ";
}
else {
if (board[b][c] != 'X' && board[b][c] != 'O'){
board[b][c] = a;
break;
}
else {
cout << "Invalid Move ";
}
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.