Part 1 Create the GUI Pamplona Spain is one of many towns in
Part 1: Create the GUI
Pamplona, Spain, is one of many towns in Spain and elsewhere that hold an annual event called \"The Running of the Bulls.\" In this event, people run through narrow city streets while attempting, often successfully, to avoid being injured by angry bulls. In this assignment, you will construct a game that simulates this event.
This week, you will 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; if you are troubled by this, see the note below. Also, you may want to make sure that the few squares nearest the start square are all empty space rather than assigning them random values.
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 start button. In my solution, this button is in an HBox contained in the MazeGUIPane.
Part 2: Make The Game Work
In our version of the Running of the Bulls, one fool runs through Pamplona trying to avoid some number of bulls. If the fool 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 fool\'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 KeyCode enum). Make sure he cannot move through walls. Use a Coordinate variable to track the fool\'s location. I did not use a Fool 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 a bull can see the fool, he moves towards him. If a bull cannot see the fool, he moves toward the fool\'s last known location if possible, otherwise he moves randomly. Like the fool, the bulls cannot move through walls.
The bulls should move faster than the fool. The best way to implement this is for the bulls to take more than one move for each fool 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 fool move event handler (the one that responds to the scroll keys.) Since the bulls move faster, the fool 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 fool are.
Create an event handler for the start button which returns both the fool 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 fool 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 fool move. If you want more practice with GUI building, provide a way to set these parameters with user input.
Turn in your .java source files and also a runnable .jar.
Solution
Answer:
import java.io.*;
public class DetectedShortestPathInMaze
{
static int number_of_rows=10; static int number_of_columns=10;
static int begin_of_row=5; static int begin_of_coloumn=3;
static int end_of_row=1; static int end_of_coloumn=6;
static int taken_maze[][]={{1,1,1,1,1,1,1,1,1,1},
{1,0,0,1,0,0,0,1,0,1},
{1,0,1,1,1,0,1,1,0,1},
{1,0,0,0,0,0,0,0,0,1},
{1,0,1,0,1,1,0,1,1,1},
{1,0,0,0,0,1,0,0,0,1},
{1,0,1,1,1,0,0,1,1,1},
{1,0,1,1,1,1,0,1,0,1},
{1,0,0,0,0,0,0,0,0,1},
{1,1,1,1,1,1,1,1,1,1}};
static int shortestpath[]=new int[number_of_rows*number_of_columns];
static int length_short;
boolean visitedalready(int row, int col, int detectedpathsofar[], int detectedlengthsofar){
int x;
int goal = row*number_of_columns+col;
for (x=0;x<detectedlengthsofar;x++)
if (detectedpathsofar[x]==goal) return true;
return false;
}
public void displayupdatedpath(int takenpath[], int takenlength){
int r,c;
for (r=0;r<number_of_rows;r++){
for(c=0;c<number_of_columns;c++){
if (taken_maze[r][c]==1)
System.out.print(\"|\");
else if (r==begin_of_row && c==begin_of_coloumn)
System.out.print(\"S\");
else if (r==end_of_row && c==end_of_coloumn)
System.out.print(\"X\");
else if (visitedalready(r,c,takenpath,takenlength))
System.out.print(\"o\");
else
System.out.print(\" \");
}
System.out.println(\"\");
}
}
public void searchnewpath(int row, int col, int detectedpathsofar[], int detectedlengthsofar){
if (row<0 || col<0 || row>=number_of_rows || col>=number_of_columns)
return;
if (taken_maze[row][col]==1) return ;
if (visitedalready(row, col, detectedpathsofar, detectedlengthsofar)) return;
int takenpath[]=new int[detectedlengthsofar+1];
System.arraycopy(detectedpathsofar, 0, takenpath, 0, detectedlengthsofar);
takenpath[detectedlengthsofar++]=row*number_of_columns+col;
if (row==end_of_row && col==end_of_coloumn){
System.out.println(\"Detected path of length \"+detectedlengthsofar+\":\");
displayupdatedpath(takenpath, detectedlengthsofar);
if (detectedlengthsofar<=length_short){
length_short=detectedlengthsofar;
System.arraycopy(takenpath, 0, shortestpath, 0, detectedlengthsofar);
System.out.println(\" The new shortest path is of length \" + detectedlengthsofar);
}
System.out.println(\"\");
return;
}
searchnewpath(row-1, col, takenpath, detectedlengthsofar);
searchnewpath(row, col-1, takenpath, detectedlengthsofar);
searchnewpath(row, col+1, takenpath, detectedlengthsofar);
searchnewpath(row+1, col, takenpath, detectedlengthsofar);
}
public static void main(String[] args)
{
int r,c,x;
int detectedpathsofar[];
int detectedlengthsofar;
DetectedShortestPathInMaze obj=new DetectedShortestPathInMaze();
detectedpathsofar=new int[obj.number_of_rows*obj.number_of_columns];
for (x=0;x<obj.number_of_rows*obj.number_of_columns;x++){
obj.shortestpath[x]=-1;
detectedpathsofar[x]=-1;
}
obj.length_short=obj.number_of_rows*obj.number_of_columns+1;
detectedlengthsofar=0;
System.out.println(\"The Maze Is Shown As Below:\");
for (r=0;r<obj.number_of_rows;r++){
for (c=0;c<obj.number_of_columns;c++){
if (r==begin_of_row && c==begin_of_coloumn)
System.out.print(\"S\");
else if (r==end_of_row && c==end_of_coloumn)
System.out.print(\"x\");
else if (obj.taken_maze[r][c]==0)
System.out.print(\" \");
else System.out.print(\"|\");
}
System.out.println(\"\");
}
System.out.println(\"\");
System.out.println(\"Searching For Paths!!!!!\");
obj.searchnewpath(begin_of_row, begin_of_coloumn, detectedpathsofar, detectedlengthsofar);
System.out.println(\"\");
System.out.println(\"The shortest path was found with the following of length \"+ obj.length_short);
obj.displayupdatedpath(obj.shortestpath, obj.length_short);
}
}



