Introduction

Many games we like to play in our cell phones are of type sliding tiles. Examples of such kind of games are shown below:

  • Rush Hour Game
  • 15 Piece Tile Game
  • Frog Crossing road and River Game

In this take home exam, you will create a tile game similar to the one shown above, the 15 Piece tile game. Please see the following video to understand how to play this game.

http://youtu.be/sXqoM7u4-t8

Since we have not learned how to create Graphical User Interfaces yet, your program shall interface with the user through a terminal window, using the keyboard as input, shown in the second part of the video above.

The following Java concepts will be asked in this exam:

  • primite variables (int, double, boolean, etc)
  • While loops
  • If loops
  • Boolean conditions
  • Boolena operators
  • Arrays
  • Methods
  • Classes
  • Class Fields and Methods

To help you to create this game, I have created a step-by-step procedure for you to follow. If you get stuck in one step, do not proceed to the next one; ask the UTAs, TA, or me before proceeding.

  • Create a new Java application project named < FirstName>_< LastName>_TileGame in NetBeans
  • A new Java class, named after the project name (e.g., < FirstName>_< LastName>_TileGame ) will be created by NetBeans and displayed to you. In its main() method, make a call to this class constructor, so we avoid those "static" problems
  • Leave this class aside for a moment. We will now create a second class, which will represents the game board. Create a new class in this project named GameBoard
  • The GameBoard class shall have two fields:
    • a primitive variable containing the maximum number of rows (that will be the same as the number of columns since the board is a square). See next item to figure out its value.
    • a two dimensional array of numbers that will contain the current position of the tiles in the board. For this specific game, the numbers are the labels shown on top of each tile and go from 1 to 15. The empty spot position in the board will also be considered as a tile, with the label "0" on it. The tiles are surrounded by the board walls, that are represented by "-1" in our 2D array variable. Therefore, your 2D array shall be able to hold the following data:
-1 -1 -1 -1 -1 -1
-1 1 2 3 4 -1
-1 5 6 7 8 -1
-1 9 10 11 12 -1
-1 13 14 0 15 -1
-1 -1 -1 -1 -1 -1
  • In the GameBoard class create a method that prints the current position of all the tiles in the board, such as shown below, to the terminal window. Name this method as printGameBoard(). Pay attention to details! The numbers are aligned in the printout. How can you accomplish that?
1 2 3 4
5 6 7 8
9 10 11 12
13 14 0 15
  • In your GameBoard() class constructor, add a call to the printGameBoard(), so that everytime a new GameBoard object is created (e.g., GameBoard gameBoard = new GameBoard()), the first thing that this object does is to print the tiles position in the screen.
  • Going back to the other class < FirstName>_< LastName>_TileGame, in the class constructor create a new object of the GameBoard class (by using the new GameBoard() command)
  • Test your program so far. Run it and you should get a display of the board in your terminal window. If you do not get, go back to the previous steps and see what is wrong. If you get stuck, call for help.
1 2 3 4
5 6 7 8
9 10 11 12
13 14 0 15
BUILD SUCCESSFUL (total time: 0 seconds)
  • This game is based on user input via keyboard. The user shall enter numbers from 1 to 15 that represent the tile labels. The user can also enter -1 to quit the game. In the < FirstName>_< LastName>_TileGame constructor, create an object that will be used to read keyboard input.
  • Still in < FirstName>_< LastName>_TileGame constructor, put a logic that will keep asking the user to enter a number (what type this number should be? Integer? Double? Think about), until the user enters -1. At this point the program should terminate.
  • Test your code so far. Try to enter the following values, 2, 13, -1. An example of what you should see in your terminal window is shown below:
