import javax.swing.*;

public class TTTDriver {
  
  private static final char PLAYER_ONE = 'X';
  private static final char PLAYER_TWO = 'O';
  
  /**
   * A java application is a set of one or more classes
   * with (at least) one main method.
   * 
   * To start executing the program, the system 
   * executes the main method.
   */ 
  public static void main(String[] args) {
    
    TicTacToe t = new TicTacToe();
    TTTWindow w = new TTTWindow(t);
    
    char currPlayer = PLAYER_ONE;
    
    // while it's not a draw and no one has won yet
    while (!t.isDraw() && t.getWinner() == TicTacToe.BLANK_SQUARE) {
      
      // the player makes a move
      int r = Integer.parseInt(JOptionPane.showInputDialog(null,
                                            "Player " + currPlayer + " enter a row:"));
      
      int c = Integer.parseInt(JOptionPane.showInputDialog(null,
                                            "Player " + currPlayer + " enter a column:"));
      
      // put the mark on the board
      t.setMark(currPlayer, r, c);
      
      // update the window
      w.update(r, c);
      
      // switch players
      if (currPlayer == PLAYER_ONE) {
        currPlayer = PLAYER_TWO;
      } else {
        currPlayer = PLAYER_ONE;
      }
    }
    
    if (t.getWinner() != TicTacToe.BLANK_SQUARE) { // someone wins
      JOptionPane.showMessageDialog(null, "Player " + t.getWinner() + " won!");
    } else { // it's a draw
      JOptionPane.showMessageDialog(null, "It's a draw!");
    }
    
  }
  
}