Skip to content

Instantly share code, notes, and snippets.

@jamesrcounts
Created April 16, 2015 15:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jamesrcounts/952c3a2efbf666cd42cf to your computer and use it in GitHub Desktop.
Save jamesrcounts/952c3a2efbf666cd42cf to your computer and use it in GitHub Desktop.
Recursive Square Attempt
package org.teachingkidsprogramming.section05recursion;
import org.teachingextensions.logo.Tortoise;
public class RecursiveSquare
{
public static void main(String[] args) throws Exception
{
Tortoise.setSpeed(10);
double length = 100.0;
makeASquare(length);
}
private static void makeASquare(double length)
{
if (10 < length)
{
moveToTheSquareStart(length);
//
// Do the following 4 times
for (int i = 0; i < 4; i++)
{
// Move the Tortoise the current length
Tortoise.move(length);
// Run the recipe makeASquare with the current length divided by two
makeASquare(length / 2);
// If the current process count is less than 4 /???????
if (i < 4)
{
// Turn the tortoise 90 degrees to the right
Tortoise.turn(90);
}
}
moveBackToCenter(length);
//
// Set the current length to the current length times two
length *= 2;
//
//End of makeASquare recipe
}
}
private static void moveToTheSquareStart(double length)
{
// Run the recipe moveToTheSquareStart with the current length
//
// Create the moveToTheSquareStart recipe
// Set the pen up for the tortoise
Tortoise.penUp(); // I looked for "set*"
// Move the tortoise the current length divided by two
Tortoise.move(length / 2);
// Turn the tortoise 90 degrees to the left
Tortoise.turn(90 * 3);
// Move the tortoise the current length divided by two
Tortoise.move(length / 2);
// Turn the tortoise 180 degrees to the right
Tortoise.turn(180);
// Set the pen down for the tortoise
Tortoise.penDown();
// End of moveToTheSquareStart recipe
}
private static void moveBackToCenter(double length)
{
//
// Run the recipe moveBackToCenter with the current length
// Create the moveBackToCenter recipe
// Set the pen up for the tortoise
Tortoise.penUp();
// Turn the tortoise 90 degrees to the right
Tortoise.turn(90);
// Move the tortoise the current length divided by two
Tortoise.move(length / 2);
// Turn the tortoise 90 degrees to the right
Tortoise.turn(90);
// Move the tortoise the current length divided by two
Tortoise.move(length / 2);
// Turn the tortoise 180 degrees to the right
Tortoise.turn(180);
// Set the pen down for the tortoise
Tortoise.penDown();
//
// End of moveToTheSquareStart recipe
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment