/*
 * A4Q4BoardLib - Library for manipulating the contents of the Tic-Tac-Tao Board
 * ITI1120 Fall 2011, Assignment 4, Question 2
 * 
 */

class A4Q2BoardLib
{  
  /*
   * METHOD:  void clearBoard(char [][])
   * 
   *  This method sets or resets the game board (matrix) by placing
   *  hyphens in all elements of the matrix.
   * 
   * GIVEN:  char [][] board:  a reference to 3X3 matrix containing '-', 'X' and 'O'
   * MODIFIED:  board
   */  
  public static void clearBoard ( char [] [] board)
  {
    // DECLARE VARIABLES / DATA DICTIONARY   
    int row;  // for indexing rows
    int col;  // for indexing col
            
    // PRINT OUT IDENTIFICATION INFORMATION    
    System.out.println();
    System.out.println("ITI1120 Fall 2011, Assignment 4, Question 2");
    System.out.println("Name: Gilbert Arbez, Student# 1234567");
    System.out.println("Lab section 999, TA: Grace Hopper");
    System.out.println();

    // ALGORITHM BODY    
    // place a hyphen in all elements
    //
    for(row = 0 ; row < board.length ; row++) 
    {
      for(col = 0 ; col < board[row].length ; col++) 
      {
        board[row][col] = '-';
      }
    }     
  }
  
  /*
   * METHOD:  boolean checkWinner(char [][])  
   *
   * GIVEN:  char [][] baord:  a reference to a 3X3 matrix containing '-', 'X' and 'O'
   *
   * This method checks for the Tic Tac Toe winner in all elements of the matrix.
   * Must look for three X's and O's in a row column, diagonal.
   * If one is found, then print winner (using string "Player X has won" or 
   * "Player O has won") and return true.
   * If a draw, print "Game is a draw" and return true.
   * If game not over, return false.
   * The method makes calls to testRows, testCols, testDiag to check for a winner.
   * Each of these calls returns the character of a winner (X or O) or '-' if there
   * is no winner.
   * The method checks for a draw by callling testDraw which returns true if a draw exists.
   */
  public static boolean checkWinner ( char [] [] board)
  {
    char winningPlayer;  // for identifying winner
    boolean winOrDraw;  // Returns true if winOrDraw, i.e. game over
    
    winOrDraw = false;
    winningPlayer = '-';
    winningPlayer = testRows(board);
    if(winningPlayer == '-')
    {
      winningPlayer = testCols(board);
      if(winningPlayer == '-')
      {
        winningPlayer = testDiag(board);
        if(winningPlayer == '-')
        {
          if(testDraw(board))
          {
            System.out.println("Game is a draw");
            winOrDraw = true;
          }   // no else - do nothing
        }  // no else - do nothing
      }  // no else - do nothing
    }  // no else - do nothing
    
    if(winningPlayer != '-')  // have winner - lets announce it
    {
      System.out.println("Player " + winningPlayer + " has won");
      winOrDraw = true;
    }
    return winOrDraw;
  }     
  
  /*
   * METHOD:  char testRows(char [][])
   * 
   * This method checks for a winning row. 
   *
   * GIVEN:  char [][] baord:  a reference to a 3X3 matrix containing '-', 'X' and 'O'
   *
   * This method checks for three X's or O's in a row.  If found
   * the corresponding character is returned to indicate the winner
   * Otherwise a hyphen '-', is returned.
   */
 public static char testRows ( char [] [] board)
  {
    int row;  // for indexing rows
    char winningPlayer;  // to find winner 
    
    winningPlayer = '-';
    for(row = 0 ; row < 3 ; row++)
    {
      if(board[row][0] != '-')  // char at column 1 is a play - it is our reference
      {
        if( (board[row][1] == board[row][0]) &&
           (board[row][2] == board[row][0]))  // all three equal)
        {
          winningPlayer = board[row][0];
        }  // no else - do nothing
      }  // no else - do nothing
    }
    return winningPlayer;   // returns winner or hyphen '-', i.e. no winner
  }
 
/*  public static char testRows ( char [] [] board)
  {
   return testRowsRec(board, 0);
  } 
  
 public static char testRowsRec ( char [] [] board, int row)   
 {
    char winningPlayer;  // to find winner   
    winningPlayer = '-';
   
    if (row == 3)
    {
    }
    else
    { if(board[row][0] != '-')  // char at column 1 is a play - it is our reference
      {
        if( (board[row][1] == board[row][0]) &&
           (board[row][2] == board[row][0]))  // all three equal)
        {
          winningPlayer = board[row][0];
        }  // no else - do nothing
      } 
      else 
      { winningPlayer= testRowsRec(board, row+1);
      } 
    }
    return winningPlayer;   // returns winner or hyphen '-', i.e. no winner
  }
 */
 
