/**
* Demonstrate 2-D Arrays and Array-Passing in Java
* using the problem domain of Tic-Tac-Toe
*
* @author (Michael Branicky), 10/04/2006, 10/02/07
*/
public class TicTacToe {
   // check to see if board has three in a row or not
   public static boolean isWin (char board [][]) {
     int row, col;

     // check each row
     for (row = 0; row<3; row++) {
       if ((board[row][0]=='X')&&(board[row][1]=='X')&&(board[row][2]=='X')) {
         System.out.println("'X' Wins");
         return true;
       }
       if ((board[row][0]=='O')&&(board[row][1]=='O')&&(board[row][2]=='O')) {
         System.out.println("'O' Wins");
         return true;
       }
     } 

     // check each column
     for (col = 0; col<3; col++) {
       if ((board[0][col]=='X')&&(board[1][col]=='X')&&(board[2][col]=='X')) {
         System.out.println("'X' Wins");
         return true;
       }
       if ((board[0][col]=='O')&&(board[1][col]=='O')&&(board[2][col]=='O')) {
         System.out.println("'O' Wins");
         return true;
       }
     } 

     // check the main diagonal
     if ((board[0][0]=='X')&&(board[1][1]=='X')&&(board[2][2]=='X')) {
       System.out.println("'X' Wins");
       return true;
     }
     if ((board[0][0]=='O')&&(board[1][1]=='O')&&(board[2][2]=='O')) {
       System.out.println("'O' Wins");
       return true;
     }

     // check the anti-diagonal
     if ((board[2][0]=='X')&&(board[1][1]=='X')&&(board[0][2]=='X')) {
       System.out.println("'X' Wins");
       return true;
     }
     if ((board[2][0]=='O')&&(board[1][1]=='O')&&(board[0][2]=='O')) {
       System.out.println("'O' Wins");
       return true;
     }

     // we've checked all rows, columns, and diagonals for both X and O
     return false;
   }


   /*
       Print a Tic-Tac-Toe board using the following style

           X | O | X
          ---+---+---
           O | X | 0
          ---+---+---
           X | O | X

   */
   public static void printBoard (char board [][]) {
     int row, col;

     System.out.println();
     for (row=0; row<3; row++) {
       for (col=0; col<3; col++) {
         System.out.print(" "+board[row][col]+" ");
         if (col!=2) { 
           System.out.print("|");
         }
         else {
           System.out.println();  // end row
         }
       }
       if (row!=2) { System.out.println("---+---+---"); }
     }
     System.out.println();
   }


   public static void main(String args[]) {
     int row, col;

     // Declare, create, and initialize some boards
     char board1 [][] = { { 'X', 'X', 'O' }, { 'O', 'O', 'X' }, { 'X', 'O', 'X' } };
     char board2 [][] = { { 'X', ' ', 'O' }, { 'O', 'O', ' ' }, { 'X', 'X', 'X' } };

     // Declare and create a blank Tic-Tac-Toe Board
     char blankboard [][] = new char [3][3];

     // initialize it
     for (row=0; row<3; row++) {
       for (col=0; col<3; col++) {
         blankboard[row][col] = ' ';
       }
     }
     
     // print the boards and check if they are winning positions
     printBoard(board1);
     System.out.println("Is win? "+isWin(board1));
     printBoard(board2);
     System.out.println("Is win? "+isWin(board2));
     printBoard(blankboard);
     System.out.println("Is win? "+isWin(blankboard));
   }
}
