complete lab 10
This commit is contained in:
3 files changed
+136
No files matched your search
@@ -0,0 +1,51 @@
|
|||||||
|
package ca.bcit.comp1510.lab10;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Collisions between RandomWalkers.
|
||||||
|
* @author Braeden Sowinski
|
||||||
|
* @version 1.0.0
|
||||||
|
*/
|
||||||
|
public class Collisions {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* samePosition detects if two RandomWalkers
|
||||||
|
* share the same coordinates.
|
||||||
|
* @param a RandomWalker
|
||||||
|
* @param b RandomWaler
|
||||||
|
* @return boolean of matching coordinates
|
||||||
|
*/
|
||||||
|
public static boolean samePosition(RandomWalker a, RandomWalker b) {
|
||||||
|
return a.getX() == b.getX() && a.getY() == b.getY();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* main program entry.
|
||||||
|
* @param args unused
|
||||||
|
*/
|
||||||
|
public static void main(String[] args) {
|
||||||
|
final int bound = 2000000;
|
||||||
|
final int steps = 100000;
|
||||||
|
|
||||||
|
final int x1 = -3;
|
||||||
|
final int y1 = 0;
|
||||||
|
final int x2 = 3;
|
||||||
|
final int y2 = 0;
|
||||||
|
|
||||||
|
RandomWalker w1 = new RandomWalker(steps, x1, y1, bound);
|
||||||
|
RandomWalker w2 = new RandomWalker(steps, x2, y2, bound);
|
||||||
|
|
||||||
|
boolean collided = false;
|
||||||
|
int stepsTaken = 0;
|
||||||
|
|
||||||
|
for (int i = 0; i < steps && !collided; i++) {
|
||||||
|
w1.takeStep();
|
||||||
|
w2.takeStep();
|
||||||
|
collided = samePosition(w1, w2);
|
||||||
|
stepsTaken++;
|
||||||
|
}
|
||||||
|
|
||||||
|
String message = collided ? "Collision!" : "No Collision.";
|
||||||
|
System.out.println(message);
|
||||||
|
System.out.println("After " + stepsTaken + " steps");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,69 @@
|
|||||||
|
package ca.bcit.comp1510.lab10;
|
||||||
|
|
||||||
|
import java.util.Scanner;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* DrunkWalker stuff.
|
||||||
|
* @author Braeden Sowinski
|
||||||
|
* @version 1.0.0
|
||||||
|
*/
|
||||||
|
public class DrunkWalker {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* readValidInt reads and validates an integer.
|
||||||
|
* @param msg to print;
|
||||||
|
* @param scan to read input
|
||||||
|
* @return validated int
|
||||||
|
*/
|
||||||
|
private static int readValidInt(String msg, Scanner scan) {
|
||||||
|
int number = 0;
|
||||||
|
boolean valid = false;
|
||||||
|
|
||||||
|
do {
|
||||||
|
System.out.print(msg);
|
||||||
|
|
||||||
|
if (!scan.hasNextInt()) {
|
||||||
|
System.out.println(scan.next() + " is not a valid number.\n");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
number = scan.nextInt();
|
||||||
|
valid = true;
|
||||||
|
|
||||||
|
} while (!valid);
|
||||||
|
|
||||||
|
return number;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* main program entry.
|
||||||
|
* @param args unused
|
||||||
|
*/
|
||||||
|
public static void main(String[] args) {
|
||||||
|
|
||||||
|
Scanner scan = new Scanner(System.in);
|
||||||
|
|
||||||
|
int steps = readValidInt("Enter a maximum number of steps: ", scan);
|
||||||
|
int bound = readValidInt("Enter a boundary size: ", scan);
|
||||||
|
int simulations = readValidInt("Enter a number of simulations: ", scan);
|
||||||
|
|
||||||
|
scan.close();
|
||||||
|
|
||||||
|
int fallen = 0;
|
||||||
|
|
||||||
|
for (int i = 0; i < simulations; i++) {
|
||||||
|
RandomWalker drunk = new RandomWalker(steps, bound);
|
||||||
|
drunk.walk();
|
||||||
|
if (!drunk.inBounds()) {
|
||||||
|
fallen++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
System.out.println(
|
||||||
|
fallen
|
||||||
|
+ "/"
|
||||||
|
+ simulations
|
||||||
|
+ " drunk walkers have fallen."
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -146,4 +146,20 @@ public class RandomWalker {
|
|||||||
public int getMaximumDistance() {
|
public int getMaximumDistance() {
|
||||||
return this.maximumDistance;
|
return this.maximumDistance;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* getX coordinate.
|
||||||
|
* @return x
|
||||||
|
*/
|
||||||
|
public int getX() {
|
||||||
|
return this.x;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* getY coordinate.
|
||||||
|
* @return y
|
||||||
|
*/
|
||||||
|
public int getY() {
|
||||||
|
return this.y;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user