  /*
   * METHOD:  char testCols(char [][])
   * 
   * This method checks for a winning colomn.
   * 
   *  GIVEN:  char [][] baord: a reference to a 3X3 matrix containing '-', 'X' and 'O'
   *
   * This method checks for three X's or O's in a column.  If found
   * the corresponding character is returned to indicate the winner
   * Otherwise a hyphen '-', is returned.
   */ 
  public static char testCols ( char [] [] board)
  {
    int row;  // for indexing columns
    int col;  // for indexing rows
    char winningPlayer;  // to find winner 
    
    winningPlayer = '-';
    for(col = 0 ; col < 3 ; col++)
    {
      if(board[0][col] != '-')  // char at row 1 is a play - it is our reference
      {
        if( (board[1][col] == board[0][col]) &&
           (board[2][col] == board[0][col]))  // all three equal)
        {
          winningPlayer = board[0][col];
        }  // no else - do nothing
      }  // no else - do nothing
    }
    return winningPlayer;   // returns winner or hyphen '-', i.e. no winner
  }
  
  /*
   * METHOD:  char testDiag(char [][])
   * 
   * This method checks for a winning diagonal. 
   *
   * GIVEN:  char [][] baord:  a 3X3 matrix containing '-', 'X' and 'O'
   *
   * This method checks for three X's or O's in the matrix diagonal.  If found
   * the corresponding character is returned to indicate the winner
   * Otherwise a hyphen '-', is returned.
   */  
  public static char testDiag ( char [] [] board)
  {
    int row;  // for indexing columns
    int col;  // for indexing rows
    char winningPlayer;  // to find winner 
    
    winningPlayer = '-';
    if( board[1][1] != '-')  // middle element is our reference
    {
      if( (board[0][0] == board[1][1]) &&
         (board[2][2] == board[1][1]))
      {
        winningPlayer = board[1][1];
      } 
      else  // check other diagonal
      {
        if( (board[0][2] == board[1][1]) &&
           (board[2][0] == board[1][1]))
        {
          winningPlayer = board[1][1];
        }     
      }
      
    }  // no else - do nothing
    return winningPlayer;   // returns winner or hyphen '-', i.e. no winner
  }
  
  /*
   * METHOD:  boolean testDraw(char [][])
   * 
   * This method checks for a draw
   *
   * GIVEN:  char [][] baord:  reference to a 3X3 matrix containing '-', 'X' and 'O'
   *
   * This method checks to see if all elements in the game matrix
   * contain either an X or an O, that is no hyphen '-' is found
   * in the matrix.  If a draw is detected (no hyphen '-' exists
   * in the matrix), return true otherwise return false.
   */
  public static boolean testDraw ( char [] [] board)
  {
    int row;  // for indexing columns
    int col;  // for indexing rows
    boolean isDraw;  // Result: return value, set to true when draw found
    
    isDraw = true;  // Assume a draw
    row = 0;
    while( (row < board.length) && isDraw)  // stop if not draw found
    {
      col = 0;
      while( (col < board[row].length) && isDraw)  // stop if not draw found
      {
        if(board[row][col] == '-') // hyphen found - not draw yet
        {
          isDraw = false;
        }  // no else - do nothing
        col++;
      }
      row++;
    }
    return isDraw;
  }
  
}