1 2 3 4
5 6 7 8
9 10 11 12
13 14 0 15
Enter tile number: 2
Enter tile number: 13
Enter tile number: -1
BUILD SUCCESSFUL (total time: 15 seconds)
  • Back to the GameBoard class We need to create few methods that will use when playing the game. We will create them one by one. Just follow the items below. The first method that will be very useful for us is one that for a given label number it tell us were the tile is currently located in the board. Create a method with a signature as shown below: public Point2D getTilePosition(int tileNumber) Hint: use two FOR loops (nested loops, one for the board row and other for the board column) to go over the 2D game board variable (as implemented in item #4) to figure out where this tileNumber is located. After finding the row and column of the searched tile, create a Point2D object and set its x and y values to the row and column you just found, and return this object to the caller function (thats your method output). Just for testing purposes, put a print statement that shows the location row and column in this method in the terminal window.
  • Test your code again. Just to test your code, place a call to getTilePosition() method just after the user enter the tile number in your < FirstName>_< LastName>_TileGame constructor. Rerun the test case from item 11. You should get the following results:
1 2 3 4
5 6 7 8
9 10 11 12
13 14 0 15
Enter tile number: 2
Tile Number: 2 Row: 1.0 Col: 2.0
Enter tile number: 13
Tile Number: 13 Row: 4.0 Col: 1.0
Enter tile number: -1
BUILD SUCCESSFUL (total time: 29 seconds)
  • To move a tile to where the empty spot is, we need to check first if the empty spot is in reality next to the tile. The empty spot can be on top, on the left, on the right, or below the tile. If the empty spot is in one of these neighbor locations, then we can move the tile. Create a method in the GameBoard with the following signature: private boolean isEmptySpotANeighborOfTile(int tileNumber) Inside this method, you should call the method developed in item 13 for getting the location of the tile and the empty spot. Hint: this method will contain 4 IF statements to check if the empty stop is right above, right below, or to the left or to the right of the tile.
  • Test your code. Just to debug your code, put a print statement in the method developed in Step 14 to tell if the tile has an empty spot as neighbor. Run the following test case and see if you obtain the same results.
1 2 3 4
5 6 7 8
9 10 11 12
13 14 0 15

Enter tile number: 2
Tile Number: 2 Row: 1.0 Col: 2.0
Tile Number: 0 Row: 4.0 Col: 3.0
Has tile an empty spot as neighbor? false
Enter tile number: 11
Tile Number: 11 Row: 3.0 Col: 3.0
Tile Number: 0 Row: 4.0 Col: 3.0
Has tile an empty spot as neighbor? true
Enter tile number: 14
Tile Number: 14 Row: 4.0 Col: 2.0
Tile Number: 0 Row: 4.0 Col: 3.0
Has tile an empty spot as neighbor? true
Enter tile number: 15
Tile Number: 15 Row: 4.0 Col: 4.0
Tile Number: 0 Row: 4.0 Col: 3.0
Has tile an empty spot as neighbor? true
Enter tile number: -1
BUILD SUCCESSFUL (total time: 36 seconds)
  • Almost there! The next method we need to create is one that moves the tile to the empty stop in case the empty spot is a neighbor. Create the following method: public void moveTile(int tileNumber) This method shall be called only if the tile has an empty spot as its neighbor. This method switches the empty spot and the tile locations in the board. Place a call for this method in the class < FirstName>_< LastName>_TileGame constructor, right after the call to isEmptySpotANeighborOfTile() (but remember, you will need an IF statement and call moveTile() only if there is an empty spot to move into).
  • Test your code: Try to reproduce the following results:
1 2 3 4
5 6 7 8
9 10 11 12
13 14 0 15
Enter tile number: 2
Tile Number: 2 Row: 1.0 Col: 2.0
Tile Number: 0 Row: 4.0 Col: 3.0
Has tile an empty spot as neighbor? false
Enter tile number: 14
Tile Number: 14 Row: 4.0 Col: 2.0
Tile Number: 0 Row: 4.0 Col: 3.0
Has tile an empty spot as neighbor? true
Tile Number: 14 Row: 4.0 Col: 2.0
Tile Number: 0 Row: 4.0 Col: 3.0

1 2 3 4
5 6 7 8
9 10 11 12
13 0 14 15

Enter tile number: 10
Tile Number: 10 Row: 3.0 Col: 2.0
Tile Number: 0 Row: 4.0 Col: 2.0
Has tile an empty spot as neighbor? true
Tile Number: 10 Row: 3.0 Col: 2.0
Tile Number: 0 Row: 4.0 Col: 2.0

1 2 3 4
5 6 7 8
9 0 11 12
13 10 14 15

Enter tile number: 6
Tile Number: 6 Row: 2.0 Col: 2.0
Tile Number: 0 Row: 3.0 Col: 2.0
Has tile an empty spot as neighbor? true
Tile Number: 6 Row: 2.0 Col: 2.0
Tile Number: 0 Row: 3.0 Col: 2.0

1 2 3 4
5 0 7 8
9 6 11 12
13 10 14 15

Enter tile number: -1
BUILD SUCCESSFUL (total time: 28 seconds)
  • We have created all the basic methods to play this game. We just need to have a way of checking if the game is over, by checking if each tile is in the right location. If they are, your code should display a message saying that the user has won the game. Create the following method: public boolean isGameOver() Hint: this method has the same kind of nested FOR loops as the ones created in item 12. You can create a second 2D array variable containing the correct locations of the tiles to win the game and them check if the 2D game board has the same locations. Place a call to this method after you move the tile.
  • Test your code: This is the final steps on winning the game
1 2 3 4
5 6 7 8
9 10 11 12
13 14 0 15
Enter tile number: 15
Tile Number: 15 Row: 4.0 Col: 4.0
Tile Number: 0 Row: 4.0 Col: 3.0
Has tile an empty spot as neighbor? true
Tile Number: 15 Row: 4.0 Col: 4.0
Tile Number: 0 Row: 4.0 Col: 3.0

1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 0
You Won!
BUILD SUCCESSFUL (total time: 5 seconds)
Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.