Lecture 14: In-class Exercise: A Graphical View and Controller for Tic Tac Toe
1 A View and Controller for Tic Tac Toe
The purpose of this exercise is to give you practice with implementing the View and Controller components of the Model-View-Controller design pattern, by means of a graphical user interface using Java’s Swing library.
You may work freely with other students on this exercise!
In the starter code, you are given an interface representing a controller for Tic Tac Toe, with two methods, playGame()
and handleCellClick()
.
You are also given an interface for a view, TTTView, which is coupled to the controller interface, as it has a method addClickListener
which
takes a TicTacToeController as an argument.
Your task is to implement the two interfaces to create an interactive, graphical Tic Tac Toe game.
Put your new controller and view code in the same package alongside your Tic Tac Toe model as it will depend on the model.
You are also given a class Main
with a main
method that will allow you to test your game interactively.
You will need to create four classes:
a public class that implements
TicTacToeController
, with a single public constructor that takes one argument of type TTTView. This class will have two fields, a model (TicTacToe
) and a view (TTTView
).a public class that implements
TTTView
and extendsJFrame
. Its constructor should take in aReadonlyTTTModel
(note that the model interface has been refactored into two related interfaces).a class that extends
JPanel
and overridespaintComponent
that draws the game board and game status. Status includes indicating whose turn it is, and if the game is over, who won (or if it was a tie).a class that extends
MouseAdapter
and overridesmouseClicked
to capture a click on the game board and pass it to the controller.
You will also need to fill in the parts of the main
method in Main.java as noted in the comments in that method.
2 Testing
Test your view by running it, inspecting the view, clicking on the game board and playing the game. No automated tests here.
3 Notes to Keep in Mind
You will likely want to refer to the sample code on graphical programs, such as the Turtle Graphics example (see the "solution code" link).