Hi, all. I'm trying to write a function that checks the status of a Tic Tac Toe game. If there's a winner, it'll return the character for the winner ('X' or 'O'). If there's a draw, it returns 'D'. I'm having trouble getting it to check whether the game is unfinished, though (i.e., there are empty spaces).
I tried to make it so that each element of the 2d array 'ttt' starts off as 'E', an empty space. Then whatever is the user's input will overwrite the E's, and if there are E's left, the function win() will return 'C' for "continue". But if I enter less than nine characters, the program waits for me to enter more until there are nine.
Here's my code so far:
#include <iostream>
using namespace std;
char win(char board[3][3]){
//Check for empty spaces ('E')
for (int i = 0; i < 3; i++){
for (int j = 0; j < 3; j++){
if (board[i][j] == 'E'){
return 'C';
break; } } }
//Check rows
for (int i = 0; i < 3; i++)
if ((board[i][0] == board[i][1]) && (board[i][0] == board[i][2]))
return board[i][0];
//Check columns
for (int i = 0; i < 3; i++)
if ((board[0][i] == board[1][i]) && (board[0][i] == board[2][i]))
return board[0][i];
//Check diagonals
if ((board[0][0] == board[1][1]) && (board[0][0] == board[2][2]))
return board[0][0];
if ((board[0][2] == board[1][1]) && (board[0][2] == board[2][0]))
return board[0][2];
//Draw (full board)
return 'D'; }
//test
int main(){
char ttt[3][3];
for (int i = 0; i < 3; i++){
for (int j = 0; j < 3; j++)
ttt[i][j] = 'E'; }
cout << "Enter a tic-tac-toe board:\n";
cin >> ttt[0][0] >> ttt[0][1] >> ttt[0][2] >> ttt[1][0] >> ttt[1][1] >> ttt[1][2] >> ttt[2][0] >> ttt[2][1] >> ttt[2][2];
cout << win(ttt) << endl;
system("PAUSE");
return 0; }
Any ideas on how to fix this?