Lab 8: Column Puzzle
Download and unzip lab8.zip, which contains all
the files you will need for this assignment. Open the
file Puzzle.java in DrJava. All the code you write for this
assignment should be added to this file.
Be sure to test your code after completing each exercise.
The Puzzle
In this lab, you'll be implementing a "column puzzle," like the one
shown here:
This toy consists of 6 columns of colored plastic tiles--3 per column,
with one space left empty. A tile can be moved vertically into the
empty space. The toy also consists of 3 horizontal wheels, each of
which can be turned to the left or right. The object of the game is
to line up the tiles so that each column consists of exactly one
color. We will represent this game on the computer screen as shown
here:
Eventually, clicking on an arrow will turn the wheel for that row,
sliding all the tiles on that row in the direction of the arrow.
Clicking on a tile that is just above or below the empty location
(shown in black) will slide that tile into the empty location.
The puzzle is implemented using the Grid file. The grid has
3 rows and 8 columns. The arrows appear in columns x=0 and x=7. The
tiles appear in columns x=1 through x=6.
drawArrows
Compile and run Puzzle.java. This will run the main
method. You should see an empty black window appear.
The main method calls the setup method, which
creates the window and calls drawArrows
and drawTiles. Complete the drawArrows method, so
that it places the "left.gif" and "right.gif" arrow images in the
appropriate locations of the grid. Test that you see these arrows
appear correctly when you run your program.
isEmpty
Complete the isEmpty method, which should only return true if
the color at (x, y) is black.
drawOneTile
Complete the drawOneTile method, which should draw one tile
in the given color. The tile should be placed in a random empty
location. Here's how:
- Assign random integers to variables x and y,
corresponding to one of the 18 possible locations where a tile can
appear in the grid.
- Repeat until the location at (x, y) is empty:
- Assign a new random location to x and y.
- Set the color at (x, y) to be the color given in the
method's parameters.
Test your code by running your program, and then
calling drawOneTile in the Interactions pane. Call it
multiple times, and make sure a new tile appears each time, and that
no existing tiles change.
drawAllTiles
The drawAllTiles method is called by setup.
Complete the drawAllTiles method, which should
use drawOneTile to draw 3 blue tiles, 3 red tiles, 3 green
tiles, 3 yellow tiles, 3 magenta tiles, and 2 cyan tiles. Run
your program and make sure you see these 17 tiles appear.
swapTiles
Complete the swapTiles method, which should swap the tile at
(x1, y1) with the tile at
(x2, y2).
Test your code by running your program, and then
calling swapTiles in the Interactions pane.
slideOneTile
Complete the slideOneTile method, which should attempt to
slide the tile at the given (x, y) location up or down
one row, into an empty location. If the tile is not above or below an
empty location, then this method should do nothing.
If the location immediately above (x, y) is empty, then
slide the tile up. (Of course, if (x, y) is in the top
row, then there won't be a location above it.) If, instead, the
location immediately below (x, y) is empty, then slide
the tile down. Call the swapTiles method to slide the tile.
Test your code by running your program, and then
calling slideOneTile in the Interactions pane. Be sure to
test a variety of cases.
slideLeft
Complete the slideLeft method, which should slide all the
tiles in row y to the left, by one column. The leftmost tile
should wrap around to become the rightmost tile. Here's how:
- Save the color of the leftmost tile in row y in appropriate
variables.
- Swap the tile in column x=1 with the tile in column x=2.
- Swap the tile in column x=2 with the tile in column x=3.
- Swap the tile in column x=3 with the tile in column x=4.
- Swap the tile in column x=4 with the tile in column x=5.
- Swap the tile in column x=5 with the tile in column x=6.
- Set the color of the rightmost column in row y to be the
color you saved earlier.
Test your code by running your program, and then
calling slideLeft in the Interactions pane.
slideRight
Complete the slideRight method, which should slide all the
tiles in row y to the right, by one column. The rightmost tile
should wrap around to become the leftmost tile. The code should be
very similar to slideLeft, with a few key differences. Which
tile's colors should you save at the beginning of your method? Which
two tiles should you swap first? Which two tiles should you swap
next? Which location should you set the color of, at the end of your
method?
Be sure to test your code.
play
Modify the main method so that, after setting up the puzzle,
it calls the play method, which should let the user attempt
to solve the puzzle.
The play method repeatedly waits for the user to click on a
location of the grid, and stores the coordinates of that location in
(x, y). Insert code inside the loop. If the user
clicked on an arrow, call slideLeft or slideRight to
slide the appropriate row in the given direction. If the user clicked
on a tile, call slideOneTile.
Now run your program and try to solve your puzzle.
Challenges
- Modify your program so that it informs you when you solve the
puzzle.
- Modify your program so that it shows the number of moves you've
made.