I am writing a Tic-Tac-Toe game using C++. I created a 2D array for the board. The issue I am experiencing is when the user is picking a position to place their piece(X or O). When the user selects any letter in either the first row or first column, the program would just end. If, for example, the user picks the middle cell, it would correctly place the user's piece. Any help would be appreciated. Thanks!
#include <iostream>
using namespace std;
char board[3][3] = { { 'A', 'B', 'C' }, { 'D', 'E', 'F' }, { 'G', 'H', 'I' } };
char player1 = 'X', player2 = 'O';
//displays board
void displayBoard() {
cout << "_________________" << endl;
cout << "| | | |" << endl;
cout << "| " << board[0][0] << " | " << board[0][1] << " | "
<< board[0][2] << " |" << endl;
cout << "|____|_____|____|" << endl;
cout << "| | | |" << endl;
cout << "| " << board[1][0] << " | " << board[1][1] << " | "
<< board[1][2] << " |" << endl;
cout << "|____|_____|____|" << endl;
cout << "| | | |" << endl;
cout << "| " << board[2][0] << " | " << board[2][1] << " | "
<< board[2][2] << " |" << endl;
cout << "| | | |" << endl;
cout << "|_______________|" << endl;
}
//check if game is over
bool checkStatus(char piece) {
bool over = false;
if ((board[0][0] == piece) && (board[0][1] = piece)
&& (board[0][2] = piece)) {
over = true;
} else if ((board[1][0] == piece) && (board[1][1] = piece) && (board[1][2] =
piece)) {
over = true;
} else if ((board[2][0] == piece) && (board[2][1] = piece) && (board[2][2] =
piece)) {
over = true;
} else if ((board[0][0] == piece) && (board[1][0] = piece) && (board[2][0] =
piece)) {
over = true;
} else if ((board[0][1] == piece) && (board[1][1] = piece) && (board[2][1] =
piece)) {
over = true;
} else if ((board[0][2] == piece) && (board[1][2] = piece) && (board[2][2] =
piece)) {
over = true;
} else if ((board[0][0] == piece) && (board[1][1] = piece) && (board[2][2] =
piece)) {
over = true;
} else if ((board[0][2] == piece) && (board[1][1] = piece) && (board[2][0] =
piece)) {
over = true;
} else {
over = false;
}
return over;
}
//gameplay
void game(char player1, char player2, bool current) {
bool turn = current; //true:player 1; false:player2
int row = 0, column = 0;
char position;
while (checkStatus(player1) != true && checkStatus(player2) != true) {
//player 1
if (turn != false) {
std::cout << "Player 1 Turn. Choose a Letter" << endl;
}
//player2
else if (turn != true) {
std::cout << "Player 2 Turn. Choose a Letter" << endl;
}
displayBoard();
std::cin >> position;
position = toupper(position);
switch (position) {
case 'A':
row = (position - 'A') / 3;
column = (position - 'A') % 3;
break;
case 'B':
row = 0;
column = 1;
break;
case 'C':
row = 0;
column = 2;
break;
case 'D':
row = 1;
column = 0;
break;
case 'E':
row = 1;
column = 1;
break;
case 'F':
row = 1;
column = 2;
break;
case 'G':
row = 2;
column = 0;
break;
case 'H':
row = 2;
column = 1;
break;
case 'I':
row = 2;
column = 2;
break;
default:
cout << "You didn't enter a correct letter! Try again\n";
game(player1, player2, turn);
}
cin.clear();
//player X
if (turn != false && board[row][column] != 'X'
&& board[row][column] != 'O') {
board[row][column] = 'X';
turn = false;
checkStatus(player1);
checkStatus(player2);
}
//player O
else if (turn != true && board[row][column] != 'X'
&& board[row][column] != 'O') {
board[row][column] = 'O';
turn = true;
checkStatus(player1);
checkStatus(player2);
} else {
cout << "Choose a valid cell!";
game(player1, player2, turn);
}
}
}
int main() {
int choice;
std::cout << "Welcome to Tic-Tac-Toe\n"
"1 player game, enter 1\n"
"2 player game, enter 2" << endl;
while (!(cin >> choice) || choice < 1 || choice > 2) {
cout
<< "Bad input. Try again!\nfor a 1 player game, enter 1\nfor a 2 plater game, enter 2"
<< endl;
cin.clear();
cin.ignore(INT_MAX, '\n');
}
//two player game
if (choice != 1) {
cout << "Tic-Tac-Toe: 2 Players" << endl;
game(player1, player2, true);
} else {
}
return 0;
}
gamerecursively? If I keep hitting the wrong key for input, your call stack piles up and up and up with calls.row = (position - 'A') / 3; column = (position - 'A') % 3;but I'm not sure i understand what this is intended to do.