title: How to fix Tic Tac Toe winner determination
body: <p>Every time I run my Tic Tac Toe program I can create the board and do my first turn. </p>
  
  <p>After the first turn, the game just ends as: "<code>IT'S A DRAW</code>", which is one of the three ending possibilities. This just happens before the computer can even make his own turn.</p>
  
  <p>Another problem in my program is that the scanner user input limit(er) is not working (at the end of the code). If user inputs i.e a letter instead of int, the program crashes.  </p>
  
  <pre><code>package newtictactoe;
  
  import java.util.Scanner;
  import java.util.Random;
  
  public class NewTicTacToe {
  
  public static final int DRAW = 0;
  public static final int COMPUTER = 1;
  public static final int PLAYER = 2;
  
  public static int size;
  public static char[][] board;
  public static int score = 0;
  public static Scanner scan = new Scanner(System.in);
  
  /**
   * Creates base for the game.
   *
   * @param args the command line parameters. Not used.
   */
  public static void main(String[] args) {
  
      System.out.println("Select board size");
      System.out.print("[int]: ");
      size = Integer.parseInt(scan.nextLine());
  
      board = new char[size][size];
      setupBoard();
  
      int i = 1;
  
      loop:
      while (true) {
          if (i % 2 == 1) {
              displayBoard();
              getMove();
          } else {
              computerTurn();
          }
          switch (isGameFinished()) {
              case PLAYER:
                  System.err.println("YOU WIN!");
                  break loop;
              case COMPUTER:
                  System.err.println("Computer WINS!\nYOU LOOSE!!");
                  break loop;
              case DRAW:
                  System.err.println("IT'S A DRAW");
                  break loop;
          }
  
          i++;
      }
  }
  
  private static int isGameFinished() {
      if (isDraw()) {
          return DRAW;
      } else if (computerHasWon()) {
          return COMPUTER;
      } else if (playerHasWon()) {
          return PLAYER;
      }
      return 0;
  }
  
  /**
   * Checks for computer's win.
   *
   * @return if this game is won by computer.
   */
  public static boolean playerHasWon() {
      boolean hasWon = false;
  
      for (int i = 0; i &lt; size; i++) {
          for (int j = 0; j &lt; size; j++) {
              // check if 5 in a line
          }
      }
  
      return hasWon;
  }
  
  /**
   * Checks for player's win.
   *
   * @return if this game is won by computer.
   */
  public static boolean computerHasWon() {
      boolean hasWon = false;
  
      for (int i = 0; i &lt; size; i++) {
          for (int j = 0; j &lt; size; j++) {
              // check if 5 in a line
          }
      }
  
      return hasWon;
  }
  
  /**
   * Checks for draws.
   *
   * @return if this game is a draw
   */
  public static boolean isDraw() {
      for (int i = 0; i &lt; size; i++) {
          for (int j = 0; j &lt; size; j++) {
              if (board[i][j] == ' ') {
                  return false;
              }
          }
      }
  
      return true;
  }
  
  /**
   * Displays the board.
   *
   *
   */
  public static void displayBoard() {
      for (int i = 0; i &lt; size; i++) {
          for (int j = 0; j &lt; size; j++) {
              System.out.printf("[%s]", board[i][j]);
          }
  
          System.out.println();
      }
  }
  
  /**
   * Displays the board.
   *
   *
   */
  public static void setupBoard() {
      for (int i = 0; i &lt; size; i++) {
          for (int j = 0; j &lt; size; j++) {
              board[i][j] = ' ';
          }
      }
  }
  
  /*
   * Checks if the move is allowed. 
   *
   *
   */
  public static void getMove() {
  
      Scanner sc = new Scanner(System.in);
  
      while (true) {
          System.out.printf("ROW: [0-%d]: ", size - 1);
          int x = Integer.parseInt(sc.nextLine());
          System.out.printf("COL: [0-%d]: ", size - 1);
          int y = Integer.parseInt(sc.nextLine());
  
          if (isValidPlay(x, y)) {
              board[x][y] = 'X';
              break;
          }
      }
  }
  
  /*
   * Randomizes computer's turn - where it inputs the mark 'O'.
   *
   *
   */
  public static void computerTurn() {
      Random rgen = new Random();  // Random number generator   
  
      while (true) {
          int x = (int) (Math.random() * size);
          int y = (int) (Math.random() * size);
  
          if (isValidPlay(x, y)) {
              board[x][y] = 'O';
              break;
          }
      }
  }
  
  /**
   * Checks if the move is possible.
   *
   * @param inX
   * @param inY
   * @return
   */
  public static boolean isValidPlay(int inX, int inY) {
  
      // Play is out of bounds and thus not valid.
      if ((inX &gt;= size) || (inY &gt;= size)) {
          return false;
      }
  
      // Checks if a play have already been made at the location,
      // and the location is thus invalid.  
      return (board[inX][inY] == ' ');
  }
  </code></pre>
  
  <p>These last two methods in the code check if the scanner input is valid but they don't work and I don't know why.</p>
  
  <pre><code>/**
   * Checks if user input is valid
   *
   * @param scan
   * @param prompt
   * @return
   */
  public static String getInput(Scanner scan, String prompt) {
      System.out.print(prompt); // Tell user what to input
      String text = "Enter one integer value i.e 5.";
      while (true) { // Keeps on looping until valid input
          text = scan.nextLine();
          if (isInteger(text)) // Checks input is int
          {
              break; // Exit loop
          }
          System.out.print("Try again, " + prompt); // If invalid
      }
      return text; // Return valid user input
  }
  
  /**
   * Checks if input string is int.
   *
   * @param str
   * @return
   */
  public static boolean isInteger(String str) {
      try {
          Integer.parseInt(str); // If this succeeds the input is int
          return true;
      } catch (NumberFormatException e) {
          return false; // If not int
          }
      }
  }
  </code></pre>