Please answer using javafx package, thank you (1) Slider Puzzle Game You will be
ID: 3727535 • Letter: P
Question
Please answer using javafx package, thank you
(1) Slider Puzzle Game You will be creating the following application: Sider Punit Game cenery Stop Time 020 exchanged horizontaly This application represents a slider puzzle game. There are 15 tiles that can be exchanged horizontally or vertically with a blank tile in order to shuffle the image pieces around. When the tiles are arranged in the correct order, they form a complete image and the game is done. The game is timed.. so the faster you can complete the puzzle, the better your score yu cano ordei hey oee ne the image pieces areExplanation / Answer
package org.hameister.javafx.puzzle;
public class PuzzleFieldStyle {
private String style;
public PuzzleFieldStyle(String style) {
super();
this.style = style;
}
public String getStyle() {
return style;
}
public void setStyle(String style) {
this.style = style;
}
@Override
public String toString() {
return style;
}
}
Second I created a class to store position of the puzzle tiles: (Yes, there are frameworks and classes which do the same, but it does not hurt to implement such a small lightweight class without any overhead in it. :-) )
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package org.hameister.javafx.puzzle;
public class Point {
private int x;
private int y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
@Override
public String toString() {
return "x:"+x+" y:"+y;
}
}
package org.hameister.javafx.puzzle;
import java.util.Random;
public class PuzzleModel {
private PuzzleFieldStyle[][] board;
private PuzzleFieldStyle[][] expectedBoard;
private int boardSize;
// Indices of the neighbor fields
private int[] indexX = { 0, -1, 1, 0 };
private int[] indexY = { 1, 0, 0, -1 };
public PuzzleModel(int size) {
board = new PuzzleFieldStyle[size][size];
expectedBoard = new PuzzleFieldStyle[size][size];
boardSize = board.length;
init();
}
private void init() {
int colorCounter = 1;
for (int x = 0; x < boardSize; x++) {
for (int y = 0; y < boardSize; y++) {
PuzzleFieldStyle color = new PuzzleFieldStyle("puzzle-field-style-no"+colorCounter++);
expectedBoard[x][y] = color;
board[x][y] = color;
}
}
// One field is empty
expectedBoard[boardSize-1][boardSize-1] = null;
board[boardSize-1][boardSize-1] = null;
shuffleBoard();
}
public Point moveToEmptyField(Point moveColoredFieldToPoint) {
for (int i = 0; i < 4; i++) {
if (moveColoredFieldToPoint.getX() + indexX[i] >= 0 && moveColoredFieldToPoint.getY() + indexY[i] >= 0 && moveColoredFieldToPoint.getX() + indexX[i] < boardSize && moveColoredFieldToPoint.getY() + indexY[i] < boardSize) {
if (board[moveColoredFieldToPoint.getX() + indexX[i]][moveColoredFieldToPoint.getY() + indexY[i]] == null) {
Point emptyFieldPos = new Point(moveColoredFieldToPoint.getX() + indexX[i], moveColoredFieldToPoint.getY() + indexY[i]);
switchField(moveColoredFieldToPoint, emptyFieldPos);
return emptyFieldPos;
}
}
}
return null;
}
public boolean areBoardsEqual() {
for (int x = 0; x < board.length; x++) {
for (int y = 0; y < board.length; y++) {
PuzzleFieldStyle expectedRec = expectedBoard[x][y];
PuzzleFieldStyle rect = board[x][y];
if (rect!=expectedRec || !(expectedRec == rect || expectedRec.toString().equals(rect.toString()))) {
return false;
}
}
}
return true;
}
private void shuffleBoard() {
Point emptyFieldPos = new Point(boardSize - 1, boardSize - 1);
Random r = new Random(System.currentTimeMillis());
for (int i = 0; i < 1000; i++) {
int fieldToMove = r.nextInt(indexX.length);
if (emptyFieldPos.getX() + indexX[fieldToMove] >= 0 && emptyFieldPos.getY() + indexY[fieldToMove] >= 0 && emptyFieldPos.getX() + indexX[fieldToMove] < boardSize && emptyFieldPos.getY() + indexY[fieldToMove] < boardSize) {
Point colorFieldPos = new Point(emptyFieldPos.getX() + indexX[fieldToMove], emptyFieldPos.getY() + indexY[fieldToMove]);
emptyFieldPos = switchField(colorFieldPos, emptyFieldPos);
}
}
}
private Point switchField(Point colorFieldPos, Point emptyFieldPos) {
PuzzleFieldStyle coloredField = board[colorFieldPos.getX()][colorFieldPos.getY()];
PuzzleFieldStyle emptyWhiteField = board[emptyFieldPos.getX()][emptyFieldPos.getY()];
board[emptyFieldPos.getX()][emptyFieldPos.getY()] = coloredField;
board[colorFieldPos.getX()][colorFieldPos.getY()] = emptyWhiteField;
return new Point(colorFieldPos.getX(), colorFieldPos.getY());
}
public PuzzleFieldStyle getColorAt(int x, int y) {
if (x < boardSize && x >= 0 && y < boardSize && y >= 0) {
return board[x][y];
}
return null;
}
public PuzzleFieldStyle getExpectedColorAt(int x, int y) {
if (x < boardSize && x >= 0 && y < boardSize && y >= 0) {
return expectedBoard[x][y];
}
return null;
}
public void printBoards() {
System.out.println("======================================================= Board");
for (int x = 0; x < boardSize; x++) {
for (int y = 0; y < boardSize; y++) {
if (board[y][x] != null) {
System.out.print(" " + board[y][x].toString() + " ");
} else {
System.out.print(" ");
}
}
System.out.println();
}
System.out.println("======================================================= Expected");
for (int x = 0; x < boardSize; x++) {
for (int y = 0; y < boardSize; y++) {
if (expectedBoard[y][x] != null) {
System.out.print(" " + expectedBoard[y][x].toString() + " ");
} else {
System.out.print(" ");
}
}
System.out.println();
}
}
}
The last class contains the JavaFX view components:
package org.hameister.javafx.puzzle;
public class PuzzleApplication extends Application {
private static final int SIZE = 4;
private static final int FIELD_SIZE_PIXELS = 50;
private final StringProperty counter = new SimpleStringProperty("0");
private final Text youWinText = TextBuilder.create().text("Y o u w i n! ").visible(false).styleClass("text-big-puzzle").build();
@Override
public void start(Stage primaryStage) {
final Label counterLabel = LabelBuilder.create().text(String.valueOf(counter.get())).styleClass("text-puzzle").layoutX(120).layoutY(20).build();
counterLabel.textProperty().bind(counter);
BorderPane borderPane = new BorderPane();
Pane headerPane = new Pane();
HBox hbox = new HBox();
hbox.setPadding(new Insets(15, 12, 15, 12));
hbox.setSpacing(10);
hbox.getChildren().add(TextBuilder.create().text("Counter:").styleClass("text-puzzle").x(50).y(20).build());
hbox.getChildren().add(counterLabel);
headerPane.getChildren().add(hbox);
VBox vBoxLeft = new VBox();
vBoxLeft.setPadding(new Insets(15, 20, 15, 20));
vBoxLeft.setSpacing(10);
VBox vBoxRight = new VBox();
vBoxRight.setPadding(new Insets(15, 20, 15, 20));
vBoxRight.setSpacing(10);
final Pane gamePane = new Pane();
init(gamePane, new PuzzleModel(SIZE));
AnchorPane anchorpane = new AnchorPane();
Button buttonReset = ButtonBuilder.create()
.text("Reset")
.styleClass("puzzle-reset-button")
.onAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
gamePane.getChildren().clear();
counter.set("0");
init(gamePane, new PuzzleModel(SIZE));
youWinText.setVisible(false);
}
})
.build();
HBox buttonBox = new HBox();
buttonBox.setPadding(new Insets(0, 10, 10, 10));
buttonBox.setSpacing(10);
buttonBox.getChildren().add(youWinText);
buttonBox.getChildren().add(buttonReset);
AnchorPane.setBottomAnchor(buttonBox, 8.0);
AnchorPane.setRightAnchor(buttonBox, 5.0);
anchorpane.getChildren().add(buttonBox);
borderPane.setTop(headerPane);
borderPane.setCenter(gamePane);
borderPane.setLeft(vBoxLeft);
borderPane.setRight(vBoxRight);
borderPane.setBottom(anchorpane);
Scene scene = new Scene(borderPane, 400*1.4, 260*1.4);
primaryStage.setTitle("JavaFX - Puzzle");
primaryStage.setScene(scene);
primaryStage.getScene().getStylesheets().add("puzzle");
primaryStage.show();
}
public void init(Pane pane, final PuzzleModel model) {
for (int x = 0; x < SIZE; x++) {
for (int y = 0; y < SIZE; y++) {
PuzzleFieldStyle expectedColor = model.getExpectedColorAt(x, y);
if (expectedColor != null) {
final Rectangle recExpected = RectangleBuilder.create().x(FIELD_SIZE_PIXELS * x + 280).y(FIELD_SIZE_PIXELS * y).width(FIELD_SIZE_PIXELS).height(FIELD_SIZE_PIXELS).styleClass(expectedColor.getStyle()).build();
pane.getChildren().add(recExpected);
}
PuzzleFieldStyle color = model.getColorAt(x, y);
if (color != null) {
final Rectangle rec = RectangleBuilder.create().x(FIELD_SIZE_PIXELS * x).y(FIELD_SIZE_PIXELS * y).width(FIELD_SIZE_PIXELS).height(FIELD_SIZE_PIXELS).styleClass(color.getStyle()).build();
pane.getChildren().add(rec);
rec.setOnMouseClicked(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent event) {
Point moveFromPoint = new Point((int) rec.getX() / FIELD_SIZE_PIXELS, (int) rec.getY() / FIELD_SIZE_PIXELS);
Point moveToPoint = model.moveToEmptyField(moveFromPoint);
if (moveToPoint != null) {
// Increase the counter
counter.set(String.valueOf(Integer.parseInt(counter.get())+1));
moveRectangle(moveToPoint, rec);
model.printBoards();
if (model.areBoardsEqual()) {
youWinText.setVisible(true);
}
}
}
});
}
}
}
}
private void moveRectangle(final Point moveToPoint, final Rectangle rec) {
rec.setX(moveToPoint.getX() * FIELD_SIZE_PIXELS);
rec.setY(moveToPoint.getY() * FIELD_SIZE_PIXELS);
}
public static void main(String[] args) {
launch(args);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package org.hameister.javafx.puzzle;
public class Point {
private int x;
private int y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
@Override
public String toString() {
return "x:"+x+" y:"+y;
}
}