#include <iostream> using namespace std; class ticTacToe { public: ticTacToe();
ID: 3736817 • Letter: #
Question
#include <iostream>
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 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.
Explanation / Answer
#include <iostream>
#include<fstream>
#include<cstdlib>
using namespace std;
class ticTacToe
{
public:
ticTacToe();
ticTacToe(int);
void setBoard();
void printBoard();
bool determineWinner();
void moves();
bool validateMove();
void play();
private:
char **board;
int counter;
int row;
int column;
char ch;
// store the size of board
int size;
};
//constructor
ticTacToe::ticTacToe()
{
// create board of size nxn
this->board = (char **)malloc(3 * sizeof(char *));
int i;
for( i = 0 ; i < 3 ; i++ )
this->board[i] = (char *)malloc(3 * sizeof(char));
this->size = 3;
setBoard();
}
//constructor
// size is the size of 2d array read from input.txt
ticTacToe::ticTacToe(int size)
{
// create board of size nxn
this->board = (char **)malloc(size * sizeof(char *));
int i;
for( i = 0 ; i < size ; i++ )
this->board[i] = (char *)malloc(size * sizeof(char));
this->size = size;
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()
{
int size;
// open file to read the size of board
ifstream in("input.txt");
// read integer from file
in>>size;
cout<<"size : "<<size<<endl<<endl;
ticTacToe t(size);
cout << "Tic Tac Toe:" << endl;
t.play();
return 0;
}
------------------------------input.txt---------------------------------
3
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.