I am making a TicTacToe game (fairly easy I know but I am a beginner) and I am almost done, my problem now is I am trying to have it check if a row, a column or a diagonal of the table have the same value, and recursively keep playing if it is not. My board looks like this :
char xToken = 'x';
char oToken = 'o';
char vertical = '|';
char horizontal = '-';
class Board {
public:
const static int rows = 6;
const static int cols = 6;
char grid[rows][cols]{};
Board() {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
grid[i][0] = '0' + i;
grid[0][j] = '0' + j;
if (j == 2 || j == 4) {
grid[i][j] = vertical;
}
if (i == 2 || i == 4) {
grid[i][j] = horizontal;
}
}
}
}
void printBoard() {
for (auto & i : grid) {
for (char j : i) {
cout << j << "t";
}
cout << endl;
}
}
}
This is my play() function:
void play() {
int i = 0;
int j = 0;
cout << "choose index you want to play in" << endl;
cout << "Player 1" << endl;
cout << "Row: " << endl;
cin >> i;
cout << "Column: " << endl;
cin >> j;
cout << i << ", " << j << endl;
if (Board().grid[i][j] != vertical && Board().grid[i][j] != horizontal) {
grid[i][j] = xToken;
printBoard();
}
cout << "choose index you want to play in" << endl;
cout << "Player 2" << endl;
cout << "Row: " << endl;
cin >> i;
cout << "Column: " << endl;
cin >> j;
cout << i << ", " << j << endl;
if (Board().grid[i][j] != vertical && Board().grid[i][j] != horizontal) {
grid[i][j] = oToken;
printBoard();
}
if(grid[i][j] == xToken) {
cout << "You Win!" << endl;
} else
play();
}
The idea is I am trying to recursively keep playing if we don’t have 3 elements in a row, column or diagonal that are the same value. The very problem now is to find how to read the board so it can find the 3 in a row so the game stops
Pelz04 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.