Question
#include using namespace std; class ticTacToe { public: ticTacToe(); void setBoard(); void printBoard(); bool determineWinner(); void moves(); bool validateMove(); void play(); private: char board[3][3]; int
#include
class ticTacToe { public: ticTacToe(); void setBoard(); void printBoard(); bool determineWinner(); void moves(); bool validateMove(); void play();
private:
char board[3][3]; int counter; int row; int column; char ch;
};
//constructor ticTacToe::ticTacToe() { setBoard(); }
void ticTacToe::setBoard() { counter=0;
for (int i=0; i<3;i++) { for (int j=0; j<3;j++) { board[i][j] = 0; } } }
void ticTacToe::printBoard() { for (int i=0; i<3;i++) { for (int j=0; j<3; j++) { cout << board[i][j]; } cout << endl; } }
void ticTacToe::moves() { cout << "Player" << counter%2+1; cout << " Enter row:" << endl; cin >> row; cout << " Enter column:" << endl; cin >> column; }
bool ticTacToe::validateMove() { if ((row<1||row>3)||(column<1||column>3)||(board[row-1][column-1]!=0)) { cout << "Invalid entry" << endl; return false; } else { return true; } }
bool ticTacToe::determineWinner() {
if (counter%2==0) { ch = 'X'; } else { ch = 'O'; }
if (validateMove()) { board[row-1][column-1] = ch; counter ++; }
if (((board[0][0]==ch)&&(board[0][1]==ch)&&(board[0][2]==ch))|| //check every row ((board[1][0]==ch)&&(board[1][1]==ch)&&(board[1][2]==ch))|| ((board[2][0]==ch)&&(board[2][1]==ch)&&(board[2][2]==ch))|| ((board[0][0]==ch)&&(board[1][0]==ch)&&(board[2][0]==ch))|| //check every column ((board[0][1]==ch)&&(board[1][1]==ch)&&(board[2][1]==ch))|| ((board[0][2]==ch)&&(board[1][2]==ch)&&(board[2][2]==ch))|| ((board[0][0]==ch)&&(board[1][1]==ch)&&(board[2][2]==ch))|| //check diagonal one ((board[0][2]==ch)&&(board[1][1]==ch)&&(board[2][0]==ch))) //check diagonal two { return true; }
else if (counter==9) { cout << "Draw."<< endl; printBoard(); setBoard(); }
return false; }
void ticTacToe::play() { setBoard(); do { moves();
if (determineWinner()) { if (counter%2==0) { cout << "Player 2 won the game." << endl; cout << endl; printBoard(); } else { cout << "Player 1 won the game." << endl; cout << endl; printBoard(); } break; } else { printBoard(); } } while (true); }
int main() { ticTacToe t; cout << "Tic Tac Toe:" << endl; t.play(); return 0; }
How would I input the value of 3 from a file named "input.txt" as the size of the 2d array?
Please see code above and the bold part.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started