This is not a complete program, I have comments to omit the incomplete portions. After "Player One" enters a row (see lines 54-5), the program ends. It outputs the "Enter a column" and the board and ends. I don't see why. Any help would be greatly appreciated!
(Output after runtime error is included after code)
void showRules();
char playerOne(char[][3], int, int);
char playerTwo(char[][3], int, int);
int getWinner(char[][3], int, int);
void showBoard(char[][3], int, int);
int main() {
const int row = 3;
const int col = 3;
int winner;
char game[row][col] = {{'*', '*', '*'},
{'*', '*', '*'},
{'*', '*', '*'}};
showRules();
//do {
showBoard(game, row, col);
playerOne(game, row, col);
//winner = getWinner(game, row, col);
showBoard(game, row, col);
playerTwo(game, row, col);
//winner = getWinner(game, row, col);
// } while (winner != 1 && winner != 2);
cout << "\n\n\n\n";
return 0;
}
/************************************************************/
void showRules() {
char ch;
system("clear");
cout << "\n\n\t\t________________________________"
<< "\n\t\t TIC - TAC - TOE"
<< "\n\n\t\tRules: Two Player Game"
<< "\n\t\t - Player One (X)"
<< "\n\t\t - Player Two (O)"
<< "\n\n\t\tObject: Place game pieces (X,O)"
<< "\n\t\t in a line of three."
<< "\n\t\t - In a row"
<< "\n\t\t - A column"
<< "\n\t\t - Or diagonally"
<< "\n\t\t________________________________"
<< "\n\t\t Press [ENTER] to Play"
<< "\n\n";
cin.get(ch);
}
/************************************************************/
char playerOne(char game[][3], int row, int col){
cout << "\n\n\t\tPlayer One"
<< "\n\t\t------------------------------"
<< "\n\t\tEnter a row (A,B, or C): ";
cin >> row;
cin.ignore();
row = toupper(row);
if (row == 65) row = 0;
if (row == 66) row = 1;
if (row == 67) row = 2;
cout << "\n\n\t\tEnter a column (1, 2, or 3): ";
cin >> col;
char play = 'X';
game[row][col - 1] = play;
return play;
}
/************************************************************/
char playerTwo(char game[][3], int row, int col){
cout << "\n\n\t\tPlayer Two"
<< "\n\t\t------------------------------"
<< "\n\t\tEnter a row (A,B, or C): ";
cin >> row;
row = toupper(row);
cin.ignore();
if (row == 65) row = 0;
if (row == 66) row = 1;
if (row == 67) row = 2;
cout << "\n\n\t\tEnter a column (1, 2, or 3): ";
cin >> col;
char play = 'O';
game[row][col - 1] = play;
return play;
}
/************************************************************/
int getWinner(char game[][3], int row, int col) {
}
/************************************************************/
void showBoard(char game[][3], int row, int col) {
system("clear");
cout << "\n\n";
cout << "\t\t" << setw(7) << "1" << setw(6) << "2" << setw(6) << "3" << "\n\n"
<< "\t\t" << setw(10) << "|" << setw(6) << "|" << "\n"
<< "\t\t" << " A " << setw(3) << game[0][0] << setw(3) << "|" << setw(3)
<< game[0][1] << setw(3) << "|" << setw(3) << game[0][2] << "\n"
<< "\t\t" << setw(10) << "|" << setw(6) << "|" << "\n"
<< "\t\t" << " -----------------" << "\n"
<< "\t\t" << setw(10) << "|" << setw(6) << "|" << "\n"
<< "\t\t" << " B " << setw(3) << game[1][0] << setw(3) << "|" << setw(3)
<< game[1][1] << setw(3) << "|" << setw(3) << game[1][2] << "\n"
<< "\t\t" << setw(10) << "|" << setw(6) << "|" << "\n"
<< "\t\t" << " -----------------" << "\n"
<< "\t\t" << setw(10) << "|" << setw(6) << "|" << "\n"
<< "\t\t" << " C " << setw(3) << game[2][0] << setw(3) << "|" << setw(3)
<< game[2][1] << setw(3) << "|" << setw(3) << game[2][2] << "\n"
<< "\t\t" << setw(10) << "|" << setw(6) << "|" << "\n\n\n\n";
}
Output after code line 55 is executed
Enter a column (1, 2, or 3):
1 2 3
| |
A * | * | *
| |
-----------------
| |
B * | * | *
| |
-----------------
| |
C * | * | *
| |
Player Two
------------------------------
Enter a row (A,B, or C):
Enter a column (1, 2, or 3):
emiller7@endofuniversecafe:$