Write a TicTacToe subclass of TwoPlayerGame and a TicTacToePlayer class that implements the IPlayer interface to play a computer-based version of TicTacToe. Although it is not necessary to write an applet GUI for this project, it might be fun to do so. Here's an example
Note: This project is considerably more challenging than the Skip Cipher Project. It requires the use of an array to store the Tic Tac Toe board as well as advanced GUI elements, such as GridLayout and ImageIcons.
The TicTacToe class should be a subclass of abstract TwoPlayerGame and it should implement either (or both) the CLUIPlayableGame or the GUIPlayableGame interface. See Figures 8.19 and 8.23 for details on the superclasses.
This means that in addition to whatever instance methods and instance variables you need for your Tic Tac Toe game, your TicTacToe class must have the following method implementations:
The following methods are inherited from the IGame and GUIPlayableGame interfaces and must be given implementations in TicTacToe (assuming a GUI is used).
Of course, the TicTacToe class must be designed to keep track of things like the state of the game board, whose turn it is, and so forth. Some of these functions (whose turn) can use methods inherited from the TwoPlayerGame class.
The TicTacToePlayer class must implement the IPlayer interface, which means it must provide an implementation of the following method:
Download the TicTacToeApplet.java file. This file is complete and needn't be changed. However, it assumes that two image files, x.gif and o.gif, store the graphical X and O icons that are used in the game.
The applet assumes you have implemented a TicTacToeGUI class that contains a constructor with the following signature: TicTacToeGUI(ImageIcon oIcon, ImageIcon xIcon). The reason for this is that because of Java's security requirements the applet has to load the images, which it then passes to the GUI.
Your GUI class will need ImageIcon instance variables. Suppose you store the nine JButtons representing the Tic Tac Toe square in an array. Then here is how you would associate an ImageIcon with the kth button. In this example, board[k] is a JButton and xButtonIcon is the ImageIcon:
board[k].setIcon(xButtonIcon);
To arrange the TicTacToe board into a 3 x 3 matrix, you can simply use a GridLayout. In this example the eight JButtons that make up the visible Tic Tac Toe board are contained in a JPanel:
buttons = new JPanel();
buttons.setLayout(new GridLayout(3,3)); // Arranges components in a 3 x 3 grid
buttons.setBorder(new TitledBorder(prompt));
// Here you need a loop to add each button to the panel:
board[k] = new JButton(""+k); // Buttons are initially labeled 0..8
board[k].addActionListener(this); // Each button has a listener
buttons.add(board[k]); // Just add the button to the grid
Note that in the demo applet, the squares on the board (the JButtons) are simply labeled 0 through 8. The icons are only associated with the board after the user or the computer has made a move. When a new game is started, the button's labels go back to their initial state.
In your actionPerformed() method, you can use a loop to determine which of the eight buttons was clicked and then take appropriate action.
javac -classpath nplayer.jar:. *.java // Compiles all java files javac -classpath nplayer.jar:. TicTacToeApplet.java // Compiles the applet and all files it uses
<applet codebase="./" archive="nplayer.jar, images.jar" code="TicTacToeApplet.class" width=500 height=500 > </applet>