// Arup Guha
// 3/25/02
// Allows two players to play Tic Tac Toe against each other.

// Edited on 7/26/05 to work without using objects for 2005 BHCSI.

import java.util.*;

public class ttt {

  final static char[] pieces = {'X','O'};

  // Tries to make a move at row, column. If it's valid the move is made
  // and true is returned. Else nothing is done and false is returned.
  public static boolean Move(char[][] board, int row, int column, 
					     int whoseturn) {

    if ( (board[row][column] == '_') && inbounds(row,column) ) {
      board[row][column] = pieces[whoseturn];
      return true;
    }
    else
      return false;
  }

  // Returns true if indexes passed to the method are inbounds.
  public static boolean inbounds(int row, int column) {

    if ((row < 0) || (column < 0))
      return false;
    if ((row > 2) || (column > 2))
      return false;
    return true;
  }

  // Prints out the playing board.
  public static void printboard(char[][] board) {

    System.out.println("\t0  1  2");
    for (int i=0; i<3; i++) {
      System.out.print(i+"\t");
      for (int j=0; j<3; j++)
        System.out.print(board[i][j]+"  ");
      System.out.println();
    }
  }

  // Returns a character signifying the winner.
  public static char winner(char[][] board, int movesmade) {

    for (int i=0; i<3; i++)
      if (SameArray(board[i]) && board[i][0] != '_')
        return board[i][0];

    for (int i=0; i<3; i++)
      if ((board[0][i] == board[1][i]) && (board[1][i] == board[2][i]) && (board[0][i] != '_'))
         return board[0][i];

    if ((board[0][0] == board[1][1]) && (board[1][1] == board[2][2]))
         return board[0][0];

    if ((board[2][0] == board[1][1]) && (board[1][1] == board[0][2]))
         return board[2][0];

    if (movesmade == 9)
      return 'T';

    return '_';

  }

  // Checks to see if all the characters in a single character array are
  // the same.
  private static boolean SameArray(char[] word) {

    char check = word[0];
    for (int i=1; i<word.length; i++)
      if (check != word[i])
        return false;
    return true;
  }

  // Returns the player who's playing piece is the character x.
  public static String whosePiece(String[] players, char x) {
    for (int i=0; i<2; i++)
      if (x == pieces[i])
        return players[i];
    return "Dummy";
  }

  public static void main(String[] args) {

    Scanner stdin = new Scanner(System.in);

    char[][] board;
    int whoseturn;
    String[] players = new String[2];
    int movesmade  = 0;
    
    // Read in players' names.
    System.out.println("Player #1, enter your name.");
    players[0] = stdin.next();

    System.out.println("Player #2, enter your name.");
    players[1] = stdin.next();

    // Set up the beginning of the game.
    board = new char[3][3];
    for (int i=0;i < 3; i++)
      for (int j=0; j < 3; j++)
        board[i][j] = '_';
    whoseturn = 0;

    // Play as long as there is no winner or tie.
    while (winner(board, movesmade) == '_') {

      int r,c;
      boolean done = false;

      // Read in a move & check if it's valid.
      do {
	
        printboard(board);

        System.out.print(players[whoseturn]);            
        System.out.print(", Enter the row(0-2) and column(0-2) ");
	  System.out.println("of your move.");

      	r = stdin.nextInt();
      	c = stdin.nextInt();	
        if (!inbounds(r,c)) 
          System.out.println("Sorry, those are invalid entries.");
        else {
          if (!Move(board,r,c,whoseturn))
            System.out.println("Sorry, that square is taken.");
          else
            done = true;
       }  
      } while (!done);
	
      // Change who's turn it is.
      whoseturn = (whoseturn + 1)%2;
      movesmade++;
    }

    // Print out a message with the winner.
    printboard(board);
    char win = winner(board, movesmade);

    if (win == 'T')
      System.out.println("Both of you played to a tie.");
    else {
      System.out.print("Congratulations, " + whosePiece(players,win));
      System.out.println(", you have won the game.");
    }

  }
}
