"Imagine a pond with a frog in it. The pond is a 9x9 array with the frog in the center. Each square in the array is a lilypad.
Part 1: Have the frog do a set of 20 random jumps to adjacent lilypads and see if the frog jumps out of the pond. Stop if the frog does jump out of the pond. Repeat this set of 100 many times, i.e. put the frog back to the center each time, and keep a record of what percentage of the time the frog manages to leap out of the pond. The frog may come back to any lilypad any number of times.
Random jumping: the frog can jump any of 8 directions and lets assume they are all equally likely. Note that this is a bit different from random choices of deltaX = -1, 0 ,1 and deltaY = -1, 0, 1, so how would you implement an eight-choice random move?
Part 2: Keep a record of where the frog ends up at the end of each set of 100 jumps and at the end print out an array of these results."
I've almost got it, but i keep getting a type mismatch when I try to set the frog's position in the array. Here's my code thus far:
public class Pond { //The frog's starting position private int frogStartX = 5; private int frogStartY = 5; //The size of the pond private int pondWidth = 9; private int pondHeight = 9; //Max number of jumps private int maxJumps = 20; //The frog's last known position private int lastX; private int lastY; //Number of times the frog would escape the pond private int timesEscaped; //The frog private static Frog froggy; //The pond private static Lilipad lilies; Lilipad pond[][] = new Lilipad[pondWidth][pondHeight]; //The results private static int iterations = 1000; String results[] [] = new String[3][iterations +1]; public Pond(){ //generates the frog froggy = new Frog(frogStartX, frogStartY); //create 2-D pond matrix for (int i=0; i < pondWidth; i++){ for (int j=0; j < pondHeight; j++){ pond [i] [j]= new Lilipad(i,j); } } //puts the frog in middle of the pond pond [frogStartX] [frogStartY] = } public void reportresults(){ System.out.println("The Results of the Frog Jumper Simulator:"); System.out.println("Total number of trials: " + iterations); System.out.println("Total number of times the frog escaped the pond: " + timesEscaped); System.out.println("Total number of times the frog remained in the pond: " + (iterations - timesEscaped)); System.out.println("Last known position of the frog at the end of each iteration: "); } //Runs one twenty jump iteration public int startSimulation(){ String results[] [] = new String[3][iterations +1]; String trial = "trial number"; String lastknownPosition = "Last known coordinates"; results [0] [0] = trial; results [0] [1] = lastknownPosition; for(int iterations = 0; iterations<1000; iterations++){ for (int jumps = 0; jumps<maxJumps; jumps++){ froggy.jumpFrogjump(froggy.getNewXCoor(),froggy.getNewYCoor()); if (froggy.getNewXCoor() - froggy.getOldXCoor() < 0 || froggy.getNewXCoor() - froggy.getOldXCoor()>pondWidth || froggy.getNewYCoor() - froggy.getOldYCoor()<0 || froggy.getNewYCoor() - froggy.getOldYCoor()>pondHeight) { timesEscaped++; int lastX = froggy.getOldXCoor(); int lastY = froggy.getOldYCoor(); results [iterations +1][1] = toString(lastX, lastY); return lastX; } else { pond[froggy.getNewXCoor() - froggy.getOldXCoor()][froggy.getNewYCoor() - froggy.getOldYCoor()] = froggy; int lastX = froggy.getOldXCoor(); int lastY = froggy.getOldYCoor(); results [iterations +1][1] = toString(lastX, lastY); } return lastX; } } } public String toString(int lastX, int lastY) { return "X=" + lastX + " " + "Y=" + lastY; } public static void main(String[] args) { for (int iterations = 0; iterations<1000; iterations++){ Pond littlePond = new Pond(); littlePond.startSimulation(); //littlePond.reportResults(); } } }
Here is my frog class:
import java.util.Random; public class Frog { int currentX; int currentY; int newXCoor; int newYCoor; int xCoor; int yCoor; boolean escaped; public Frog(int xTemp, int yTemp) { xCoor = xTemp; yCoor = yTemp; } public void setXPosition(int XCoor){ newXCoor = XCoor; } public void setYPosition(int YCoor){ newYCoor = YCoor; } public int getOldXCoor(){ return currentX; } public int getOldYCoor(){ return currentY; } public int getNewXCoor(){ return newXCoor; } public int getNewYCoor(){ return newYCoor; } public void jumpFrogjump(int xNewPosition, int yNewPosition){ Random directionPick = new Random(); if (newXCoor == 0 && newYCoor == 0){ int newXCoor = directionPick.nextInt(3)-1; int newYCoor = directionPick.nextInt(3)-1; } } //Brings the frog back to the center of the pond public void reset(){ //pond[frogStartX][frogStartY] = froggy; } }
And here is my lilipad class:
public class Lilipad { private int x; private int y; public Lilipad(int xTemp, int yTemp) { x = xTemp; y = yTemp; } public int getX() { return x; } public int getY() { return y; } public void setX(int xTemp) { x = xTemp; } public void setY(int yTemp) { y = yTemp; } public String toString() { return "X=" + x + " " + "Y=" + y; } }
I've never used coordinates before and I have no idea how to do it! =(
Please help