#include <iostream> #include <iomanip> #include \"TicTacToe.h\" using namespace
ID: 3635215 • Letter: #
Question
#include <iostream>#include <iomanip>
#include "TicTacToe.h"
using namespace std;
TicTacToe::TicTacToe() {
for (int j = 0; j < 3; j++)
for (int k = 0; k < 3; k++)
board[j][k] = ' ';
}
bool TicTacToe::validMove(int r, int c) {
return r >= 0 && r < 3 && c >= 0 && c < 3 && board[r][c] == ' ';
}
TicTacToe::Status TicTacToe::gameStatus() {
int a;
if (board[0][0] != ' ' && board[0][0] == board[1][1] && board[0][0]
== board[2][2])
return WIN;
else if (board[2][0] != ' ' && board[2][0] == board[1][1] && board[2][0]
== board[0][2])
return WIN;
for (a = 0; a < 3; ++a)
if (board[a][0] != ' ' && board[a][0] == board[a][1] && board[a][0]
== board[a][2])
return WIN;
for (a = 0; a < 3; ++a)
if (board[0][a] != ' ' && board[0][a] == board[1][a] && board[0][a]
== board[2][a])
return WIN;
for (int r = 0; r < 3; ++r)
for (int c = 0; c < 3; ++c)
if (board[r][c] == ' ')
return CONTINUE;
return DRAW;
}
void TicTacToe::printBoard() {
cout << "012 ";
for (int r = 0; r < 3; ++r) {
cout << r;
for (int c = 0; c < 3; ++c) {
cout << setw(3) << static_cast<char> (board[r][c]);
if (c != 2)
cout << " |";
}
if ( r != 2 )
cout << " ____|____|____ ";
}
cout << " ";
}
void TicTacToe::makeMove() {
printBoard();
while (true) {
if (xoMove('X'))
break;
else if (xoMove('O'))
break;
}
}
bool TicTacToe::xoMove(int symbol) {
int x;
int y;
do {
cout << "Player " << static_cast<char> (symbol) << " enter move: ";
cin >> x >> y;
cout << ' ';
} while (!validMove(x, y));
board[x][y] = symbol;
printBoard();
Status xoStatus = gameStatus();
if (xoStatus == WIN) {
cout << "Player " << static_cast<char> (symbol) << " wins! ";
return true;
} else if (xoStatus == DRAW) {
cout << "Game is a draw. ";
return true;
} else
return false;
}
Explanation / Answer
#include #include void Board(); void PlayerX(); void PlayerO(); void Player_win(); void check(); int win=0,wrong_X=0,wrong_O=0,chk=0; char name_X[30]; char name_O[30]; int pos_for_X[3][3]; int pos_for_O[3][3]; int pos_marked[3][3]; void main() { int i,ch,j; char ans; /* clrscr(); printf(" TIC TAC TOE"); printf(" "); for(i=1;iRelated Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.