The Tile Board Game

tile01

1 Game Logic

  1. The board is initialized with the tiles in some initial location (as the one shown in the figure above).
  2. The user clicks on a given tile, such as 6.
  3. The program checks if there is an empty spot close to this tile (empty spot can be located above, left, right, or down).
  4. If there is an empty spot, move the tile to the new position. If there is no empty spot, no tile is moved.
  5. After moving the tile to its new position, check if game is over (all the tiles should be in sequential order as shown below). If game is over, show a message stating that for the player.

tile02

2 Java Program

In this first phase you will create a terminal window application. Later on we will create the GUI for it.

  1. Create a new NetBeans project named TileGame.
  2. NetBeans automatically creates a main class. This main class will be using a second class named GameBoard. Before implementing the TileGame, let’s work on the GameBoard class first.

2.1  The RequiredMethodsInterface

  1. The GameBoard class will implement the methods defined in an interface named RequiredMethodsInterface.java. This interface was defined by the software group that will be developing the TileGame.java class. The interface methods are shown in item (3) below.
  2. Create the RequiredMethodsInterface.java Interface file inside your project using NetBeans IDE.
  3. Place the following method signatures in the interface file:

import java.awt.geom.Point2D;

public interface MethodsInterface {

   // returns true if there is an empty spot close to tileNumber

    public boolean tileHasAnEmptyNeighbor(int tileNumber);  // Group A

    // moves tileNumber to the empty spot.

    public void moveTile(int tileNumber);  // Paulo

    // prints the board on the terminal window

    public void printGameBoard();  Group B

    // Get the position of a given tile number in the 2D board

    public Point2D getTilePositionInTheBoard(int tileNumber);  // Group C

    // Checks if the all the tiles are in their correct location

    public boolean isGameOver();  // Group D

}

 

2.2  The GameBoard Class

  1. Create a new class inside your project called GameBoard. This class shall implement the interface
  2. In the GameBoard class create a class field 2D array variable representing the board as shown below:

  int[][] gameBoard =

      { { -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 } };

 

  1. At this point, NetBeans should be complaining that this class has not implemented the interface parameters yet. Left click on the small red dot that references this error and select “implement all the methods defined in the interface”.
  2. Start implementing the method assigned to your group.

Hints:

  • A tile position is defined by a pair of number defining its row and column location in the board, e.g., the Tile 6 in the first figure of this document is located at row 2, column 3. Use the Java API Point2D class to work with tile locations. An example on how to create and use a tile location using Point2D is given below:

Point2D emptySpotLocation = new Point2D.Double(row,col);

emptySpotLocation.setLocation(2, 3);

tilePosition.getX()

emptySpotPosition.getY()

  • Working with the 2D array board game:

gameBoard[row][col] = some integer value here” //  updating a value

gameBoard[row][col] == value”  //  checking for a given value

 

  • In many methods you will need to use 2 nested FOR loops to scan the values from the boardGame 2D array.
  • I am giving you the implementation of this method :

 

public void moveTile(int tileNumber) {

   Point2D tilePosition = getTilePositionInTheBoard(tileNumber);

   Point2D emptySpotPosition = getTilePositionInTheBoard(0);

   gameBoard[(int)tilePosition.getX()][(int)tilePosition.getY()] = 0;

   gameBoard[(int)emptySpotPosition.getX()][(int)emptySpotPosition.getY()] = tileNumber;

}

2.3 Back to the TileGame class

Now that your and the other groups have created the supporting class (GameBoard in Section 2.2), another group (Group E) will be responsible for creating the application that will use all the GameBoard methods to make the game alive!

The TileGame class shall follow the logic described in Section 1. Here is the beginning of it:

public static void main(String[] args) {

        new TileGame();

    }

    private TileGame() {

        gameBoard = new GameBoard();

        playGame();

    }

private void playGame() {

 

       // get user input (tile number) from keyboard (item b of Section 1)

       // continue the logic shown in Section 1

    }