Advertisement
Guest User

SlidingJAVAFX

a guest
May 21st, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.66 KB | None | 0 0
  1. public class PuzzleFieldStyle {
  2.     private String style;
  3.  
  4.     public PuzzleFieldStyle(String style) {
  5.         super();
  6.         this.style = style;
  7.     }
  8.  
  9.     public String getStyle() {
  10.         return style;
  11.     }
  12.  
  13.     public void setStyle(String style) {
  14.         this.style = style;
  15.     }
  16.  
  17.     @Override
  18.     public String toString() {
  19.         return style;
  20.     }
  21. }
  22.  
  23.  
  24. public class Point {
  25.     private int x;
  26.     private int y;
  27.  
  28.     public Point(int x, int y) {
  29.         this.x = x;
  30.         this.y = y;
  31.     }
  32.  
  33.     public int getX() {
  34.         return x;
  35.     }
  36.  
  37.     public int getY() {
  38.         return y;
  39.     }
  40.  
  41.     @Override
  42.     public String toString() {
  43.         return "x:"+x+" y:"+y;
  44.     }
  45. }
  46.  
  47.  
  48. import java.util.Random;
  49.  
  50. public class PuzzleModel {
  51.     private PuzzleFieldStyle[][] board;
  52.     private PuzzleFieldStyle[][] expectedBoard;
  53.     private int boardSize;
  54.  
  55.     // Indices of the neighbor fields
  56.     private int[] indexX = { 0, -1, 1, 0 };
  57.     private int[] indexY = { 1, 0, 0, -1 };
  58.  
  59.     public PuzzleModel(int size) {
  60.         board = new PuzzleFieldStyle[size][size];
  61.         expectedBoard = new PuzzleFieldStyle[size][size];
  62.  
  63.         // Simplify the range checks
  64.         boardSize = board.length;
  65.  
  66.         init();
  67.     }
  68.  
  69.     private void init() {
  70.         int colorCounter = 1;
  71.         for (int x = 0; x < boardSize; x++) {
  72.             for (int y = 0; y < boardSize; y++) {
  73.                 PuzzleFieldStyle color = new PuzzleFieldStyle("puzzle-field-style-no"+colorCounter++);
  74.                 expectedBoard[x][y] = color;
  75.                 board[x][y] = color;
  76.             }
  77.         }
  78.  
  79.         // One field is empty
  80.         expectedBoard[boardSize-1][boardSize-1] = null;
  81.         board[boardSize-1][boardSize-1] = null;
  82.  
  83.         shuffleBoard();
  84.     }
  85.  
  86.     public Point moveToEmptyField(Point moveColoredFieldToPoint) {
  87.         for (int i = 0; i < 4; i++) {
  88.             if (moveColoredFieldToPoint.getX() + indexX[i] >= 0 && moveColoredFieldToPoint.getY() + indexY[i] >= 0 && moveColoredFieldToPoint.getX() + indexX[i] < boardSize && moveColoredFieldToPoint.getY() + indexY[i] < boardSize) {
  89.                 //Check if the field is empty (null)
  90.                 if (board[moveColoredFieldToPoint.getX() + indexX[i]][moveColoredFieldToPoint.getY() + indexY[i]] == null) {
  91.  
  92.                     Point emptyFieldPos = new Point(moveColoredFieldToPoint.getX() + indexX[i], moveColoredFieldToPoint.getY() + indexY[i]);
  93.                     switchField(moveColoredFieldToPoint, emptyFieldPos);
  94.                     return emptyFieldPos;
  95.                 }
  96.             }
  97.         }
  98.         return null;
  99.     }
  100.  
  101.     /**
  102.      * Compare the colors of the rectangles.
  103.      */
  104.     public boolean areBoardsEqual() {
  105.         for (int x = 0; x < board.length; x++) {
  106.             for (int y = 0; y < board.length; y++) {
  107.                 PuzzleFieldStyle expectedRec = expectedBoard[x][y];
  108.                 PuzzleFieldStyle rect = board[x][y];
  109.                 if (rect!=expectedRec || !(expectedRec == rect || expectedRec.toString().equals(rect.toString()))) {
  110.                     return false;
  111.                 }
  112.             }
  113.         }
  114.  
  115.         return true;
  116.     }
  117.  
  118.     private void shuffleBoard() {
  119.         // Per definition this is the empty field in the initial state
  120.         // see colorSet4: Last value is null
  121.         Point emptyFieldPos = new Point(boardSize - 1, boardSize - 1);
  122.  
  123.         Random r = new Random(System.currentTimeMillis());
  124.  
  125.         for (int i = 0; i < 1000; i++) {
  126.             int fieldToMove = r.nextInt(indexX.length);
  127.             if (emptyFieldPos.getX() + indexX[fieldToMove] >= 0 && emptyFieldPos.getY() + indexY[fieldToMove] >= 0 && emptyFieldPos.getX() + indexX[fieldToMove] < boardSize && emptyFieldPos.getY() + indexY[fieldToMove] < boardSize) {
  128.                 Point colorFieldPos = new Point(emptyFieldPos.getX() + indexX[fieldToMove], emptyFieldPos.getY() + indexY[fieldToMove]);
  129.                 emptyFieldPos = switchField(colorFieldPos, emptyFieldPos);
  130.             }
  131.         }
  132.     }
  133.  
  134.     private Point switchField(Point colorFieldPos, Point emptyFieldPos) {
  135.         // Switch with one temp variable was possible, too. But this is
  136.         // better to understand.
  137.         PuzzleFieldStyle coloredField = board[colorFieldPos.getX()][colorFieldPos.getY()];
  138.         PuzzleFieldStyle emptyWhiteField = board[emptyFieldPos.getX()][emptyFieldPos.getY()];
  139.         board[emptyFieldPos.getX()][emptyFieldPos.getY()] = coloredField;
  140.         board[colorFieldPos.getX()][colorFieldPos.getY()] = emptyWhiteField;
  141.         return new Point(colorFieldPos.getX(), colorFieldPos.getY());
  142.     }
  143.  
  144.     public PuzzleFieldStyle getColorAt(int x, int y) {
  145.         if (x < boardSize && x >= 0 && y < boardSize && y >= 0) {
  146.             return board[x][y];
  147.         }
  148.         return null;
  149.     }
  150.  
  151.     public PuzzleFieldStyle getExpectedColorAt(int x, int y) {
  152.         if (x < boardSize && x >= 0 && y < boardSize && y >= 0) {
  153.             return expectedBoard[x][y];
  154.         }
  155.         return null;
  156.     }
  157.  
  158.     public void printBoards() {
  159.         System.out.println("======================================================= Board");
  160.         for (int x = 0; x < boardSize; x++) {
  161.             for (int y = 0; y < boardSize; y++) {
  162.                 if (board[y][x] != null) {
  163.                     System.out.print("\t" + board[y][x].toString() + "\t");
  164.                 } else {
  165.                     System.out.print("\t\t\t\t");
  166.                 }
  167.             }
  168.             System.out.println();
  169.         }
  170.  
  171.         System.out.println("======================================================= Expected");
  172.  
  173.         for (int x = 0; x < boardSize; x++) {
  174.             for (int y = 0; y < boardSize; y++) {
  175.                 if (expectedBoard[y][x] != null) {
  176.                     System.out.print("\t" + expectedBoard[y][x].toString() + "\t");
  177.                 } else {
  178.                     System.out.print("\t\t\t");
  179.                 }
  180.             }
  181.             System.out.println();
  182.         }
  183.     }
  184. }
  185.  
  186. public class PuzzleApplication extends Application {
  187.  
  188.     private static final int SIZE = 4;
  189.     private static final int FIELD_SIZE_PIXELS = 50;
  190.  
  191.     private final StringProperty counter = new SimpleStringProperty("0");
  192.     private final Text youWinText = TextBuilder.create().text("Y o u   w i n!        ").visible(false).styleClass("text-big-puzzle").build();
  193.  
  194.     @Override
  195.     public void start(Stage primaryStage) {
  196.         final Label counterLabel = LabelBuilder.create().text(String.valueOf(counter.get())).styleClass("text-puzzle").layoutX(120).layoutY(20).build();
  197.         counterLabel.textProperty().bind(counter);
  198.  
  199.         BorderPane borderPane = new BorderPane();
  200.  
  201.         Pane headerPane = new Pane();
  202.         HBox hbox = new HBox();
  203.         hbox.setPadding(new Insets(15, 12, 15, 12));
  204.         hbox.setSpacing(10);
  205.         hbox.getChildren().add(TextBuilder.create().text("Counter:").styleClass("text-puzzle").x(50).y(20).build());
  206.         hbox.getChildren().add(counterLabel);
  207.         headerPane.getChildren().add(hbox);
  208.  
  209.         VBox vBoxLeft = new VBox();
  210.         vBoxLeft.setPadding(new Insets(15, 20, 15, 20));
  211.         vBoxLeft.setSpacing(10);
  212.         VBox vBoxRight = new VBox();
  213.         vBoxRight.setPadding(new Insets(15, 20, 15, 20));
  214.         vBoxRight.setSpacing(10);
  215.  
  216.         final Pane gamePane = new Pane();
  217.         init(gamePane, new PuzzleModel(SIZE));
  218.  
  219.         AnchorPane anchorpane = new AnchorPane();
  220.         Button buttonReset = ButtonBuilder.create()
  221.                 .text("Reset")
  222.                 .styleClass("puzzle-reset-button")
  223.                 .onAction(new EventHandler<ActionEvent>() {
  224.  
  225.                     @Override
  226.                     public void handle(ActionEvent event) {
  227.                         gamePane.getChildren().clear();
  228.                         counter.set("0");
  229.                         init(gamePane, new PuzzleModel(SIZE));
  230.                         youWinText.setVisible(false);
  231.                     }
  232.                 })
  233.                 .build();
  234.  
  235.         HBox buttonBox = new HBox();
  236.         buttonBox.setPadding(new Insets(0, 10, 10, 10));
  237.         buttonBox.setSpacing(10);
  238.         buttonBox.getChildren().add(youWinText);
  239.         buttonBox.getChildren().add(buttonReset);
  240.  
  241.         AnchorPane.setBottomAnchor(buttonBox, 8.0);
  242.         AnchorPane.setRightAnchor(buttonBox, 5.0);
  243.         anchorpane.getChildren().add(buttonBox);
  244.  
  245.         borderPane.setTop(headerPane);
  246.         borderPane.setCenter(gamePane);
  247.         borderPane.setLeft(vBoxLeft);
  248.         borderPane.setRight(vBoxRight);
  249.         borderPane.setBottom(anchorpane);
  250.  
  251.         Scene scene = new Scene(borderPane, 400*1.4, 260*1.4);
  252.  
  253.         primaryStage.setTitle("JavaFX - Puzzle");
  254.         primaryStage.setScene(scene);
  255.         primaryStage.getScene().getStylesheets().add("puzzle");
  256.         primaryStage.show();
  257.     }
  258.  
  259.     public void init(Pane pane, final PuzzleModel model) {
  260.         for (int x = 0; x < SIZE; x++) {
  261.             for (int y = 0; y < SIZE; y++) {
  262.                 PuzzleFieldStyle expectedColor = model.getExpectedColorAt(x, y);
  263.                 if (expectedColor != null) {
  264.                     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();
  265.                     pane.getChildren().add(recExpected);
  266.                 }
  267.  
  268.                 PuzzleFieldStyle color = model.getColorAt(x, y);
  269.                 if (color != null) {
  270.                     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();
  271.                     pane.getChildren().add(rec);
  272.  
  273.                     rec.setOnMouseClicked(new EventHandler<MouseEvent>() {
  274.                         @Override
  275.                         public void handle(MouseEvent event) {
  276.                             Point moveFromPoint = new Point((int) rec.getX() / FIELD_SIZE_PIXELS, (int) rec.getY() / FIELD_SIZE_PIXELS);
  277.                             Point moveToPoint = model.moveToEmptyField(moveFromPoint);
  278.                             if (moveToPoint != null) {
  279.                                 // Increase the counter
  280.                                 counter.set(String.valueOf(Integer.parseInt(counter.get())+1));
  281.                                 moveRectangle(moveToPoint, rec);
  282.                                 model.printBoards();
  283.                                 if (model.areBoardsEqual()) {
  284.                                     youWinText.setVisible(true);
  285.                                 }
  286.                             }
  287.                         }
  288.                     });
  289.                 }
  290.             }
  291.         }
  292.     }
  293.  
  294.     private void moveRectangle(final Point moveToPoint, final Rectangle rec) {
  295.         rec.setX(moveToPoint.getX() * FIELD_SIZE_PIXELS);
  296.         rec.setY(moveToPoint.getY() * FIELD_SIZE_PIXELS);
  297.     }
  298.  
  299.     public static void main(String[] args) {
  300.         launch(args);
  301.     }
  302. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement