import java.awt.*; import java.awt.event.*; /** * This class provides the user interface and control for the game Othello. * * @Hal Perkins, Robert Carr * @version August 5, 2001 */ public class Othello extends Panel { // game instance variables private Board board; // game board // GUI components private Label blackScore; // black and white scores private Label whiteScore; private Label turn; // Turn indicator private Button resetButton; // reset control private Square[][] squares; // board squares for display /** Construct new Othello game */ public Othello() { // create and initialize game board and display representation this.board = new Board(this); this.squares = new Square[Board.NROWSCOLS][Board.NROWSCOLS]; // set layout for game display this.setLayout(new BorderLayout()); // Set up panel containing scores Panel infoPanel = new Panel(); infoPanel.setLayout(new GridLayout(1,3)); this.blackScore = new Label("black: " , Label.LEFT); this.whiteScore = new Label("white: " , Label.RIGHT); this.turn = new Label("current turn: ", Label.CENTER); infoPanel.add(this.blackScore); infoPanel.add(this.turn); infoPanel.add(this.whiteScore); this.add(infoPanel, BorderLayout.NORTH); // Create board squares and add to display Panel boardPanel = new Panel(); boardPanel.setLayout(new GridLayout(Board.NROWSCOLS,Board.NROWSCOLS)); for (int row = 0; row < Board.NROWSCOLS; row++) { for (int col = 0; col < Board.NROWSCOLS; col++) { squares[row][col] = new Square(Board.EMPTY, row, col, this.board); boardPanel.add(squares[row][col]); } } this.add(boardPanel, BorderLayout.CENTER); // Set up reset button so it starts new game when clicked resetButton = new Button("new game"); resetButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { board.newGame(); updateDisplay(); repaint(); } }); this.add(resetButton, BorderLayout.SOUTH); // show initial display this.updateDisplay(); } // Update display to match game state. public void updateDisplay() { // update scores this.whiteScore.setText("white: " + this.board.getWhiteScore()); this.blackScore.setText("black: " + this.board.getBlackScore()); String currentTurn = "current turn: "; if (this.board.getCurrentColor() == Board.WHITE) { currentTurn = currentTurn + "WHITE"; } else { currentTurn = currentTurn + "BLACK"; } this.turn.setText(currentTurn); // update board display for (int row = 0; row < Board.NROWSCOLS; row++) { for (int col = 0; col < Board.NROWSCOLS; col++) { this.squares[row][col].setState(this.board.getSquare(row,col)); } } this.repaint(); } /** Create new game and a window to display it */ public static void main(String[] args) { Frame f = new Frame("Othello"); // top-level window Othello o = new Othello(); f.add(o); f.addWindowListener( new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); f.setSize(400,400); f.show(); } }