Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package sample;
- import javafx.collections.ObservableList;
- import javafx.event.ActionEvent;
- import javafx.event.EventHandler;
- import javafx.scene.Node;
- import javafx.scene.input.MouseEvent;
- import javafx.scene.layout.*;
- import javafx.scene.control.*;
- public class Controller {
- //Main panes
- public BorderPane bpMainPane;
- public GridPane gpCenterPane;
- //Top horizontal box pane
- public HBox hbTop;
- //Buttons
- public Button btnSetSize;
- public Button btnControl;
- //Top objects
- public Label lblTop;
- public TextField tfSize;
- //Maze Size (NxN)
- private int mazeSize;
- //Current player position
- private int pos_x;
- private int pos_y;
- //Step Counter
- private int stepCount;
- //Controller for Search algorithm
- private SearchController search;
- /**
- * Button action for btnSetSize. Set maze size, create maze, then move top scene.
- */
- public void setButtonAction()
- {
- mazeSize = Integer.parseInt(tfSize.getText());
- createMaze();
- wallsMakeScene();
- }
- /**
- * Creates individual Cells then adds to the gridPane.
- */
- private void createMaze()
- {
- for(int i = 0; i < mazeSize; i++)
- for(int z = 0; z < mazeSize; z++)
- {
- Cell cell = new Cell(i , z, mazeSize);
- gpCenterPane.add(cell, i, z);
- }
- //Initialize x, y, step counter
- pos_x = 0;
- pos_y = 0;
- stepCount = 0;
- }
- /**
- * Set top objects to fit wall making scene
- */
- public void wallsMakeScene()
- {
- //Remove text field and set size button
- hbTop.getChildren().remove(tfSize);
- hbTop.getChildren().remove(btnSetSize);
- //Set new text and show new button
- lblTop.setText("Choose Walls");
- btnControl.setVisible(true);
- }
- /**
- * Gets Cell object from certain index at GridPane
- * @param row x index of Cell object
- * @param col y index of Cell object
- * @return Cell object found with specified row col index
- */
- private Cell getCellAtIndex(int row, int col)
- {
- Node cell = null;
- for(Node node : gpCenterPane.getChildren())
- if(gpCenterPane.getRowIndex(node) == row && gpCenterPane.getColumnIndex(node) == col)
- {
- cell = node;
- break;
- }
- return (Cell)cell;
- }
- /**
- * Moves player from one Cell to another
- */
- public void move()
- {
- //Get next Cell of player
- Cell destination = search.getNextMove();
- if(destination != null)
- destination.addPlayer();
- pos_x = destination.getX();
- pos_y = destination.getY();
- stepCount++;
- lblTop.setText("Step Counter: " + stepCount);
- }
- /**
- * Disables mouse click events for each Cell in the grid.
- * Then sets up text and button to start the simulation.
- */
- public void saveMaze()
- {
- //Retrieves Cells from grid.
- ObservableList<Node> maze = gpCenterPane.getChildren();
- for(int i = 0; i < Math.pow(mazeSize, 2); i++)
- {
- Cell cell = (Cell)maze.get(i);
- cell.removeMouseClick();
- }
- lblTop.setText("Step Counter: " + stepCount);
- btnControl.setText("Move");
- //Set button new action when clicked
- btnControl.setOnAction(new EventHandler<ActionEvent>() {
- @Override
- public void handle(ActionEvent actionEvent) {
- move();
- }
- });
- search = new SearchController(gpCenterPane);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement