Name __________________________                                                              Date: ___/___/____

AP CS Java  - Mr. Merlis                                                                                 Block ___4 - ACE__      

CONCENTRATION LAB

 

 

 Concentration is a game in which tiles are placed face-down in rows on a “concentration board”. Each tile contains an image. For each tile, there is exactly one other tile with the same image. The player is to pick a tile, see which image it has, and try to find its match from the remaining face-down tiles. If the player fails to turn over its match, both tiles are turned face-down and the player attempts to pick a matching pair again. The game is over when all tiles on the board are turned face-up.

 

In this implementation of a simplified version of concentration,

·          Images will be strings chosen from a FooList.

·          The concentration board will contain an even number of Tiles.

·          The Board will be implemented as a one-dimensional array of Tiles.

 

For example, if the size of the concentration board is requested to be 4, the board will have 16 Tiles. The one-dimensional array, gameboard, can be viewed as a 4-by-4 concentration board as follows:

 

gameboard[0]     gameboard[1]     gameboard[2]     gameboard[3]

gameboard[4]     gameboard[5]     gameboard[6]     gameboard[7]

gameboard[8]     gameboard[9]     gameboard[10]    gameboard[11]

gameboard[12]    gameboard[13]    gameboard[14]    gameboard[15]

 

You can find an incomplete implementation of the Tile class and the Board class in the lab.

 

 

 

a.     Write the implementation for the Board method fillBoard. The method fillBoard will randomly fill the concentration Board with Tiles whose images are randomly chosen from strings contained in the FooList possibleTileValues. A tile image that appears on the board appears exactly twice on the board. Use the header below to write fillBoard.

 

/*

   Randomly fills this concentration board with tiles. The

   number of distinct tiles used on the board is size / 2.

   Any one tile image appears exactly twice.   

   Precondition: number of positions on board is even,

   possibleTileValues contains at least size / 2 elements.

*/

private void fillBoard()

 

b.     Write the implementation for the Board method lookAtTile. This method will call the appropriate method of the Tile class to turn the Tile face-up. Assume that the method you wrote for part a and the other methods whose headers and comments are given in the problem specification work as intended. Use the header below to write lookAtTile.

      /*

   Assumes Tile in position p is face-down.

   After execution, Tile in position p is face-up.

*/

public void lookAtTile(int p)

 

c.     Write the implementation for the Board’s method checkMatch. This method will check whether the tiles in its two integer parameter positions on gameBoard have the same image. If they do, the tiles will remain face-up. If they have different images, the tiles will be turned face-down. Assume that the methods you wrote for parts a and b and the other methods whose headers and comments were given in the problem specification work as intended. Use the header below to write checkMatch.

      

 /*

   Checks whether the Tiles in pos1 and pos2 have the same image.

   If they do, the Tiles remain face-up. If not, the Tiles are turned face-down.

   Precondition: gameBoard[pos1] is face-up,

   gameBoard[pos2] is face-up.

*/

public void checkMatch(int pos1, int pos2)

 

d.     Complete the implementation for printBoard so that the concentration board is printed as described in the comment and illustrated below. You should call the Board method format to right-justify the printing of the Tile image or the printing of the Tile position. For example, if MAXPLACES = 5,

 

System.out.print(format("go", MAXPLACES));

   // will print ["   go"]

 

i = 4;

System.out.print(format(i, MAXPLACES));

   // will print ["    4"]

 

Complete the implementation of printBoard that appears below.

 

 /*

   Board is printed for the player. If the tile is turned face-up, the image is       printed. If the tile is turned face-down, the Tile position is printed.

 */

public void printBoard()

{

   final int PADDING = 3;   // spacing of tiles

   int spacing = possibleTileValues.getFooLength() + PADDING;

   for (int i = 0; i < size; i++)

   {

      // Code goes here

   }

}

 

An example of printBoard for a 4 by 4 concentration board that is partially solved is shown below after 4 matches have been found. The Foolist passed as a parameter to the Board constructor contains strings of length

 

 

fox        fox    dog    dog

 

cow       5      6      7

 

cow      cat     10     11

 

12         13     14    cat


 

2.   (AB) Suppose that the implementation of the concentration board constructed a two-dimensional array as an alternative to the one-dimensional array used above.

Board constructor

 /*

   Constructs n by n Concentration Board.

   n is an even positive integer

*/

public Board(int n, FooList list)

{

   gameBoard = new Tile[n][n];   // Concentration board

 

   size = n * n;   // number of Tiles on Board

   numberOfTilesFaceUp = 0;   // number of Tiles face up

   rowLength = n;   // number of Tiles in a row

   possibleTileValues = list;   // possible Tile values

   fillBoard();   // calls method to fill board with Tiles

}

Private instance fields

private Tile[][] gameBoard;   // concentration board of Tiles

private int size;   // number of Tiles on board

private int rowLength;   // number of Tiles printed in a row

int numberOfTilesFaceUp;   // number of Tiles face-up

private FooList possibleTileValues;   // possible Tile values

 

a.   Implement the Board method fillBoard using this two-dimensional array implementation. Use the header below to write fillBoard.

 

/*

   Randomly fills this concentration board with tiles. The

   number of distinct tiles used on the board is size / 2.

   Any one tile image appears exactly twice.

   Precondition : number of positions on board is even,

   possibleTileValues contains at least size / 2 elements

*/

private void fillBoard()

 

b.   Implement the Board method printBoard using this two-dimensional array implementation. Use the header below to write printBoard.

 

 /*

   Board is printed for the player. If the Tile is turned face-up,

   the image is printed. If the Tile is turned face-down, the Tile

   position is printed.

*/

public void printBoard()