hey guys i need help with my tic tac toe program - its just the rules of the game not the graphical parts.

the gameisover() method isn't working

its supposed to check each row and column and diagonal to see if it has all X's or O's and it should make sure there is at least 1 underscore remaining (and setting the winner).

Im having trouble with the underscore part
can you guys check fix the problem and check the rest of the program?
(goes by normal tic tac toe rules)

Thanks for the help :)

public class TicTacGame
{
  private char grid[][] = new char[3][3];
  private char currentPlayer;
  private String winner;
  
  public TicTacGame()
  {
    for(int i = 0; i < grid.length; i++)
      for(int j = 0; j <grid[0].length; j++)
        grid[i][j] = '_'; 
    currentPlayer = 'X';
    winner = "none";
  }
  
  public char getCurrentPlayer()
  {
      return currentPlayer;
  }
  
  public boolean makePlay(int r, int c)
  {
   if (grid[r][c] == '_')
   {
      grid[r][c] = currentPlayer;
      if (gameIsOver() == false)
           changePlayer();
      return true;
   }
   else
      return false;
  }
  
  public String toString()
  {
    return "Current Player: " + currentPlayer + " Game Over: " + winner +
    " r1: " + grid[0][0] + "," + grid[0][1] + "," + grid[0][2] + "; " +
    " r2: " + grid[1][0] + "," + grid[1][1] + "," + grid[1][2] + "; " +
    " r3: " + grid[2][0] + "," + grid[2][1] + "," + grid[2][2] + "; ";
  } 
  
  public boolean gameIsOver()
  {
    boolean underScoreLeft = false;
    if (grid[0][0] == '_' || grid[0][1] == '_' || grid[0][2] == '_' || grid[1][0] == '_' || grid[1][1] == '_' || grid[1][2] == '_' || grid[2][0] == '_' || 
          grid[2][1] == '_' || grid[2][2] == '_' )
        underScoreLeft = true;
    else 
        underScoreLeft = false;
   
        
    if (underScoreLeft == true)
    {
      winner = "Tie";
      if (grid[0][0] != '_' && grid[0][0] == grid[1][1] && grid[1][1] == grid[2][2])
      {
          winner = getCurrentPlayer() + "";
          return true;
      }    
      else if (grid[0][2] != '_' && grid[0][2] == grid[1][1] && grid[1][1] == grid[2][2])
      {
          winner = getCurrentPlayer() + "";
          return true;
      }
      else if (grid[1][0] != '_' && grid[1][0] == grid[1][1] && grid[1][1] == grid[1][2])
      {
          winner = getCurrentPlayer() + "";
          return true;
      }
      else if (grid[2][0] != '_' && grid[2][0] == grid[2][1] && grid[2][1] == grid[2][2])
      {
          winner = getCurrentPlayer() + "";
          return true;
      }
      else if (grid[0][0] != '_' && grid[0][0] == grid[0][1] && grid[0][1] == grid[0][2])
      {
          winner = getCurrentPlayer() + "";
          return true;
      }
      else if (grid[0][0] != '_' && grid[0][0] == grid[0][1] && grid[0][1] == grid[0][2])
      {
          winner = getCurrentPlayer() + "";
          return true;
      }
      else if (grid[1][0] != '_' && grid[1][0] == grid[1][1] && grid[1][1] == grid[1][2])
      {
          winner = getCurrentPlayer() + "";
          return true;
      }
      else if (grid[2][0] != '_' && grid[2][0] == grid[2][1] && grid[2][1] == grid[2][2])
      {
          winner = getCurrentPlayer() + "";
          return true;
      }
   else 
   {
      winner = "TIE";
      return false;
   }
                              // also giving me a return statement missing error?
 }
    
  public void resetGame()
  {
    for(int i = 0; i < grid.length; i++)
      for(int j = 0; j <grid[0].length; j++)
        grid[i][j] = '_'; 
    currentPlayer = 'X';
    winner = "none";
  }
  
  public void changePlayer()
  {
    if (currentPlayer == 'X')
       currentPlayer = 'Y';
    else
       currentPlayer = 'X';
  }
  
  public String getWinner()
  {
      return winner;
  }
  
  public char charAt( int r, int c)
  {
    return grid[r][c];
  }

here is the winner checker for it see if you can find your problem by comparison.

public static boolean checkWinner(char[][] board) {
        //Check for any rows with all the same value
        for (int i = 0; i < board.length; i++) {
            int winCount = 0;
            for (int j = 0; j < board[0].length; j++) {
                if (board[i][j] != 'X')
                    if(board[i][j] != 'O')
                        break;
                if (board[i][0] == board[i][j])
                    winCount++;
                else
                    break;
            }

                if (winCount == board[0].length)
                    return true;
        }
        //Check for any columns with all the same value
        for (int j = 0; j < board.length; j++) {
            int winCount = 0;
            int[] column = new int[board.length];
            for (int i = 0; i < board.length; i++) {
                column[i] = board[i][j];
                    if (column[i] != 'X')
                        if(column[i] != 'O')
                            break;
                    if (column[i] == column[0])
                        winCount++;
                    else
                        break;
            }

                if (winCount == board.length)
                    return true;
        }
        //Check for downward diagonal win
        int winCount = 0;
        for (int i = 0; i < board.length; i++) {
            if (board[i][i] != 'X')
                if(board[i][i] != 'O')
                    break;
            if (board[i][i] == board[0][0])
                winCount++;
            else
                break;
        }
            if (winCount == board.length)
                return true;
        //Check for an upward diagonal win
        int j = 0;
        winCount = 0;
        for (int i = board.length - 1; i >= 0; i--) {
            if (board[i][j] != 'X')
                if(board[i][j] != 'O')
                    break;
            if (board[i][j] == board[board.length - 1][0])
                winCount++;
            else
                break;
            j++;
        }
        if (winCount == board.length)
            return true;
        else
            return false;
    }
}

The return statement error is not hard to spot, although it would be easier if you tried to simplify your code a little.
One way to find this is to trace the logic of that method - make sure that every branch leads to a return statement. You should find the error almost immediately, if you examinge the code carefully in this way. Remember that every if has two branches, and only one executes.
The other error may be solved when you find this one - I think it's likely, but I don't want to promise anything, since it would require spending more time than I like in that logic.

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.