Java Reference
In-Depth Information
An example project
14
In this chapter we present a small project based on the model-view-control ap-
proach. The aim is to implement a game that can be interactively played by the user.
The design and implementation are described in some detail because this example
is also to serve as a template for further projects. The aim is not to design a stylish
layout, but a working graphical interface.
14.1
Specification
Our example game is the 15-puzzle .Itconsists of a 4
4 board with 15 movable
blocks and one free space. One can move a block adjacent to the empty place in
that direction. See Figure 14.1 for an example.
The user interface has to display the board and enable the user to move blocks.
The implementation has to ensure that only legal moves are possible. These are
moves that can be performed in the real puzzle.
×
14.2
The model part
The first step in the model design is to find out what the essential components in
the project are. Here it is quite natural that the 'hardware' of the game, the board,
is one such component. Other components are more abstract, such as the current
configuration of blocks on the board and the concept of a move. Every essential
component should be implemented into a class of its own. In our example, it is
more or less clear what the essential components are. In more complex projects
there might be different ways to structure the problem, which would result in
different definitions of the essential components. Which of the solutions is better
cannot always be decided. In any case the use of large, all-purpose classes should
be avoided.
The model part of the puzzle is implemented in the classes BoardModel , Move-
Model , ConfigurationModel and Constants . They offer all the methods to make
moves in an abstract way. In addition, class BlockPuzzleTest is a non-graphical
Search WWH ::




Custom Search