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){
//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] == board[i]) && (board[i] == board[i]))
return board[i];

//Check columns
for (int i = 0; i < 3; i++)
if ((board[i] == board[i]) && (board[i] == board[i]))
return board[i];

//Check diagonals
if ((board == board) && (board == board))
return board;

if ((board == board) && (board == board))
return board;

//Draw (full board)
return 'D'; }

//test
int main(){
char ttt;
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 >> ttt >> ttt >> ttt >> ttt >> ttt >> ttt >> ttt >> ttt;

cout << win(ttt) << endl;

system("PAUSE");

return 0; }``````

Any ideas on how to fix this?

``````in main
{
create board and intialize to E
declare plays and initialize to 1
declare no Winner and initialze to true
while(plays < 9 && no Winner)
{
make play
++plays
winner = win()
if(winner != 'C')
no Winner is false
}
if plays == 10 …``````

It's shorthand to stand for a 2 dimensional array. It would mean something like:

char win(char board)

## All 5 Replies

``````in main
{
create board and intialize to E
declare plays and initialize to 1
declare no Winner and initialze to true
while(plays < 9 && no Winner)
{
make play
++plays
winner = win()
if(winner != 'C')
no Winner is false
}
if plays == 10
draw
else
if winner ==  'X'
cout X won
else
cout O won
}

char win(char ** ttt)
{
char result == 'C';

//check rows
for each row
if(first element != 'E')
if(first element equals second element and first equals third)
result = first element
break;
if(result == 'C')
check columns
if(result == 'C')
check diagonals

return result
}``````

Thanks for the input. I have just one question: what are the two asterisks in line 23 for?

It's shorthand to stand for a 2 dimensional array. It would mean something like:

char win(char board)

I got it to work! : ) Thanks again.

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){
//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] == board[i]) && (board[i] == board[i]))
return board[i];

//Check columns
for (int i = 0; i < 3; i++)
if ((board[i] == board[i]) && (board[i] == board[i]))
return board[i];

//Check diagonals
if ((board == board) && (board == board))
return board;

if ((board == board) && (board == board))
return board;

//Draw (full board)
return 'D'; }

//test
int main(){
char ttt;
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 >> ttt >> ttt >> ttt >> ttt >> ttt >> ttt >> ttt >> ttt;

cout << win(ttt) << endl;

system("PAUSE");

return 0; }``````

Any ideas on how to fix this?

. Pearle, I,m trying to write a program based on C++ objject oriented progamming, except that mine involves something far more comlex than tic tac toe. I have to write a program for a chess game. Would you mind sending me your full code for the tic tac toe so I can run it to see how it actually works, and take it from there? Many thanks. Usain

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.21 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.