Question: In java please. please show the code and the ouput. All classes and subclasses required are given below the question. We will be creating a
In java please. please show the code and the ouput. All classes and subclasses required are given below the question.
We will be creating a slider-puzzle game where there are both horizontal-sliding game pieces and verticalsliding game pieces. The objective is to get the red horizontal-sliding goal piece out of the puzzle through the exit on the right side of the board. The user can grab and drag the game pieces to try and create a path for the red goal piece to reach the exit. Here, on the right, is an image of a game in progress. In this example, there are 2 horizontal-sliding game pieces (light pink & purple), a horizontal-sliding goal piece (red), and 5 vertical-sliding game pieces. The gray border represents non-movable "walls" in the game. Each time a game piece is grabbed and dragged, it counts as one "move". The idea is to get the goal piece to the exit in the least number of "moves" possible. Once the goal piece is slid over to the exit, then a new board arrangement is brought up and the game continues. (1) The Model classes To begin, we will develop the model classes as shown in the hierarchy here. The game itself is defined in a class called SliderPuzzleGame. This class will contain a GameBoard. The GameBoard will be composed of GamePiece objects, where each piece must be an instance of VerticalGamePiece, HorizontalGamePiece or GoalPiece. Each GamePiece has a width, a height and a color. As well, we will keep track of the piece's current location in the GameBoard by storing the grid location of the top left corner of the GamePiece. On the next page, it shows the topLeftX and topLeftY values for each piece as well as an explanation of the width and the height for a piece. The code for the class is given on the next page. Notice that moving a game piece simple causes the topLeftX or topLeftY value to be altered. And, by default, GamePieces cannot be moved. The subclasses will override these methods. Here is the code for the subclasses of GamePiece: Some of the code is missing, we will get to that in a moment. The GameBoard class is as shown below. Note that the WIDTH and HEIGHT of the GameBoard is 6, making for a 6x6 board as in our images shown earlier. The GameBoard keeps a list of GamePieces as a one dimensional array. The GameBoard also keeps track of whether or not it has been completed. Notice that there are some sample GameBoards that can be obtained from calls to board1(), board2(), etc.. Finally, here is the SliderPuzzleGame class that holds it all together: Notice that these classes are missing some code, which you must complete. First, try to understand the code that you have been given and then complete the blank methods in the following order: HorizontalGamePiece class: o canMoveLeftIn(GameBoard b) and canMoveRightIn(GameBoard b) These methods should return true if the game piece is able to be moved left or right, respectively, within the given GameBoard. A piece can move left or right if it is not obstructed by another piece or a wall. You will need to consider the topLeft position of the game piece in each case, as well as the width of the piece. VerticalGamePiece class: o canMoveDownIn(GameBoard b) and canMoveUpIn(GameBoard b) These methods should return true if the game piece is able to be moved down or up, respectively, within the given GameBoard. A piece can move down or up if it is not obstructed by another piece or a wall. You will need to consider the topLeft position of the game piece in each case, as well as the height of the piece. GoalPiece class: o canMoveRightIn(GameBoard b) This method should return true if the goal piece is able to be moved right within the given GameBoard. The method is similar to the one in the HorizontalGamePiece class, except that it allows you to move beyond the rightmost wall opening. GameBoard class: o pieceAt(int x, int y) This method should return the GamePiece that is at the given position in the board. For example, from our image above, if (5,3), (5,4) or (5,5) is passed in as parameters, then the blue game piece should be returned. If (1,1) is passed in, then null should be returned. o checkCompletion(GamePiece gp) This method should set the completed attribute to true if parameter gp is a GoalPiece and if it's topLeft is at position (5,2), indicating that the GoalPiece has been slid into the opening.
SliderPuzzleApp
import javafx.application.Application; import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.scene.Scene; import javafx.scene.input.MouseEvent; import javafx.stage.Stage; public class SliderPuzzleApp extends Application { private SliderPuzzleGame model; private SliderPuzzleView view; private GamePiece selectedPiece; private boolean justGrabbed; private int lastX; private int lastY; public void start(Stage primaryStage) { model = new SliderPuzzleGame(); view = new SliderPuzzleView(model); // Add event handlers to the inner game board buttons for (int w=1; w<=(GameBoard.WIDTH); w++) { for (int h=1; h<=(GameBoard.HEIGHT); h++) { view.getGridSection(w, h).setOnMousePressed(new EventHandler
import javafx.application.Application; import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.scene.Scene; import javafx.scene.input.MouseEvent; import javafx.stage.Stage; public class SliderPuzzleApp extends Application { private SliderPuzzleGame model; private SliderPuzzleView view; private GamePiece selectedPiece; private boolean justGrabbed; private int lastX; private int lastY; public void start(Stage primaryStage) { model = new SliderPuzzleGame(); view = new SliderPuzzleView(model); // Add event handlers to the inner game board buttons for (int w=1; w<=(GameBoard.WIDTH); w++) { for (int h=1; h<=(GameBoard.HEIGHT); h++) { view.getGridSection(w, h).setOnMousePressed(new EventHandler
import javafx.scene.paint.Color; public abstract class GamePiece { protected int width; protected int height; protected Color color; protected int topLeftX; protected int topLeftY; public GamePiece(int w, int h, Color c, int x, int y) { width = w; height = h; color = c; topLeftX = x; topLeftY = y; } public int getWidth() { return width; } public int getHeight() { return height; } public Color getColor() { return color; } public int getTopLeftX() { return topLeftX; } public int getTopLeftY() { return topLeftY; } public void moveRight() { topLeftX += 1;} public void moveLeft() { topLeftX -= 1;} public void moveDown() { topLeftY += 1;} public void moveUp() { topLeftY -= 1;} public boolean canMoveLeftIn(GameBoard b) { return false; } public boolean canMoveRightIn(GameBoard b) { return false; } public boolean canMoveDownIn(GameBoard b) { return false; } public boolean canMoveUpIn(GameBoard b) { return false; } } HorizontalGamePiece
import javafx.scene.paint.Color; public class GoalPiece extends HorizontalGamePiece { public GoalPiece(int x, int y) { super(2, Color.RED, x, y); } public boolean canMoveRightIn(GameBoard b) { // REPLACE THE CODE BELOW WITH YOUR OWN CODE return false; } } Vertical GamePiece
import javafx.scene.paint.Color; public class VerticalGamePiece extends GamePiece { public VerticalGamePiece(int h, Color c, int x, int y) { super(1, h, c, x, y); } public boolean canMoveDownIn(GameBoard b) { // REPLACE THE CODE BELOW WITH YOUR OWN CODE return false; } public boolean canMoveUpIn(GameBoard b) { // REPLACE THE CODE BELOW WITH YOUR OWN CODE return false; } } GoalPiece
import javafx.scene.paint.Color; public class GoalPiece extends HorizontalGamePiece { public GoalPiece(int x, int y) { super(2, Color.RED, x, y); } public boolean canMoveRightIn(GameBoard b) { // REPLACE THE CODE BELOW WITH YOUR OWN CODE return false; } } Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
