Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Pamplona, Spain, is one of many towns in Spain and elsewhere that hold an annual

ID: 3825125 • Letter: P

Question

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.

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.

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.

The following code generates the maze and it resets it when the reset button is pushed. However, when I press the arrow keys to move the player the entire maze becomes the same color as the player.

/******************************************************************************************************/

package lab8;

import javafx.application.Application;
import javafx.application.Platform;
import javafx.event.Event;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.input.MouseEvent;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;

public class MazeGUIPane extends Application {
   private BorderPane border = new BorderPane();
   private GridPane grid = new GridPane();
   private Label[][] labelCoordinates = new Label[25][25];
   static StreetMap map = new StreetMap();
   private Scene scene = new Scene(border);
   // Label square;
   int row;
   int column;

   public void start(Stage stage) {
       scene.getStylesheets().add(this.getClass().getResource("style.css").toExternalForm());
       grid.getStyleClass().add("grid");
       border.getStyleClass().add("border");
       Label pamplonaTitle = new Label("Map Of Pamplona");
       pamplonaTitle.getStyleClass().add("text");
       // Button startButton = new Button("Start");
       Button resetButton = new Button("Reset");
       HBox actualGrid = new HBox();
       HBox bottomButtons = new HBox();
       HBox title = new HBox();
       map.grid();
       for (column = 0; column < 25; column++) {
           for (row = 0; row < 25; row++) {
               Label square = new Label();
               // Label square = new Label();
               if (map.coordinates[column][row].getValue() == ' ') {
                   square.getStyleClass().add("path");
               } else if (map.coordinates[column][row].getValue() == 'S') {
                   square.setText("Start");
                   square.getStyleClass().add("start-end");
               } else if (map.coordinates[column][row].getValue() == 'P') {
                   square.getStyleClass().add("player");
               } else if (map.coordinates[column][row].getValue() == 'E') {
                   square.setText("End");
                   square.getStyleClass().add("start-end");
               } else {
                   square.getStyleClass().add("wall");
               }

               scene.addEventFilter(KeyEvent.KEY_PRESSED, e -> {
                  
                   column = 1;
                   if (e.getCode().equals(KeyCode.UP)) {
                       if (map.coordinates[column][row].getValue() == ' ' && row >= 0 && column >= 0) {
                           row++;
                           square.getStyleClass().removeAll("path");
                           square.getStyleClass().add("player");
                           map.coordinates[column][row].setRow(row);
                           map.coordinates[column][row].setColumn(column);
                           map.coordinates[column][row].setValue('P');
                       }
                   }
                   if (e.getCode().equals(KeyCode.DOWN)) {
                       if (map.coordinates[column][row].getValue() == ' ') {
                           row--;
                           square.getStyleClass().removeAll("path");
                           square.getStyleClass().add("player");
                           map.coordinates[column][row].setRow(row);
                           map.coordinates[column][row].setColumn(column);
                           map.coordinates[column][row].setValue('P');
                       }
                   }
                   if (e.getCode().equals(KeyCode.LEFT)) {
                       if (map.coordinates[column][row].getValue() == ' ' && column >= 0) {
                           column--;
                           square.getStyleClass().removeAll("path");
                           square.getStyleClass().add("player");
                           map.coordinates[column][row].setRow(row);
                           map.coordinates[column][row].setColumn(column);
                           map.coordinates[column][row].setValue('P');
                       }
                   }
                   if (e.getCode().equals(KeyCode.RIGHT)) {
                       if (map.coordinates[column + 1][row].getValue() == ' ') {
                           //column++;
                           square.getStyleClass().removeAll("path");
                           square.getStyleClass().add("player");
                           map.coordinates[column + 1][row].setRow(row);
                           map.coordinates[column + 1][row].setColumn(column);
                           map.coordinates[column + 1][row].setValue('P');
                       }
                   }
                   e.consume();

               });

               square.addEventHandler(MouseEvent.MOUSE_CLICKED, new EventHandler<Event>() {

                   @Override
                   public void handle(Event event) {
                       row = GridPane.getColumnIndex(square);
                       column = GridPane.getRowIndex(square);
                       if (map.coordinates[column][row].getValue() == 'W') {
                           square.getStyleClass().removeAll("wall");
                           square.getStyleClass().add("path");
                           map.coordinates[column][row].setRow(row);
                           map.coordinates[column][row].setColumn(column);
                           map.coordinates[column][column].setValue(' ');
                       } else {
                           square.getStyleClass().removeAll("path");
                           square.getStyleClass().add("wall");
                           map.coordinates[column][row].setRow(row);
                           map.coordinates[column][row].setColumn(column);
                           map.coordinates[column][row].setValue('W');
                       }

                   }
               });

               labelCoordinates[column][row] = square;
               grid.add(square, column, row);
           }
       }
       title.getStyleClass().add("title");
       title.getChildren().add(pamplonaTitle);
       bottomButtons.setPadding(new Insets(15, 12, 15, 12));
       bottomButtons.setSpacing(10);
       bottomButtons.getStyleClass().add("start-button");
       bottomButtons.getChildren().add(resetButton);
       actualGrid.getChildren().add(grid);
       border.setTop(title);
       border.setCenter(actualGrid);
       border.setBottom(bottomButtons);
       stage.setScene(scene);
       stage.show();

       resetButton.addEventHandler(MouseEvent.MOUSE_CLICKED, new EventHandler<Event>() {

           @Override
           public void handle(Event event) {
               stage.close();
               Platform.runLater(() -> new MazeGUIPane().start(new Stage()));
               stage.setScene(scene);
           }

       });

   }

   public static void main(String[] args) {
       launch(args);

   }

}

/******************************************************************************************************/

package lab8;

public class Coordinate {
   private int row;
   private int column;
   private char value;

