Advertisement
Guest User

Con

a guest
Oct 23rd, 2019
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.73 KB | None | 0 0
  1. package sample;
  2.  
  3. import javafx.collections.ObservableList;
  4. import javafx.event.ActionEvent;
  5. import javafx.event.EventHandler;
  6. import javafx.scene.Node;
  7. import javafx.scene.input.MouseEvent;
  8. import javafx.scene.layout.*;
  9. import javafx.scene.control.*;
  10.  
  11.  
  12.  
  13. public class Controller {
  14.  
  15.     //Main panes
  16.     public BorderPane bpMainPane;
  17.     public GridPane gpCenterPane;
  18.  
  19.     //Top horizontal box pane
  20.     public HBox hbTop;
  21.  
  22.     //Buttons
  23.     public Button btnSetSize;
  24.     public Button btnControl;
  25.  
  26.     //Top objects
  27.     public Label lblTop;
  28.     public TextField tfSize;
  29.  
  30.     //Maze Size (NxN)
  31.     private int mazeSize;
  32.  
  33.     //Current player position
  34.     private int pos_x;
  35.     private int pos_y;
  36.  
  37.     //Step Counter
  38.     private int stepCount;
  39.  
  40.     //Controller for Search algorithm
  41.     private SearchController search;
  42.  
  43.     /**
  44.      * Button action for btnSetSize. Set maze size, create maze, then move top scene.
  45.      */
  46.     public void setButtonAction()
  47.     {
  48.         mazeSize = Integer.parseInt(tfSize.getText());
  49.         createMaze();
  50.         wallsMakeScene();
  51.     }
  52.  
  53.     /**
  54.      * Creates individual Cells then adds to the gridPane.
  55.      */
  56.     private void createMaze()
  57.     {
  58.         for(int i = 0; i < mazeSize; i++)
  59.             for(int z = 0; z < mazeSize; z++)
  60.             {
  61.                 Cell cell = new Cell(i , z, mazeSize);
  62.                 gpCenterPane.add(cell, i, z);
  63.             }
  64.  
  65.         //Initialize x, y, step counter
  66.         pos_x = 0;
  67.         pos_y = 0;
  68.         stepCount = 0;
  69.     }
  70.  
  71.     /**
  72.      * Set top objects to fit wall making scene
  73.      */
  74.     public void wallsMakeScene()
  75.     {
  76.         //Remove text field and set size button
  77.         hbTop.getChildren().remove(tfSize);
  78.         hbTop.getChildren().remove(btnSetSize);
  79.  
  80.         //Set new text and show new button
  81.         lblTop.setText("Choose Walls");
  82.         btnControl.setVisible(true);
  83.     }
  84.  
  85.     /**
  86.      * Gets Cell object from certain index at GridPane
  87.      * @param row x index of Cell object
  88.      * @param col y index of Cell object
  89.      * @return Cell object found with specified row col index
  90.      */
  91.     private Cell getCellAtIndex(int row, int col)
  92.     {
  93.         Node cell = null;
  94.         for(Node node : gpCenterPane.getChildren())
  95.             if(gpCenterPane.getRowIndex(node) == row && gpCenterPane.getColumnIndex(node) == col)
  96.             {
  97.                 cell = node;
  98.                 break;
  99.             }
  100.  
  101.         return (Cell)cell;
  102.     }
  103.  
  104.     /**
  105.      * Moves player from one Cell to another
  106.      */
  107.     public void move()
  108.     {
  109.         //Get next Cell of player
  110.         Cell destination = search.getNextMove();
  111.  
  112.         if(destination != null)
  113.             destination.addPlayer();
  114.  
  115.         pos_x = destination.getX();
  116.         pos_y = destination.getY();
  117.  
  118.         stepCount++;
  119.         lblTop.setText("Step Counter: " + stepCount);
  120.     }
  121.  
  122.     /**
  123.      * Disables mouse click events for each Cell in the grid.
  124.      * Then sets up text and button to start the simulation.
  125.      */
  126.     public void saveMaze()
  127.     {
  128.         //Retrieves Cells from grid.
  129.         ObservableList<Node> maze = gpCenterPane.getChildren();
  130.  
  131.         for(int i = 0; i < Math.pow(mazeSize, 2); i++)
  132.         {
  133.             Cell cell = (Cell)maze.get(i);
  134.             cell.removeMouseClick();
  135.         }
  136.  
  137.         lblTop.setText("Step Counter: " + stepCount);
  138.         btnControl.setText("Move");
  139.  
  140.         //Set button new action when clicked
  141.         btnControl.setOnAction(new EventHandler<ActionEvent>() {
  142.             @Override
  143.             public void handle(ActionEvent actionEvent) {
  144.                 move();
  145.             }
  146.         });
  147.  
  148.         search = new SearchController(gpCenterPane);
  149.  
  150.     }
  151.  
  152. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement