// ITI1120 2010, Assignment 4, Question 2
// Name: Gilbert Arbez, Student# 1234567

// This program is an implementation of the Tic Tac Toe game.
// The user is prompted to play a game (can play many times)
// A game board is displayed and each player can make a play 
// (that is place an X or O on the board)
// When a win or draw is seen, the program prints the appropriate
// message and prompts the user to play again.

// Methods from the A4Q2BoardLib.java are use to clear the contents of the board
// and check for winners.

class A4Q2TTT
{
  /*
   * Method: main
   * 
   * It all starts here.  This method provides the overall control
   * of the program.  The outer loop is used to ask the user to play a
   * game while the inner loop repeats the plays for a single game.
   */
  public static void main (String[] args)
  {
    // DECLARE VARIABLES/DATA DICTIONARY 
    
    char [] [] gameBoard;  //Reference to the gameboard
    char [] response;   // response from user to play game
    boolean winner;  // is true when winner is found
    
    
    // Create the gameboard matrix
    gameBoard = new char[3][3];  // This is the only matrix used in the game.
    
    System.out.println ("Start a game (Y or N):");
    response = ITI1120.readCharLine();    
    while ( (response[0] == 'y') || (response[0] == 'Y') ) 
    {
      A4Q2BoardLib.clearBoard(gameBoard);  // setup game board
      winner = false;  // initialise winner
      while(!winner)
      {
        printBoard(gameBoard);  // display current board
        play(gameBoard,'X');  // have player X enter play
        winner = A4Q2BoardLib.checkWinner(gameBoard);  // do we have winner
        if(!winner) 
        {  // no winner yet, so let other player play
          printBoard(gameBoard); // show play from X player
          play(gameBoard,'O');  // have player O enter play
          winner = A4Q2BoardLib.checkWinner(gameBoard);  // check for winner again
        }
      }
      printBoard(gameBoard); // prints the winning baord
      System.out.println ("Start a game (Y or N):");
      response = ITI1120.readCharLine(); // see if we play again
    }
  }
  
  /*
   * METHOD:  void printBoard(char [][])
   *  This method displays the board
   * 
   * GIVEN:  char [][] baord:  a reference to a 3X3 matrix containing '-', 'X' and 'O'
   * 
   * This outputs the board using the following format:
   *    0 1 2
   *  0 - - O
   *  1 - X -
   *  2 - - X
   */   
  public static void printBoard ( char [] [] board)
  {
    // DECLARE VARIABLES / DATA DICTIONARY    
    int row;  // for indexing rows
    int col;  // for indexing col
    
    // ALGORITHM BODY
    System.out.println("  0 1 2");
    for(row = 0 ; row < board.length ; row++) 
    {
      System.out.print(row);
      for(col = 0 ; col < board[row].length ; col++) 
      {
        System.out.print(" " + board[row][col]);
      } // no else - do nothing
      System.out.println();
    }
  }
  
  /*
   * METHOD:  void play(char [][] board, char player) 
   * 
   *  This method lets player make a play
   * 
   *  GIVEN:  char [][] board:  a reference to the 3X3 board containing '-', 'X' and 'O'
   *          char player:  either X or O to indicate player making play
   *  MODIFIED: board (an element in the matrix is updated)
   *
   * This method prompts the user for a position on the board, reads in
   * two integer values in an integer array, checks to see that the number of
   * integers entered are valid, that the values of the integers are between 
   * 0 and 2 inclusive, and that a hyphen occupies the position specified by
   * the user. The user is continually prompted until proper values
   * have been entered. When a proper position has been provided by the user,
   * the corresponding character is entered into the game board (i.e matrix).
   */
  public static void play ( char [] [] board, char player)
  {
    // DECLARE VARIABLES / DATA DICTIONARY    
    int [] place;  // reference variable, for row and col position
    
    // ALGORITHM BODY
    do  // find a position that has a '-', i.e. empty
    {
      do  // get valid row/column value
      {
        System.out.println ("Player " + player + 
                            ", please enter row and col between 0 and 2: ");
        place = ITI1120.readIntLine(); // readIntLine creates an array
      }
      while( place.length != 2 || // note here that this when this test is false
                                  // the other tests are not performed, so no error
                                  // occurs when not enough integers are entered by the
                                  // user.
            ! ((0 <= place[0]) && (place[0] <= 2)) ||
            ! ((0 <= place[1]) && (place[1] <= 2))  );
      
      if(board[place[0]][place[1]] != '-')
      {
        System.out.println("Space at " + place[0] + " " + place[1] + " is occupied");
      }
    }
    while(board[place[0]][place[1]] != '-');
    
    // place the play in matrix
    //
    board[place[0]][place[1]] = player ;
    // Note that no result is returned
  }
}

