Search code examples
javatic-tac-toe

Tic Tac Toe java draw game


So I did a Tic Tac Toe assignment for my class. I have successfully created a simple Tic Tac Toe program but somehow the method to check for draw sometimes doesn't come out right. If everything is filled but there's no winner, then it's a draw. But if everything else is filled except for row 0 column 1, it will still show "draw" even if that box is still blank. If you don't get what I mean, just try filling out everything but the middle box on the top row but don't win, it will say "draw" even though that last box is not filled. What did I do wrong in my code???? Here's the driver: import javax.swing.JOptionPane;

public class TwoDimensionalArray_Driverr
{
  public static void main(String[]args)
  {
    char player = 'o';
    TwoDimensionalArrayy game = new TwoDimensionalArrayy();

    while (game.checkGame() == "PLAY")
    {
      if (player == 'o') player = 'x';
      else player = 'o';
      System.out.println(game);
      String input = JOptionPane.showInputDialog("Enter Position of Row for player "+ player +" or     press Cancel to exit");
      if (input == null)
        System.exit(0);
      int row = Integer.parseInt(input);

      input = JOptionPane.showInputDialog("Enter Position of Column for player " + player);
      int column = Integer.parseInt(input);

      game.set(row,column,player);
      game.isDraw();
      game.hasWon(row,column,player);
      game.checkGame();
      System.out.println(game.checkGame());
    }
    if (game.checkGame()=="DRAW"){
      System.out.println(game);
      System.out.println("It's a draw.");
    }
    else {
      System.out.println(game);
      System.out.println(player + " has won.");}
  }
}

And here is the Object: public class TwoDimensionalArrayy { private String currentState = "GO"; private char[][] board; private static final int ROWS = 3; private static final int COLUMNS = 3;

  public TwoDimensionalArrayy(){
  board = new char[ROWS][COLUMNS];

  for(int i=0;i<ROWS;i++) //always do ROWS first!!!!
    for(int j = 0;j<COLUMNS;j++)
    board[i][j]=' ';
  }

  public void set(int i, int j, char player)
  {
    if(board[i][j] != ' ' )
      throw new IllegalArgumentException("Position Occupied");
    board[i][j] = player;
  }

  public String toString()
  {
    System.out.println("This is the board. 3x3");
    System.out.println("Position start @ row[0]col[0],row[0]col[1],row[0]col[2]");
    String dString= "";
    for (int row = 0; row<ROWS; row++)
    {
      if (COLUMNS>0)
      dString += board[row][0];
      for (int col = 1; col<COLUMNS; col++)
      {
        dString+= "|" + board[row][col];
      } //end 2nd for
      dString += "\n";
    }//end first for
    return dString;
  }

  public String checkGame(){
    if (currentState=="Win"){
      return "END";}
    else if (currentState=="Draw"){
      return "DRAW";}
    else return "PLAY";
  }
  public void hasWon(int i,int j,char player){
    if (board[i][0] == player         // 3-in-the-row
                   && board[i][1] == player
                   && board[i][2] == player
              || board[0][j] == player      // 3-in-the-column
                   && board[1][j] == player
                   && board[2][j] == player
              || i == j            // 3-in-the-diagonal
                   && board[0][0] == player
                   && board[1][1] == player
                   && board[2][2] == player
              || i + j == 2  // 3-in-the-opposite-diagonal
                   && board[0][2] == player
                   && board[1][1] == player
                   && board[2][0] == player)
      currentState = "Win";
  }
  public void isDraw(){
     for ( int row = 0; row < ROWS; row++) {
         for (int col = 0; col < COLUMNS; col++) {
            if (board[row][col] == ' ') {
               currentState = "Play";
               break;
            }
            else {currentState = "Draw";} // no empty cell, it's a draw}
         }
      }
   }
}

Solution

  • public void isDraw(){
      for ( int row = 0; row < ROWS; row++) {
        for (int col = 0; col < COLUMNS; col++) {
          if (board[row][col] == ' ') {
            currentState = "Play";
            break;
          } else {
            currentState = "Draw"; // no empty cell, it's a draw
          } 
        }
      }
    }
    

    The break here will escape the inner for loop, but not the outer one. Essentially isDraw only considers the last row. You should try using return instead.