title: Java Tic Tac Toe Game Confirming Winner
body: <p>Below is my code for a Tic Tac Toe game. There are two problems I am running into. For one, I am not sure how to return which player has won (X or O), I can just return if there is a winner. As well, when I try to run my program I can an out of bounds error. Where did I go wrong?</p>
  
  <p>I have two files, one being TicTacToe and the other TTTBoard.</p>
  
  <p><em>TicTaeToe.java</em></p>
  
  <pre><code>import java.util.Scanner;
  import java.util.Random;
  
  public class TicTacToe
  {
      public static void main(String[]args){
          Scanner reader = new Scanner(System.in);
          TTTBoard board = new TTTBoard();
  
          System.out.println(board);
  
          Random gen = new Random();
          char player;
          if(gen.nextInt(2)==1)
              player = 'o';
          else
              player = 'x';
          while(!board.checkWinner() &amp;&amp; !board.full()){
  
              System.out.println(player + " 's turn");
              System.out.println("Enter the row and column [1-3, space, 1-3]: ");
              int row = reader.nextInt();
              int column = reader.nextInt();    
              boolean success = board.placeXor0(player, row, column);
              if(!success)
                  System.out.println("Error: cell already occupied!");
              else{
                  System.out.println(board);
                  if(player == 'x')
                      player = 'o';
                  else
                      player = 'x';
                  }
              }        
  }
  }
  </code></pre>
  
  <p><em>TTTBoard.java</em></p>
  
  <pre><code>public class TTTBoard{
      private char[][] board;
  
      public TTTBoard(){
          board = new char[3][3];
          reset();
      }
  
      public void reset(){
          for(int row = 0; row &lt; 3; row++)
              for(int column = 0; column &lt; 3; column++)
                  board[row][column] = '-';
          }
      public String toString(){
          String result = "\n";
          for(int row = 0; row &lt;3; row++){
              for (int column = 0; column &lt; 3; column++)
                  result += board[row][column] + " ";
              result +="\n";
          }
          return result;
      }
      public boolean placeXor0(char player, int row, int column){
          if(board[row -1][column -1]=='-'){
              board[row-1][column-1]= player;
              return true;
          }
          else
              return false;
  
      }
      public boolean checkWinner(){
          return(checkRowsForWin()||checkColumnsForWin()||checkDiagnalsForWin());
      }
  
      public boolean full(){
          boolean full = true;
          for(int row = 0; row &lt; 3; row++){
              for(int column = 0; column &lt; 3; column++){
                  if(board[row][column] == '-'){
                      full = false;
                  }
              }
          }
          return full;
      }
      public boolean checkRowsForWin(){
          for(int row = 0; row &lt; 3; row++){
                  if(placeXor0(board[row][0], board[row][1], board[row][2]) == true){
                      return true;
                  }
              }
  
          return false;
      }
      public boolean checkColumnsForWin(){
          for(int column = 0; column &lt; 3; column++){
              if(placeXor0(board[0][column], board[1][column], board[2][column]) == true){
                  return true;
                  }
              }
          return false;
      }
      public boolean checkDiagnalsForWin(){
          return((placeXor0(board[0][0], board[1][1], board[2][2]) == true) || (placeXor0(board[0][2], board[1][1], board[2][0]) == true));
          }    
  }
  </code></pre>
  
  <p>New Code:</p>
  
  <pre><code>public class TTTBoard{
      private char[][] board;
  
      public TTTBoard(){
          board = new char[3][3];
          reset();
      }
  
      public void reset(){
          for(int row = 0; row &lt; 3; row++)
              for(int column = 0; column &lt; 3; column++)
                  board[row][column] = '-';
          }
      public String toString(){
          String result = "\n";
          for(int row = 0; row &lt;3; row++){
              for (int column = 0; column &lt; 3; column++)
                  result += board[row][column] + " ";
              result +="\n";
          }
          return result;
      }
      public boolean placeXor0(char player, int row, int column){
          if(board[row -1][column -1]=='-'){
              board[row-1][column-1]= player;
              return true;
          }
          else
              return false;
  
      }
      public boolean checkWinner(){
          return(checkRowsForWin()||checkColumnsForWin()||checkDiagnalsForWin());
      }
      /*public String getWinner(){
          for(int row = 0; row &lt;3; row++){
              for (int column = 0; column &lt; 3; column++)
              }
          }*/
      public boolean full(){
          boolean full = true;
          for(int row = 0; row &lt; 3; row++){
              for(int column = 0; column &lt; 3; column++){
                  if(board[row][column] == '-'){
                      full = false;
                  }
              }
          }
          return full;
      }
      public boolean checkRowsForWin(){
          for(int row = 0; row &lt; 3; row++){
                  if(board[row][0]== board[row][1]&amp;&amp; board[row][0]== board[row][2]){
                      return true;
                  }
              }
  
          return false;
      }
      public boolean checkColumnsForWin(){
          for(int column = 0; column &lt; 3; column++){
              if (board[0][column] == board[1][column] &amp;&amp; board[0][column] == board[2][column]) {
                  return true;
                  }
              }
          return false;
      }
      public boolean checkDiagnalsForWin(){
          return((board[0][0]== board[1][1]&amp;&amp; board[0][0] == board[2][2]) || (board[0][2] == board[1][1] &amp;&amp; board[0][2]== board[2][0]));
          }
  
  
  }
  </code></pre>