Java Reference
In-Depth Information
(a)
(b)
(c)
(d)
Figure 14.1 The 15-puzzle: (a) some configuration of the 15 blocks; (b), (c) and (d) are
derived from (a) by moving one block at a time. The aim is to arrange the blocks in such a
way that the numbers increase when read row-wise
driver to test the functionality of the non-graphical classes. One might argue
whether a class for moves or constants is really necessary for this simple game.
In fact, the use of these classes here helps to structure the project, speeds up the
design, and makes the program easy to maintain.
The BoardModel is not restricted to a size of 4
m
board. The parameters determining the size are passed in the constructor. The
blocks are numbered 0
×
4 but allows an arbitrary n
×
1; where 0 stands for the empty place. The
board is represented by the two-dimensional n
,
1
,
2
,...,
nm
m array board . The array stores
at board[i][j] the number of the block in row i column j .Two other arrays
rowOfBlock and colOfBlock store for every block the number of the row and
column that it is currently in. This way positions and numbers of the blocks can
be easily linked. The board is initialized with the blocks consecutively numbered
in a row-wise fashion with the empty place at the lower right corner.
The class supplies get -methods to access the current configuration and the
board size. The dynamics of the game is provided by method moveIt .Itreceives
an instance of MoveModel , checks whether the move is legal and - if so - moves
the block. It returns a boolean value to indicate whether the move has been made
or not.
×
Search WWH ::




Custom Search