Flash Website Builder- Trendy Site Builder is a Flash Site Building tool that helps users build stunning websites. Check Out Custom Custom Logo Design by LogoBee. Website Design and Free Logo Templates available.
 CodingForums.com HELP W/Turtle!
 User Name Remember Me? Password

Before you post, read our: Rules & Posting Guidelines

Enjoy an ad free experience by logging in. Not a member yet? Register.
 04-18-2012, 11:16 PM PM User | #1 ConfusedW/Code 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 } }
 04-19-2012, 12:42 AM PM User | #2 Old Pedant Supreme Master coder!     Join Date: Feb 2009 Posts: 24,938 Thanks: 75 Thanked 4,305 Times in 4,272 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.
 04-19-2012, 12:49 AM PM User | #3 Old Pedant Supreme Master coder!     Join Date: Feb 2009 Posts: 24,938 Thanks: 75 Thanked 4,305 Times in 4,272 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.
 04-19-2012, 12:55 AM PM User | #4 ConfusedW/Code 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? =(
04-19-2012, 01:51 AM   PM User | #5
Old Pedant
Supreme Master coder!

Join Date: Feb 2009
Posts: 24,938
Thanks: 75
Thanked 4,305 Times in 4,272 Posts
Quote:
 Originally Posted by ConfusedW/Code 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.)

Quote:
 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.

 Bookmarks

 Thread Tools Rate This Thread Rate This Thread: 5 : Excellent 4 : Good 3 : Average 2 : Bad 1 : Terrible

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is On HTML code is Off Forum Rules
 Forum Jump User Control Panel Private Messages Subscriptions Who's Online Search Forums Forums Home :: Client side development     JavaScript programming         DOM and JSON scripting         Ajax and Design         JavaScript frameworks         Post a JavaScript     HTML & CSS     XML     Flash & ActionScript         Adobe Flex     Graphics and Multimedia discussions     General web building         Site reviews         Building for mobile devices :: Server side development     Apache configuration     Perl/ CGI     PHP         Post a PHP snippet     MySQL         Other Databases     Ruby & Ruby On Rails     ASP     ASP.NET     Java and JSP     Other server side languages/ issues         ColdFusion         Python :: Computing & Sciences     Computer Programming     Computer/PC discussions     Geek News and Humour Web Projects and Services Marketplace     Web Projects         Small projects (quick fixes and changes)         Medium projects (new script, new features, etc)         Large Projects (new web application, complex features etc)         Unknown sized projects (request quote)         Vacant job positions         Looking for work/ for hire         Project collaboration/ partnership         Paid work offers and requests (Now CLOSED)     Career, job, and business ideas or advice     Domains, Sites, and Designs for sale         Domains for sale         Websites for sale         Design templates and graphics for sale :: Other forums     Member Offers     Forum feedback and announcements

All times are GMT +1. The time now is 08:08 PM.