|
New to the CF scene
Join Date: Apr 2012
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
|
HELP W/Turtle!
Hello,
So my code compiles and brings up the graphic screen window BUT its just blank white screen...nothing is happening! What is wrong? this is what its suppose to be doing:
In the first part of this assignment, you are asked to develop a simulation of a single, 2D random walk and to use turtle graphics to show the path that the particle takes. To do this, please implement a program that does the following:
1.Create a Turtle at (x0,y0) = (0,0) and set the X and Y scales (the drawing window’s internal coordinate system) to be X = (-25,+25) and Y = (-25,+25). Put the pen down. use two variables x and y to keep track of the Turtle’s current location, initializing them to zero.
2.Use a loop to iterate the “walking” behavior. You can use a constant to fix the number of steps in the walk.
3.Each time through the loop, choose one of the four directions (up, down, left, right) randomly. turn the Turtle to face in that direction. Move the Turtle forward one step (step size being 1.0) and update your x and y variables with the Turtle’s new location.
4.When your loop is done, compute the final, straight-line distance of the Turtle from the origin and output the number of iterations and the distance to the console.
The end of a random walk simulation results in an interesting graphical plot and the final distance. Experiment with different numbers of iterations and find a value for the number of iterations, N, that creates a random walk that covers some reasonable fraction of the 51×51 coordinate system.
this is the code:
import java.util.Random;
public class RandomWalk1 {
public static void main(String[] args) {
// play around with the numbers
double max = 25;
double min = -25;
Turtle turtle = new Turtle(0, 0, 90.0); //make the turtle facing up
turtle.penDown(); // We draw right from the starting point
turtle.setXscale(min,max);
turtle.setYscale(min,max);
//makes a 25x25 grid
double step = 1.0; //step size
double x=0;
double y=0;
int face=90;//facing up
//90=up
//180=left
//270=down
//0=right
int steps=25; //number of times it will run.(iterations)
Random randomGenerator = new Random(); //for random
for(int i=1;i<=steps;i++){//runs step number of times
int rand=randomGenerator.nextInt(4); //number between 0,3
//turn a direction
if (rand==0){
turtle.turnLeft(0);//don't turn
//
if (face==90){
y=y+1;
break;//break out of turn loop so other "if"s don't register.
}//if facing up, turns 0 degrees left, so +y, face stays same
if (face==180){
x=x-1;
break;
}//if facing left, turns 0 degrees left, so -x, face stays same
if (face==270){
y=y-1;
break;
}//if facing down, turns 0 degrees left, so -y, face stays same
if (face==0){
x=x+1;
break;
}//if facing right, turns 0 degrees left, so +x, face stays same
//
}//turn loop end
if (rand==1){
turtle.turnLeft(90);//turn left
//
if (face==90){
x=x-1;
face=180;
break;
}//if facing up, turns 90 degrees left, so -x, face to left
if (face==180){
y=y-1;
face=270;
break;
}//if facing left, turns 90 degrees left, so -y, face to down
if (face==270){
x=x+1;
face=0;
break;
}//if facing down, turns 90 degrees left, so +x, face to right
if (face==0){
y=y+1;
face=90;
break;
}//if facing right, turns 90 degrees left, so +y, face to up
//
}//turn loop end
if (rand==2){
turtle.turnLeft(180);//turn around
//
if (face==90){
y=y-1;
face=270;
break;
}//if facing up, turns around, so -y, face to down
if (face==180){
x=x+1;
face=0;
break;
}//if facing left, turns around, so +x, face to right
if (face==270){
y=y+1;
face=90;
break;
}//if facing down, turns around, so +y, face to up
if (face==0){
x=x-1;
face=180;
break;
}//if facing right, turns around, so -x, face to left
//
}//turn loop end
if (rand==3){
turtle.turnLeft(270);//turn right
//
if (face==90){
x=x+1;
face=0;
break;
}//if facing up, turns right, so +x, face to right
if (face==180){
y=y+1;
face=90;
break;
}//if facing left, turns right, so +y, face to up
if (face==270){
x=x-1;
face=180;
break;
}//if facing down, turns right, so -x, face to left
if (face==0){
y=y-1;
face=270;
break;
}//if facing right, turns right, so -y, face to down
//
}//turn loop end
//move one step
turtle.goForward(step);
//x,y has been updated^
//line distance = sqrt(x^2+y^2)
double dis=Math.sqrt(Math.pow(x,2)+Math.pow(y,2)); //not sure if you need to import
System.out.println(steps);
System.out.println(dis);
}//for loop walking end
}
}
|
|