Question: Part 1 Create a JavaFX GUI that represents the streets of Pamplona. You do not need to consult an actual map of Pamplona, although you
Part 1
Create a JavaFX GUI that represents the streets of Pamplona. You do not need to consult an actual map of Pamplona, although you may if you want to.
Do the styling (coding of the details of the appearance of the GUI, such as colors and font sizes) using CSS, not the JavaFX setters, wherever possible. Make the start and finish squares appear different from other squares.
Grading for this lab will heavily weight the appearance of the GUI as well as its functionality. To get a good grade on this assignment, your GUI must both work correctly and look good.
Here are some of the classes you will need. This is not a complete list, and each class will need more than the basics I specify below:
A Coordinate class with an int row, an int column, and a char representing the value of the coordinate (this week, you will use four values for the Coordinates: blank (' ') for an empty space, W for a wall, S for the starting point, and E for the exit.) Provide getters and setters.
A StreetMap class with a two-dimensional array of Coordinates but no GUI code.
A JavaFX GUI class called MazeGUIPane. Since the constructor for Scene takes a Pane as a parameter, this class should extend one of the Pane classes, most likely BorderPane. It should also contain a GridPane with a two-dimensional grid of Labels. One label in the GridPane corresponds to one Coordinate in the StreetMap. Note this important distinction: the MazeGUIPane is for the the user interface while the StreetMap is for data.
The outer edges of the grid should consist of walls, except for one starting square and one exit square. When the game starts, squares that are not on the edges should be randomly set to wall and space squares (use about 20% walls.) Note that this does not guarantee that it is possible to escape the bulls at all; that is why you need to be able to toggle between walls and spaces.
Clicking on a label that is not on the edge of the board toggles the label between wall and empty space. The event handling code must update the StreetMap when the squares toggle. It must also change the css class of the Label in order to change its appearance.
You will also need a game reset button. You do not need to make the button work yet. In my solution, this button is in an HBox contained in the MazeGUIPane.
You may use this GUI as a model if you like, but feel free to create a different one:
Part 2
In our version of the Running of the Bulls, one runner runs through Pamplona trying to avoid some number of bulls. If he makes it to the exit square without being gored, he wins; if a bull catches him first, he loses. When one of these events occurs, a message should appear next to the Run button.
The runner's movement must be controlled by the four scroll keys (use an event handler that handles a KeyEvent and find the key strokes using the the method getKeyCode(), which returns a KeyCode enum). Make sure he cannot move through walls. Use a Coordinate variable to track the runner's location and use the event handler to change the location. Both the GUI and the map class need to track this location. I did not use a Runner class, but you may use one if you like.
You will need a Bull class. Among other things, it will contain a method to determine the bull's next step. If he can see the runner, he moves towards him. If the bull cannot see the runner, he moves toward the runner's last known location if possible, otherwise he moves randomly. Like the runner, the bulls cannot move through walls. Both the GUI and the map class need to know each location.
Here are some key lines of code for the event handler. These are hints; they will not be consecutive lines of code in your application.
final EventHandler keyEventHandler = new EventHandler() { public void handle(final KeyEvent keyEvent) { if (keyEvent.getCode() == KeyCode.DOWN) As the Bulls and the runner move, change the css classes for the labels they move to as well as the labels they move from. Don't try to add new labels to the grid as they move.
The bulls should move faster than the runner. The best way to implement this is for the bulls to take more than one move for each runner move. You can code this by writing a method in StreetMap that calls each bull's move method, and calling the StreetMap method some number of times from inside the runner move event handler (the one that responds to the scroll keys.) Since the bulls move faster, the runner should get a few free moves before the Bulls begin moving.
Use css to make it obvious at all times where the bulls and the runner are.
Create an event handler for the run button which returns both the runner and the bulls to the starting point. You may also have the button also create a new map.
Experiment with the game to make it difficult, but still possible, for the runner to escape unharmed. Design Main so that you can set parameters there for the number of turns in the head start, the number of bulls, and the number of bull moves per runner move. If you want more practice with GUI building, provide a way to set these parameters with user input.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
