import javax.swing.*;
import java.awt.*;
import javax.swing.border.*;

/** A graphical user interface for tic-tac-toe. */
public class TTTWindow extends JFrame {
  
  /** The tic-tac-toe game. */
  private TicTacToe game;
  
  /** Display for each element of the board. */
  private JLabel[][] visuals;
  
  /** A new GUI to display tic-tac-toe game g. */
  public TTTWindow(TicTacToe g) {
    
    this.game = g;
    
    Container cont = this.getContentPane();    
    
    // create the grid layout
    cont.setLayout(new GridLayout(g.BOARD_SIZE, TicTacToe.BOARD_SIZE));
    
    // Create 2D array to store the JLabels
    this.visuals = new JLabel[TicTacToe.BOARD_SIZE][TicTacToe.BOARD_SIZE];
    
    // Create the JLabels
    for (int r = 0; r < TicTacToe.BOARD_SIZE; r++) {
      for (int c = 0; c < TicTacToe.BOARD_SIZE; c++) {
        
        // creating a proper JLabel  
        JLabel jl = new JLabel("" + g.getMark(r, c));
        jl.setBorder(new LineBorder(Color.BLACK));
        jl.setHorizontalAlignment(SwingConstants.CENTER);
        jl.setVerticalAlignment(SwingConstants.CENTER);
        jl.setFont(new Font(null, Font.BOLD, 50));
        
        // adding the JLabel to the contentPane
        cont.add(jl);
        
        // storing the address of the JLabel for future updates
        this.visuals[r][c] = jl;
      }
    }    
    
    this.pack();
    this.setVisible(true);
  }
  
  /**
   * Update the JLabel to match the corresponding element
   * of the character board.
   */
  public void update(int r, int c) {
    this.visuals[r][c].setText("" + this.game.getMark(r, c));
  }
}