title: How do I determine a winner in my Tic Tac Toe program
body: <p>Earlier post: <a href="https://stackoverflow.com/questions/33655531/how-do-i-make-my-tictactoe-program-scalable/33655701?noredirect=1#comment55208355_33655701">How do I make my tictactoe program scalable</a></p>
  
  <p>I have tried to make a Tic Tac Toe program (human vs computer) scalable (the board size can be changed). I had major problems earlier but fixed most of them.</p>
  
  <p>The game's rules are basic Tic Tac Toe but one different rule is that no matter how large the board is (when <code>&gt;= 5</code>) the player or computer only need five marks in a row to win.</p>
  
  <p>Now the only gamebreaking problem with my program is determining who wins the game. It is only possible that the game ends a 'draw' at the moment. (Also I have not implemented the "<code>&gt;= 5</code>" yet).</p>
  
  <p>Specific problem explanation is that I need to determine a winner and end screen for something like "<code>computer wins</code>" and/or "<code>player wins</code>". </p>
  
  <pre><code>package tictactoe;
  
  import java.util.Scanner;
  import java.util.Random;
  
  public class TicTacToe {
  
      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;
  
          while (true) {
              if (i % 2 == 1) {
                  displayBoard();
                  getMove();
              } else {
                  computerTurn();
              }
  
              // isWon()
              if (isDraw()) {
                  System.err.println("Draw!");
                  break;
              }
  
              i++;
          }
  
      }
  
      /**
       * 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>