End and Start the New Game
Learn how to fill In the missing tile when the puzzle is solved and set a way to start a new game.
Filling in the missing tile when the puzzle is solved
When the puzzle is solved, SlidingTiles
will need to fill in the missing portion of the image. How will the SlidingTiles
check whether the puzzle is solved? By checking whether all the image IDs are in order.
- Add a private method called
imagesInOrder()
. It should take no parameters and return a boolean. - Create an integer called
id
, initialized to0
. - Create a boolean called
inOrder
, initialized to true. - Potentially loop through all the rows and columns of tile, continuing while
inOrder
is true. Use integersrow
andcol
for the iteration variables. In the for loop for row, continue whilerow
is less thangridSize
andinOrder
istrue
. Write similar code in the for loop forcol
. - Create an integer called
currentId
, initialized to theimageId
of the tile atrow
andcol
. Use TileButton’sgetImageId()
method. - If
currentId
is not equal toid
, setinOrder
tofalse
. - Increment
id
. - Return
inOrder
.
...
_______________ imagesInOrder(____) {
__________ id = _____;
__________ inOrder = ______;
for (_________; row__________ && ______; row____) {
for (_________; col__________ && ______; col____) {
_________ currentId = _____________;
if(___________________) {
inOrder = ______;
}
id______;
}
}
return ____________;
}
...
Get hands-on with 1200+ tech skills courses.