|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object PuzzleModel
This is the model for a sliding puzzle game. The model keeps track of the location of n^2 squares, for n specified when the model is constructed. A grid if n^2-1 visible squares is represented by the model, one square is "the blank square".
Clients click on a square adjacent to the blank square and the clicked on square swaps with the blank. This simulates moving the clicked square into the blank spot --- and the previous location of the clicked square is now blank. All communication between a model and its views is handled by the model's Controller. The controller can have multiple views, but the model has a single controller in the current architecture.
Notes on Implementation
The model is conceptually a grid of n x n elements. However, the current implementation models this as a single array of n x n elements --- and the elements are int values.
The idea is that originally, array[k] == k where array is the state that represents the model. When two values are swapped, they change locations in the array. Thus, when a client wants to move square number 7, this square must be searched for. That is the value 7 is searched for in the array that represents the model. This happens for the blank square too --- both the move and the blank are searched for in the entire grid/array. Then the code currently implemented determines if these elements (move and blank) are neighbors. If so, they're swapped and the controller updates the views.
This means that making a move is an O(n^2) operation for an n x n grid.
Constructor Summary | |
PuzzleModel(PuzzleController control,
int size)
Creates a model with the specified controller and size. |
Method Summary | |
int |
getSize()
Returns the dimension of one side of the grid represented by this model. |
boolean |
makeMove(PuzzleMove move)
Attempts to make a move and returns true if the move was successfully made, else returns false. |
boolean |
undo()
Undoes the last move (if there is one) and returns true iff another undo is possible |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
public PuzzleModel(PuzzleController control, int size)
control
- mediates communication between model and its viewssize
- is the size of one square of the modelMethod Detail |
public boolean makeMove(PuzzleMove move)
move
- is the move being attempted in this model
public boolean undo()
public int getSize()
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |