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.

  1. Add a private method called imagesInOrder(). It should take no parameters and return a boolean.
  2. Create an integer called id, initialized to 0.
  3. Create a boolean called inOrder, initialized to true.
  4. Potentially loop through all the rows and columns of tile, continuing while inOrder is true. Use integers row and col for the iteration variables. In the for loop for row, continue while row is less than gridSize and inOrder is true. Write similar code in the for loop for col.
  5. Create an integer called currentId, initialized to the imageId of the tile at row and col. Use TileButton’s getImageId() method.
  6. If currentId is not equal to id, set inOrder to false.
  7. Increment id.
  8. Return inOrder.
    ...
  
  _______________ imagesInOrder(____) {
    __________ id = _____;
    __________ inOrder = ______;
    for (_________; row__________ && ______; row____) {
      for (_________; col__________ && ______; col____) {
        _________ currentId = _____________;
        if(___________________) {
          inOrder = ______;
        }
        id______;
      }
    }
    return ____________;
  }
    ...

Get hands-on with 1200+ tech skills courses.