   public Coordinate() {
       column = 0;
       row = 0;
       value = ' ';
   }

   public int getColumn() {
       return column;
   }

   public int getRow() {
       return row;
   }

   public char getValue() {
       return value;
   }

   public void setColumn(int columnInput) {
       column = columnInput;
   }

   public void setRow(int rowInput) {
       row = rowInput;
   }

   public void setValue(char valueInput) {
       value = valueInput;
   }

}

/******************************************************************************************************/

package lab8;

import lab8.Coordinate;

public class StreetMap {
   Coordinate[][] coordinates = new Coordinate[25][25];

   public void grid() {
       /*
       * String testRandomness = ""; for(int counter = 0; counter < 20;
       * counter++){ testRandomness += ((int)(Math.random() * 4)) + " "; }
       * System.out.println(test);
       */
       for (int column = 0; column < 25; column++) {
           for (int row = 0; row < 25; row++) {
               coordinates[column][row] = new Coordinate();

               if (row == 0 || column == 0 || row == 24 || column == 24) {
                   if (column == 0 && row == 1) {
                       coordinates[column][row].setValue('S');
                       coordinates[column][row].setColumn(column);
                       coordinates[column][row].setRow(row);
                   } else if (column == 24 && row == 23) {
                       coordinates[column][row].setValue('E');
                       coordinates[column][row].setColumn(column);
                       coordinates[column][row].setRow(row);
                   } else {
                       coordinates[column][row].setValue('W');
                       coordinates[column][row].setColumn(column);
                       coordinates[column][row].setRow(row);
                   }

               } else {
                   if (column == 1 && row == 1)
                       coordinates[column][row].setValue('P');
                       coordinates[column][row].setColumn(column);
                       coordinates[column][row].setRow(row);
                   if (((int) (Math.random() * 5)) == 1 && row != 1 && column != 1) {
                       coordinates[column][row].setValue('W');
                       coordinates[column][row].setColumn(column);
                       coordinates[column][row].setRow(row);
                   } else {
                       coordinates[column][row].setValue(' ');
                       coordinates[column][row].setColumn(column);
                       coordinates[column][row].setRow(row);
                   }
               }
           }
       }

   }

   public static void main(String[] args) {
       StreetMap streetMap = new StreetMap();
       streetMap.grid();
   }

}

/******************************************************************************************************/

package lab8;

public class Bull {

}

Explanation / Answer

//Sample.java
import java.util.*;

/*
class Example
{
   int x;
   Example(){}
   Example(int x){this.x = x;}
}

class AddingCustomObjects
{
   public static void main(String[] args)
   {
       TreeSet ts = new TreeSet();
       ts.add(new Example());
       //ts.add(new Example()); RE: Exception in thread "main" java.lang.ClassCastException: Example cannot be                                                                                                                                    cast to java.lang.Comparable
       //ts.add(new Example(20));
   }
}


public boolean add(Object obj)
{
   if (this.size() >1)
   {
       Comparable c = (Comparable)obj;
       c.compateTo(tsItr.next());
   }
}
*/

class Example implements Comparable
{
   int x;
  
   Example(){}
   Example(int x){this.x = x;}

   public int compareTo(Object obj)
   {
       Example e = (Example)obj;
      
       //System.out.println(x);
       //System.out.println(e.x);

       return e.x - this.x;
   }

   public String toString()
   {
       return ""+x;
   }
}

class AddingCustomObjects
{
   public static void main(String[] args)
   {

       TreeSet ts = new TreeSet();

       ts.add(new Example());
       ts.add(new Example(20));
       ts.add(new Example());
       ts.add(new Example(5));
       ts.add(new Example(30));
       ts.add(new Example(20));
       ts.add(new Example());

       System.out.println(ts.size());
       System.out.println(ts);
   }
}

-------------------------------------------------------------------

import java.util.*;

class AddingCustomObjectsToSetAndMap
{
   public static void main(String[] args)
   {
       Student s1 = new Student(1, 1, "Hari");
       Student s2 = new Student(2, 1, "Hari");
       Student s3 = new Student(1, 1, "Hair");
       Student s4 = new Student(1, 1, "Hiar");
       Student s5 = new Student(1, 1, "Hari");
       Integer io1 = new Integer(391);      
       Student s6 = new Student(1, 1, "aHri");
   //   System.out.println(s1.equals(s2));
//       System.out.println(s1.equals(s3));

//       System.out.println(s1.hashCode());
//       System.out.println(s2.hashCode());
//       System.out.println(s3.hashCode());
       System.out.println(io1.hashCode());

       HashSet hs = new HashSet();
       hs.add(s1);
       hs.add(s2);
       hs.add(s3);
       hs.add(s4);
       hs.add(s5);
       hs.add(io1);
       hs.add(s6);
       System.out.println(hs.size());
       System.out.println(hs);
   }
}

class Student
{
   int sno;
   int whichclass;
   String name;

   Student(int sno, int whichclass, String name)
   {
       this.sno               = sno;
       this.whichclass   = whichclass;
       this.name               = name;
   }

   public boolean equals(Object obj)
   {
       System.out.println("In equals");
       if (obj instanceof Student)
       {
           Student s = (Student)obj;
           if (this.sno == s.sno && this.whichclass == s.whichclass && this.name.equalsIgnoreCase(s.name))
           {
               return true;
           }
       }
       return false;
   }

   public int hashCode()
   {
       System.out.println("In hashcode");
       int size   = this.name.length();
       int result = 1;      
      
       for (int i = 0 ; i < size ; i ++ )
       {
           result = result + (name.charAt(i) ) ;
       }

       return this.sno + this.whichclass + result;      
   }
   public String toString()
   {
       return sno+ "-"+ whichclass +"-" + name;
   }
}