Thread: Check tic tac toe winner

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    22

    Check tic tac toe winner

    I have a tic tac toe program. The board is stored in a two dimensional character array board[3][3]. Here is a function that is supposed to check if the player specified in the player argument wins. It doesn't work. Ideas?

    Code:
    bool is_threeinrow(char player, char board[3][3])
    {
         bool done = 0;
         for (int x = 0; x < 3; x++)
         {
             if (board[x][0] == board[x][1] == board[x][2] == player)
             {
                return 1;
                done = 1;
             }
         }
         for (int y = 0; y < 3; y++)
         {
             if (board[0][y] == board[1][y] == board[2][y] == player)
             {
                return 1;
                done = 1;
             }
         }
         if (board[0][0] == board[1][1] == board[2][2] == player)
         {
            return 1;
            done = 1;
         }
         if (board[0][2] == board[1][1] == board[2][0] == player)
         {
            return 1;
            done = 1;
         }
         if (done == 0)
         {
         return 0;
         }
         
    }

  2. #2
    Unregistered User
    Join Date
    Sep 2005
    Location
    Antarctica
    Posts
    341
    looks fine to me, beside the fact that done = 1 never gets set, but you don't need that anyway. In what cases is it returning the wrong result?

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> board[x][0] == board[x][1] == board[x][2] == player
    You can't string together == like that because one call to == returns true or false, and so the character stored in the board will not be equal to the bool true or false the next time it is called.

    Use a series of == expressions combined with &&.

  4. #4
    Unregistered User
    Join Date
    Sep 2005
    Location
    Antarctica
    Posts
    341
    doh! can't belive that I missed that.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Bizarre Tic Tac Toe!
    By Lynux-Penguin in forum C Programming
    Replies: 5
    Last Post: 05-14-2002, 05:10 PM
  2. Check my Tic Tac Toe game for bugs please
    By aresashura in forum Game Programming
    Replies: 5
    Last Post: 12-19-2001, 08:47 AM
  3. Replies: 22
    Last Post: 11-08-2001, 11:01 PM
  4. My tic tac toe game, please check this out.
    By Leeman_s in forum C++ Programming
    Replies: 1
    Last Post: 11-05-2001, 11:14 PM
  5. Tic tac toe (how do i check for winnner?)
    By Leeman_s in forum C++ Programming
    Replies: 3
    Last Post: 10-20-2001, 03:38 PM