COMP1406 - Tutorial
#5
Action Listeners
![]() |
![]() |
Purpose:
The purpose of this tutorial is to give you practice writing action listeners for JButton GUI objects. This tutorial is based on the action listeners described in section 4.4 of the course notes and the update() architecture described in section 5.4 of the notes. In addition you will get more practice using if-statements.
This tutorial involves completing a tic tac toe, or "X"'s and "O"'s game. To get credit for this tutorial your game has to be able to beat the TA, or at worst tie.
Demonstration:
Open the java file TicTacToeGUI.java within JCreator and examine the code. Also open TicTacToeModel.java. Compile the program and run it. It should produce an window as shown below.
The demonstration program is a Tic Tac Toe,
or X's and O's, game. There is a model class that represents the
TicTacToe game and rules. The model class is complete and will require
no modification to get the game working but it does not play a very
smart defending strategy. There is also a
TicTacToeGUI class that represents the window and interface. The
model does not know anything about the interface -as it should be.
Also, the model uses a single array to store the game positions. The
GUI, on the other hand, "thinks" in terms of rows and columns on the
game board. You will notice code to translate between the model's
"positions" and the GUI's "rows" and "columns". This kind of miss-match
is very common in programming where one world must translate into the
co-ordinate system of another world. Notice also that the code is based
on an on-paper model where rows are numbered 1,2,3 and columns are
numbered 1,2,3 the way non-programmers would number things. Array
positions, in languages like Java, however are numbered 0,1,2,... etc.
Again this is a common miss-match between the real world and the
programming world. Be aware of this.
When the demonstration program opens it
creates a GUI of 9 buttons that are meant to represent an X's and O's
game. Currently, nothing happens though when you click on the buttons.
What is intended is that the button's label be changed to an "X" or an
"O" based on the state of the game model. Your task is to write the
action listeners so that when the user clicks on a button the game
model is consulted and the buttons get properly labelled in the update() process. The code uses the update() architecture described in section
5.4 of the notes, but this part of the code is already built for you,
you need only write action listeners as described in section 4.4 of the
course notes.
Problem 1
Action Listeners
Examine the TicTacToeGUI.java code. You will notice some places commented with //MISSING CODE and additional comments about what is to happen at that point the code. To make the game work you will have to fill in the missing code. Here are the specific items you will have to add.
1) You will have to create an action listener actionPerformed() method and add it to the code. Use the technique described in section 4.4 whereby the TicTacToeGUI class implements the ActionListener interface.
2) You will have to add the action listener to each of the of the JButton objects.
3) Your actionPerformed() method will have to communicate with the game model to make the move intended by the user's button click. To do this your action listener will have to have a statement like model.XPlays(i,j) where i,j are the row and column, numbered from 1,2,3 represented by the button that the user clicked.
After completing this problem the game should work. That is, the user should be able to click on a button and have an "X" appear as their move, and the game will make a defending move.
Problem 2
Defence, Defence
If you completed problem 1 you will notice the game makes a defending move for each of the user's "X" moves. The game's defence strategy, however, is not very good and is easy to beat. To get credit for this tutorial you have to come up with a defence strategy that will keep the TA from being able to beat your game.
So for problem 2 you need to expand the code in the TicTacToeModel's makeADefendingMove() method so that it can better defend. This will require using if-statements that account for various threatening situations and make a defending move. The default defence strategy provided simply looks for a free square in a very basic fashion.
After completing this problem your code should be able to either beat the user (TA) or force a tie game. If the TA can beat your game then keep working on your defence strategy.
After completing problems 1 and 2 demonstrate your results to the tutorial TA to get credit for the tutorial. (Remember you only get credit if your game can beat or at worst tie with the TA)
Problem 3 (Advanced Optional Part)
You will notice that the demonstration code in TicTacToeGUI.java is rather clumsy in that the buttons are handled as individual named variables rather than a 2D array of buttons. Modify the code so that a 2D array of JButton objects is used to represent the game board. Watch out though because the game model uses numbering 1,2,3 etc. whereas the array will probably use positions numbered 0,1,2, etc.
Here are some additional refinements you can also make to the game.
1) Have a winning indicated by having the the three win-buttons be in a different color.
2) Allow the player to have repeated games. That is, when a game is over a new one starts automatically on the same game board.
3) Have a running score over multiple games.