import java.awt.*;
import java.awt.event.*;

        /* The basis for a Noughts and Crosses (TicTacToe) game
         * using a GridLayout of Buttons.
         * This class simply creates a 3x3 grid of buttons
         * as a View of a TicTacToe game.
         */
class TicTacToeFrame extends Frame implements ActionListener {
    public TicTacToeFrame(){
        final int width = 200, height = 200;
        setSize(width,height);
        setBackground(Color.white);

        // Use a Grid Layout
        setLayout(new GridLayout(rows,cols));

        // Add each button to the frame and the board.
        Button[][] board = getBoard();
        for(int r = 0; r < rows; r++){
            for(int c = 0; c < cols; c++){
                Button square = new Button("");
                // Give it a unique action command corresponding to
                // its index.
                square.setActionCommand(String.valueOf(r*cols+c));
                // Tell us about button presses.
                square.addActionListener(this);
                board[r][c] = square;
                add(square);
            }
        }

    }

        // Work out which square was selected from the value
        // stored in its action command.
    public void actionPerformed(ActionEvent event){
        String command = event.getActionCommand();

        // See if it was a number
        try{
            int value = Integer.parseInt(command);
            if((0 <= value) && (value < (rows*cols))){
                // Ok.
                if(selectSquare(value)){
                    // Swap symbol for next time.
                    toggleSymbol();
                }
                else{
                    System.out.println("Square already taken.");
                }
            }
        }
        catch(NumberFormatException e){
            // It wasn't.
            System.out.println("Unknown command: "+command);
        }
    }

        // Select the given squares and set the appropriate symbol.
        // Return true if the square was free.
    protected boolean selectSquare(int value){
        int row = value/cols;
        int col = value%cols;
        Button[][] board = getBoard();
        Button square = board[row][col];

        if(square.getLabel().equals("")){
            square.setLabel(nextSymbol);
            // Redisplay the board.
            repaint();
            return true;
        }
        else{
            return false;
        }
    }

    protected String getSymbol(){
        return nextSymbol;
    }

        // Change the symbol to be use for the next square.
    protected void toggleSymbol(){
        if(nextSymbol == Nought){
            nextSymbol = Cross;
        }
        else{
            nextSymbol = Nought;
        }
    }

    protected Button[][] getBoard(){
        return board;
    }

    // The board's dimensions.
    private static final int rows = 3, cols = 3;
    // The strings used for the two choices.
    private static String Nought = "O", Cross = "X";
    // Which symbol to use next.
    private String nextSymbol = Nought;
    // The board of buttons.
    private Button[][] board = new Button[rows][cols];
}
