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?

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[3][3])

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[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?

. 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 developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.