Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The fifteen-puzzle is a well-known game comprising a squareplastic board with fi

ID: 3611158 • Letter: T

Question

The fifteen-puzzle is a well-known game comprising a squareplastic board with fifteen interlocking numbered plastic tiles andone empty space:

The tiles can slide past each other horizontally and vertically,hence any of the tiles next to the space can be moved into it:

The aim of the game is to make a series of moves that restoresthe puzzle to the goal configuration:

The aim of this lab is to write a classFifteenPuzzle that allows a user to play thisgame.

You must adhere to this specification preciselyor the marking program will be unable to mark yourcode.

The class should have one constructor, with the signaturepublic FifteenPuzzle (int[][] initialGrid).

The parameter specifies the starting position of the puzzle. Itshould be size 4 x 4 and contain the numbers 0, 1,..., 15, where 0 is used to represent the space.initialGrid[x][y] holds the number on the tile that isx units along and y units down from thetop-left hand corner.

Immediately on construction, a visual representation of thecurrent state of the puzzle should be displayed on aSimpleCanvas.

The constructor should check that the client-specified input isa valid puzzle (i.e. is the right size and contains the rightnumbers). It should throw an IllegalArgumentExceptionotherwise. (More on exceptions later in this lab sheet...)

You may need a number of private helper methods, but there areonly two public methods.

This method should move the tile located in position(x,y) into the space. It must check that the requestis legitimate and throw an IllegalArgumentExceptionotherwise. In a legitimate request, (x,y) are thecoordinates of a tile that is adjacent to the space.

This is an accessor method that simply returns the current stateof the game as a 2D-array, using the same conventions as outlinedin the constructor.

When you detect an error that you want to report, you constructand throw an IllegalArgumentException object. It has aconstructor that accepts a string which can be the description ofthe error. Here is how you might throw an exception if you detectthat the grid does not have four rows:

Here are a few simple examples of how your class shouldbehave.

Suppose that the client creates a FifteenPuzzlewith the initial grid

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

Then the initial display should be

Notice how the array {0, 1, 2, 3} appears as the first column inthe grid because we are using the first index to indicate the xcoordinate and the second index to indicate y.

At this point, the only legitimate calls tomoveTile are moveTile(0,1) andmoveTile(1,0). If you try a few silly ones likemoveTile(5,23) or moveTile(0,-17) ormoveTile(1,1), your object should throwIllegalArgumentExceptions. If you finally domoveTile(1,0), the display should change to

Now if you do moveTile(1,1), the display shouldchange to

and if you follow that with moveTile(2,1), thedisplay should change to


You do not need to put in any special code to detect the goal stateand issue a congratulatory message, but of course you are free todo so.

grid[0][0] grid[1][0] grid[2][0] grid[3][0] grid[0][1] grid[1][1] grid[2][1] grid[3][1] grid[0][2] grid[1][2] grid[2][2] grid[3][2] grid[0][3] grid[1][3] grid[2][3] grid[3][3]

Explanation / Answer

import java.awt.Color;public class FlagDrawer{private int size;private SimpleCanvas sc;static Color dkBlue = new Color(0,0,204);static Color red = new Color(255,0,0);static Color white = new Color(255,255,255);static Color yellow = new Color(255,255,0);static Color black = new Color(0,0,0);public FlagDrawer(int size){this.size = size;sc = new SimpleCanvas("Flags", size, size);}private void drawLine(int x1, int y1, int x2, int y2, Color c){sc.setForegroundColour(c);sc.drawLine(x1,y1,x2,y2);}private void drawRectangle(int x1, int y1, int x2, int y2, Color c){sc.setForegroundColour(c);for (int i=y1; i
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote