Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5

Thread: HELP W/Turtle!

  1. #1
    New to the CF scene
    Join Date
    Apr 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy 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


    }
    }

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,652
    Thanks
    80
    Thanked 4,640 Times in 4,602 Posts
    Ummm...this is the javaSCRIPT forum. Your question is about JAVA.

    About the only thing the two languages have in common are the first four letters of their names.

    Try the Java forum.

    Oh...and when you do, wrap your code in [ code ]...[/ code] tags (without spaces in those tags) so it is readable.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,652
    Thanks
    80
    Thanked 4,640 Times in 4,602 Posts
    By the way, your code is almost 4 times as long as it needs to be. Aside from making no sense.

    Why would it matter which way the turtle was *previously* facing as to what you would do when it is "now facing" 90 degrees???
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #4
    New to the CF scene
    Join Date
    Apr 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Turtle help..

    ummm...have you worked with TurtleGraphics?
    Can you fix it? =(

  • #5
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,652
    Thanks
    80
    Thanked 4,640 Times in 4,602 Posts
    Quote Originally Posted by ConfusedW/Code View Post
    ummm...have you worked with TurtleGraphics?
    Maybe 30 years ago? With the LOGO language, which had built-in turtle graphics. (Was another language about that time that used them, too, but can't remember what it's name was.)

    Can you fix it? =(
    Look here:
    http://www.codingforums.com/rules.htm
    Look at rule 1.5.

    No, I can't fix it.

    I can give you hints. I can give you assistance with *particular* parts of the problem. But I'm not going to do your homework for you.

    Start by rethinking the problem.

    Okay, you have this turtle sitting at (0,0). Now you randomly turn him to one of the four directions. And then you move him one unit.

    Let's say you turn him East. In your code, that would be turnLeft(270), right? Okay, so you will then move him one unit East. In other words, x += 1;

    Now, you randomly choose one of the four directions again. Say this time you pick 0 randomly and so you call turnLeft(0). He's still facing East. Wouldn't you *STILL* do x += 1;?????

    SO WHY do you have to make a *SEPARATE* set of checks each time that depends on how many degrees you turned him????

    Oh, what the heck. One more hint: WHY do you need to do an if test to figure out how much to turn? Why not simply turn 90 times your random integer? That is: 0*90==>>turnLeft(0), 1*90==>>turnLeft(90), etc.

    SO:
    Code:
        int rand=randomGenerator.nextInt(4); //number between 0,3
    
        //turn a direction
        turtle.turnLeft(n * 90);
    An now NO IF TEST is needed for the rand value!
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •