diff --git a/Final_SA/.classpath b/Final_SA/.classpath new file mode 100644 index 0000000..12fc71b --- /dev/null +++ b/Final_SA/.classpath @@ -0,0 +1,6 @@ + + + + + + diff --git a/Final_SA/.gitignore b/Final_SA/.gitignore new file mode 100644 index 0000000..ae3c172 --- /dev/null +++ b/Final_SA/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/Final_SA/.project b/Final_SA/.project new file mode 100644 index 0000000..09de54a --- /dev/null +++ b/Final_SA/.project @@ -0,0 +1,17 @@ + + + Final_SA + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/Final_SA/.settings/org.eclipse.jdt.core.prefs b/Final_SA/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000..bb35fa0 --- /dev/null +++ b/Final_SA/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,11 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 +org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve +org.eclipse.jdt.core.compiler.compliance=1.8 +org.eclipse.jdt.core.compiler.debug.lineNumber=generate +org.eclipse.jdt.core.compiler.debug.localVariable=generate +org.eclipse.jdt.core.compiler.debug.sourceFile=generate +org.eclipse.jdt.core.compiler.problem.assertIdentifier=error +org.eclipse.jdt.core.compiler.problem.enumIdentifier=error +org.eclipse.jdt.core.compiler.source=1.8 diff --git a/Final_SA/src/Chick.java b/Final_SA/src/Chick.java new file mode 100644 index 0000000..eec17d9 --- /dev/null +++ b/Final_SA/src/Chick.java @@ -0,0 +1,62 @@ +//package eggHunt; +/** + * Snakes in the Grass. + * Chick.java + * + * @author Alice Fischer + * @version March 18, 2012, from version of 4/30/07. + */ +import javafx.application.Platform; + +public class Chick extends Thread { + private EggHunt hunt; + private int speed; + //-------------------------------------------------------------------------- + public Chick( EggHunt hunt, int speed ) { + this.hunt = hunt; + this.speed = speed; + } + + //-------------------------------------------------------------------------- + /** The hen alternates between laying eggs and taking naps. + * The sleep command puts a thread into a suspended state. When the nap is + * over the interrupt system will throw an exception, which we receive in + * the catch block. + */ + public void run() { + System.out.println(" Chick is running. "); + try { + while ( ! hunt.isDone() ){ + Egg e = new Egg(10+Math.random()*380, 10+Math.random()*380 ); + synchronized (hunt) { + hunt.oEggList.add( e ); + hunt.laid++; + } + layOnGrass( e ); + // Lay eggs at slightly random times and take naps between eggs. + sleep (speed + (int)(Math.random() * 1000 )); // milliseconds + } + } + // Come here when the nap is over. + catch (InterruptedException e) { return; } + } + + + //-------------------------------------------------------------------------- + public void layOnGrass( final Egg eg ){ + Platform.runLater( new Runnable() { + public void run() { + hunt.grass.getChildren().add( eg ); + } + }); + } + + //-------------------------------------------------------------------------- + public void tracker( final String s ){ + Platform.runLater( new Runnable() { + public void run() { + hunt.message.setText( s ); + } + }); + } +} \ No newline at end of file diff --git a/Final_SA/src/Egg.java b/Final_SA/src/Egg.java new file mode 100644 index 0000000..c3790bf --- /dev/null +++ b/Final_SA/src/Egg.java @@ -0,0 +1,32 @@ +//package eggHunt; +/** + * Egg.java + * The Data class for EggCarton + * + * @author Alice Fischer + * @version March, 2015, from earlier versions of March 2013, 3/18/08, 11/28/04. + */ +import javafx.scene.shape.Ellipse; +import javafx.scene.paint.*; +import javafx.geometry.Point2D; // for the click-point + +public class Egg extends Ellipse { + private static double EggLong = 18; + private static double EggHigh = 14; + + //-------------------------------------------------------------------------- + public Egg( double x, double y ) { + setCenterX( x ); + setCenterY( y ); + setRadiusX( EggLong ); + setRadiusY( EggHigh ); + + double r = Math.random()* .5 + .4; // Choose a color. + double g = Math.random()* .5 + .4; + double b = Math.random()* .5 + .4; + setFill( new Color( r, g, b, 1) ); + System.out.printf("Egg at %f.2, %f.2\n", x, y); + } + + Point2D getCenter() { return new Point2D(getCenterX(), getCenterY()); } +} \ No newline at end of file diff --git a/Final_SA/src/EggHunt.java b/Final_SA/src/EggHunt.java new file mode 100644 index 0000000..34971bc --- /dev/null +++ b/Final_SA/src/EggHunt.java @@ -0,0 +1,165 @@ +//package eggHunt; +/** + * Snakes in the eggList. + * EggHunt.java + * + * @author Alice Fischer + * @version March 18, 2012, from version of 4/30/07. + */ +import javafx.application.Application; +import javafx.application.Application.Parameters; +import javafx.application.Platform; +import javafx.stage.Stage; +import javafx.scene.Scene; +import javafx.scene.layout.*; +import javafx.scene.paint.*; +import javafx.scene.shape.Circle; +import javafx.scene.shape.Rectangle; +import javafx.scene.shape.Line; +import javafx.scene.control.*; +import javafx.scene.input.MouseEvent; +import javafx.event.EventHandler; +import javafx.scene.text.*; // Text +import javafx.geometry.*; // for Insets +import javafx.animation.*; +import javafx.util.*; + +import java.util.List; +import java.util.ArrayList; + +import javafx.collections.ObservableList; +import javafx.collections.ListChangeListener; +import javafx.collections.FXCollections; + +//============================================================================== +public class EggHunt extends Application { + // These are for the command-line arguments. + private int speed; + double radiusSq; + private long lifetime; + + // This is the model. + private boolean done = false; + int eggs = 1; // The number of eggs currently available. + int laid = 0; // The number of eggs the chick has laid. + final int goal = 15; // the number of eggs for chick to win. + + private ArrayList< Egg > eggList = new ArrayList(); + ObservableList< Egg > oEggList = FXCollections.observableArrayList(eggList); + + // This class is used to produce a concurrent threads that shares the model. + private Chick C; + //private Referee R; + + // These variables are part of the GUI view. + Text mouse = new Text(" "); // Click coordinate display + Text message = new Text(" "); // Win or lose message + + BorderPane mainPane = new BorderPane(); + Pane grass = new Pane(); // Display of the eggList. + Scene sn; + + //-------------------------------------------------------------------------- + // Chick will stop laying when done becomes true. + public boolean isDone() { return done; } + public long getLifetime() { return lifetime; } + + //========================================================================== + private void buildGui() { + Color bColor = new Color(.95, 1, .8, 1.); + BackgroundFill bFill = new BackgroundFill( bColor, null, new Insets(2)); + Background bkGrass = new Background( bFill ); + grass.setBackground( bkGrass ); + + VBox p2 = new VBox(); // Scoreboard. + p2.getChildren().addAll(message, mouse); + + mainPane.setCenter(grass); + mainPane.setBottom(p2); + sn = new Scene( mainPane, 400, 430 ); + System.out.println("Gui is built."); + } + + /*========================================================================= + * @version March 2015, from versions of March 18, 2012, 4/30/07. + * + * @param arg[0] Base frequency at which the hen lays eggs, in milliseconds. + * @param arg[1] Distance from snake's birthplace at which he can eat an egg. + * @param arg[2] Lifetime of a snake, in milliseconds. + */ + public void start( Stage st ) throws InterruptedException { + + // Get and process the command line arguments -------------------------- + Application.Parameters params = getParameters(); + final java.util.List argv = params.getRaw(); + + // If user supplied command-line arguments, use them. + if (argv.size() == 3) { + speed = Integer.parseInt(argv.get(0)); + double radius = Double.parseDouble( argv.get(1) ); + radiusSq = radius * radius; + lifetime = Long.parseLong( argv.get(2) ); + System.out.println("Arg 0 = " + speed + ", Arg 1 = " + radius + + ", Arg 2 = " + lifetime); + } + else { // Set the parameters to relatively small values. + speed = 0; + radiusSq = 900; + lifetime = 2000; + } + //---------------------------------------------------------------------- + grass.setOnMouseClicked( e -> { doClick( e ); } ); + + oEggList.addListener( new ListChangeListener(){ + public void onChanged( ListChangeListener.Change change) { + int eggs = oEggList.size(); // Current size of arraylist. + System.out.println (" List change event caught"); + + if (eggs >= goal) { + done = true; + tracker(" I won! There are " + eggs + " eggs left!"); + } + else if (eggs == 0) { // Announce the score. + done = true; + tracker(" Super! You won!" ); + } + else { + tracker(" There are " + eggs + " eggs."); + } + } + } ); + + //---------------------------------------------------------------------- + buildGui(); + st.setTitle("Snakes in the Grass"); + st.setScene( sn ); + st.show(); + + // Get a new Chicken and tell it to start laying eggs. + C = new Chick( this, speed ); + C.start(); + } + + //-------------------------------------------------------------------------- + public void tracker( final String s ){ + Platform.runLater( new Runnable() { + public void run() { + message.setText( s ); + } + }); + } + + //========================================================================== + // Handle a mouse click by spawning a Snake thread for that board position. + public void doClick( MouseEvent ev ){ + double x = ev.getX(); + double y = ev.getY(); + mouse.setText(" New snake at ("+ x +", " +y +")" ); + System.out.print(" New snake at ("+ x +", " +y +") " ); + Snake hiss = new Snake( this, ev, lifetime ); + hiss.start(); + } + public static void main(String[] args) { + Application.launch(args); + } +} diff --git a/Final_SA/src/MyPoint.java b/Final_SA/src/MyPoint.java new file mode 100644 index 0000000..087ba45 --- /dev/null +++ b/Final_SA/src/MyPoint.java @@ -0,0 +1,28 @@ +//package eggHunt; +/** + * Egg.java + * The Data class for EggCarton + * + * @author Alice Fischer + * @version March, 2015, from earlier versions of March 2013, 3/18/08, 11/28/04. + */ +import javafx.scene.shape.Ellipse; +import javafx.scene.paint.*; +import javafx.geometry.Point2D; // for the click-point + +public class MyPoint { + double x; + double y; + + //-------------------------------------------------------------------------- + public MyPoint( double x, double y ) { + this.x = x; + this.y = y; + } + + double distanceSq( Point2D pt2 ){ + double deltaX = x - pt2.getX(); + double deltaY = y - pt2.getY(); + return deltaX * deltaX + deltaY * deltaY; + } +} \ No newline at end of file diff --git a/Final_SA/src/Snake.java b/Final_SA/src/Snake.java new file mode 100644 index 0000000..aef57bc --- /dev/null +++ b/Final_SA/src/Snake.java @@ -0,0 +1,81 @@ +//package eggHunt; +/** + * Snakes in the Grass. + * Snake.java + * + * @author Alice Fischer + * @version March 18, 2012, from version of 4/30/07. + */ + +import javafx.geometry.Point2D; // for the click-point +import javafx.scene.input.MouseEvent; +import javafx.event.EventHandler; +import javafx.application.Platform; + +public class Snake extends Thread { + private EggHunt hunt; + private MyPoint p; // The point at which the snake is born. + private final long death; + private static int shared=1; + private int serial; + + //-------------------------------------------------------------------------- + public Snake( EggHunt hunt, MouseEvent ev, long lifetime ) { + this.hunt = hunt; + serial = shared++; + p = new MyPoint( ev.getX(), ev.getY()); + death = lifetime + System.currentTimeMillis(); + } + //-------------------------------------------------------------------------- + public void run() { + int eggs; + long now = 0; // Initialize it to make eclipse happy. + do { + eggs = eatEggs(); + System.out.printf("Snake %d found %d eggs.\n", serial, eggs); + try { + synchronized( hunt ) { + // Hibernate until Chick lays another egg. + hunt.wait( death - System.currentTimeMillis() ); + hunt.notify(); + } + } + catch (InterruptedException e) { continue; } + + now = System.currentTimeMillis(); + } while ( now < death ); + // Snake will die a natural death now. + } + + //-------------------------------------------------------------------------- + // Eats the eggs that are close enough to point p. + public int eatEggs() { // Called by the Snakes. + int ate = 0; + Egg egg; + double dSq; + + // Lock the shared eggList before searching it. + synchronized (this) { + for(int k=0; k