This assignment you will create a C++ simulation of lions hunting a gazelle on t
ID: 656095 • Letter: T
Question
This assignment you will create a C++ simulation of lions hunting a gazelle on the Serengeti. The rules for the simulation are:
Lions move up to 1 meter each time step.
The Gazelle moves up to 2 meters each time step.
The lions win if at the start of any turn there are 2 or more Lions adjacent to the Gazelle.
The Gazelle wins if it avoids the Lions for 50 time steps.
The Gazelle starts in the center of the board.
The Lions start in random locations at least 5 meters away from the Gazelle.
You may use any intelligence scheme you like for your lions and gazelle. A good first approximation is to have the lions move toward the gazelle, and the gazelle to move away from the nearest lion. You can add some randomness to the direction if you like, or use other strategies. The board size should be 20 rows by 30 columns.
You will need to implement this as a two-dimensional array of pointers to a Creature class. The Creature class should have some means of telling whether a given instance is a lion or a gazelle, and should store its own "location" on the grid (r,c). You could also have it simply store its maximum movement instead of its type. Another helpful class would be a Vector class that can do vector computations in either polar or cartesian coordinates.
Lions and gazelles move in real space. In other words, a creature's location could be something like (2.1, 3.4). This would place the creature in the (2,3) "cell", which is where you should print it on the map. When a creature moves, it's current cell should "release" it (by changing the pointer to NULL), and the destination should "receive" it by setting its pointer to point to the creature. If the cell is already occupied, then the creature doesn't move. This can be accomplished in a way very similar to the Robot World.
Specific requirements
Correctly implement the Serengeti simulation with the following constraints:
Lions move up to 1 meter per turn
Gazelle moves up to 2 meters per turn
Lions win if 2 are adjacent to the gazelle at the start of the turn.
Gazelle wins if it has not been eaten by the end of turn 51.
Gazelle starts in center, lions start in random locations at least 5 meters from the gazelle.
Creatures move in real space and then are printed on the grid based upon the integer part of their location.
Creatures cannot move into cells occupied by other cretures, and may not move over the border.
Two dimensional array of pointers to creatures used to store the creatures.
Explanation / Answer
import javafx.animation.PathTransition;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.SplitPane;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.shape.CubicCurve;
import javafx.scene.shape.Line;
import javafx.scene.shape.MoveTo;
import javafx.scene.shape.Path;
import javafx.scene.shape.QuadCurveTo;
import javafx.stage.Stage;
import javafx.util.Duration;
public class BallOnCurve extends Application implements
EventHandler<ActionEvent> {
Button button;
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
primaryStage.setTitle("Animation: Ball on Curve");
button = new Button();
button.setText("Set Color");
button.setOnAction(new EventHandler<ActionEvent>() {
button = new button();
button.add(btStart);
button.add(btStop);
add(buttonl, BorderLayout.center);
btStart.addActionListener(new StartcoloringBallTest());
btStop.addActionListener(new StopcoloringBallTest());
}
public void handle(ActionEvent event) {
launch(args) ;
}
});
BorderPane layout = new BorderPane();
layout.setTop(getSPaneCurve());
layout.setBottom(getHBoxButtons());
Scene scene = new Scene(layout, 300, 500);
primaryStage.setScene(scene);
primaryStage.show();
}
public StackPane getSPaneCurve() {
StackPane graph = new StackPane();
Path path = new Path();
MoveTo moveTo = new MoveTo();
moveTo.setX(0);
moveTo.setY(100);
QuadCurveTo p2 = new QuadCurveTo();
p2.setX(30f);
p2.setY(40f);
p2.setControlX(5f);
p2.setControlY(50f);
QuadCurveTo p3 = new QuadCurveTo();
p3.setX(60f);
p3.setY(100f);
p3.setControlX(50f);
p3.setControlY(40f);
QuadCurveTo p4 = new QuadCurveTo();
p4.setX(100f);
p4.setY(160f);
p4.setControlX(70f);
p4.setControlY(155f);
QuadCurveTo p5 = new QuadCurveTo();
p5.setX(130f);
p5.setY(100f);
p5.setControlX(125f);
p5.setControlY(155f);
QuadCurveTo p6 = new QuadCurveTo();
p6.setX(160f);
p6.setY(40f);
p6.setControlX(140f);
p6.setControlY(50f);
QuadCurveTo p7 = new QuadCurveTo();
p7.setX(200f);
p7.setY(100f);
p7.setControlX(180f);
p7.setControlY(30f);
QuadCurveTo p8 = new QuadCurveTo();
p8.setX(240f);
p8.setY(160f);
p8.setControlX(210f);
p8.setControlY(150f);
QuadCurveTo p9 = new QuadCurveTo();
p9.setX(280f);
p9.setY(100f);
p9.setControlX(270f);
p9.setControlY(160f);
path.getElements().add(moveTo);
path.getElements().add(p2);
path.getElements().add(p3);
path.getElements().add(p4);
path.getElements().add(p5);
path.getElements().add(p6);
path.getElements().add(p7);
path.getElements().add(p8);
path.getElements().add(p9);
path.setFill(new Color(1, 0, 0, 0));
path.setStrokeWidth(3);
Line hline = new Line();
hline.setStartX(0.0);
hline.setStartY(100.0);
hline.setEndX(280.0f);
hline.setEndY(100.0f);
Line vline = new Line();
vline.setStartX(130.0f);
vline.setStartY(0.0f);
vline.setEndX(130.0f);
vline.setEndY(200.0f);
Circle circle = new Circle(125, 100, 50);
circle.setFill(Color.WHITE);
circle.setStroke(Color.BLACK);
PathTransition pt = new PathTransition();
pt.setDuration(Duration.millis(4000));
pt.setPath(circle);
pt.setNode(path);
pt.setOrientation(PathTransition.OrientationType.ORTHOGONAL_TO_TANGENT);
pt.setCycleCount(Timeline.INDEFINITE);
pt.setAutoReverse(true);
pt.play();
graph.getChildren().addAll(path, hline, vline);
return graph;
}
public HBox getHBoxButtons() {
HBox hBox = new HBox(15);
hBox.setPadding(new Insets(15, 5, 5, 5));
return hBox;
}
private EventHandler<KeyEvent> keyListener = new EventHandler<KeyEvent>() {
public void handle(KeyEvent event) {
//Up: Play Animation
//Down: Pause Animation
//Left: Slow Down Animation
//Right: Play Animation
if (event.getCode() == KeyCode.UP
|| event.getCode() == KeyCode.DOWN
|| event.getCode() == KeyCode.RIGHT
|| event.getCode() == KeyCode.LEFT) {
try {
if(event.getCode() == KeyCode.UP){
pt.pause();
}
if(event.getCode() ==KeyCode.DOWN) {
pt.play();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
public void handle(ActionEvent event)
{
launch(args) ;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.