import javax.swing.*;

public class TTTGame {

  /** The letter representing player one. */
  private static final char PLAYER_ONE = 'X';
  
  /** The letter representing player two. */
  private static final char PLAYER_TWO = 'O';
  
  public static void main(String[] args) {
    TicTacToe t = new TicTacToe();
    TTTWindow w = new TTTWindow(t);
    char currPlayer;
    
    // if the user passes in an argument
    if(args.length > 0) { 
      currPlayer = args[0].charAt(0);
    } else {
      currPlayer = PLAYER_ONE;
    }
    
    w.pack();
    w.show();
    
    // while nobody won and there is no draw
    while(t.whoWon() == '\0' && !t.isDraw()) {
      
      // Prompt the player to make a move
      int row = Integer.parseInt(
        JOptionPane.showInputDialog(null, 
        "Player " + currPlayer + ": pick the row: "));
      
      int col = Integer.parseInt(
        JOptionPane.showInputDialog(null, 
        "Player " + currPlayer + ": " + "Pick the column: "));
      
      // set the mark on the board
      t.setMark(row, col, currPlayer);
      
      // update the window
      w.update(row, col);
      
      // switch to the other player
      if(currPlayer == PLAYER_ONE) {
        currPlayer = PLAYER_TWO;
      } else {
        currPlayer = PLAYER_ONE;
      }
    }
    
    // if someone won
    if(t.whoWon() != '\0')  {
      JOptionPane.showMessageDialog(null, 
        "Winner is " + t.whoWon());
    } else {
      JOptionPane.showMessageDialog(null, "Draw");                                 
    }
  }
}
