0

I'm working on a project where we are supposed to have a turtle that can take the commands forward, left, right, penUp, penDown, penColor and quit. When the pen is down it's supposed to draw a line between points. As the turtle moves it should leave a footprint. Left and right are supposed to change the direction in degrees. So far I can't get it to draw more points. This is the code I have so far one class main and one turtle

import java.util.Scanner;
public class Main {

public static void main(String[] args) {
    Turtle t = new Turtle();

    Scanner keyboard = new Scanner(System.in); 
    String command;

    StdDraw.setPenRadius(0.05);
    t.Draw();


    do {

    System.out.println("Enter A Command: forward, right, left, penup, pendown,      pencolor, or quit");
    command = keyboard.next();


    if (command.equalsIgnoreCase("quit")){

        break;
    }   
        else if (command.equalsIgnoreCase("pencolor")) {

        System.out.println("Enter A Color: Red, Green, Black, Yellow, Blue");
        String color = keyboard.next();
        t.setColor(color);
        t.Draw();
    }

    else if (command.equalsIgnoreCase("forward")) {
        System.out.println("Enter The Number of Steps You Would Like To Move");
        int steps = keyboard.nextInt();
        t.moveForward(steps);
        t.Draw();

    } else if (command.equalsIgnoreCase("right")) {
        System.out.println("Enter The Number Of Degrees You Would Like To Move");
        String radAngle = keyboard.next();
        t.Draw();

    } else if (command.equalsIgnoreCase("left")) {
        System.out.println("Enter The Number Of Degrees You Would Like To Move");
        String radAngle = keyboard.next();
        t.Draw();

    } else if (command.equalsIgnoreCase("penup")) {
        t.putPenUp();
        t.Draw();

    } else if (command.equalsIgnoreCase("pendown")) {
        t.putPenDown();
        t.Draw();

    }} while (!(command.equalsIgnoreCase("quit"))); 

}
}



public class Turtle {
private double location;
public double xCoord;
public double yCoord;
double direction;
private boolean penPosition;
public static boolean penDown = false;
public static boolean penUp = true;
private int red;
private int green;
private int blue;
private int steps;

public Turtle() {
    xCoord = .5;
    yCoord = .5;
    penPosition = penUp;
    location = 90;
}

public void Draw(){

    StdDraw.setPenColor(StdDraw.BLACK);
    StdDraw.point(xCoord, yCoord);

}

public void setColor(String color) {
if (color.equalsIgnoreCase("red")) {
    red = 255;
    green = 0;
    blue = 0;
} else if (color.equalsIgnoreCase("green")) {
    red = 0;
    green = 255;
    blue = 0;
} else if(color.equalsIgnoreCase("blue")) {
    red = 0;
    green = 0;
    blue = 255;
} else if(color.equalsIgnoreCase("yellow")) {
    red = 255;
    green = 255;
    blue = 0;
} else if(color.equalsIgnoreCase("black")) {
    red = 0;
    green = 0;
    blue = 0;
} else  {
        red = 0;
        green = 0;
        blue = 0;
    }
}


public void moveForward(int steps) {

    double radAngle = Math.toRadians(location);


    double newx = xCoord + (Math.cos(radAngle) * steps);
    double newy = yCoord + (Math.sin(radAngle) * steps);
    StdDraw.point(newx, newy);
    StdDraw.line(xCoord, yCoord, newx, newy);
    StdDraw.show();
    }



  public void putPenDown() {
     penPosition = penDown;
     if (true) {
        // StdDraw.line(x, y, xCoord, yCoord);
     } 
 } public void putPenUp() {
     penPosition = penUp;
    // StdDraw.line(xCoord, yCoord, newx, newy);
 }
}
cdlane
  • 40,441
  • 5
  • 32
  • 81
donkey brian
  • 29
  • 1
  • 4

1 Answers1

0

Your program has several problems: StdDraw thinks of the coordinate plane as going from 0.0 to 1.0 so your integer steps don't make sense unless you scale it into StdDraw coordinates -- in my example below I divide the plane into 100 steps; your moveForward() function didn't set the xCoord and yCoord variables to the new location after the move so the turtle never really went anywhere; it isn't clear why you rely on your Draw() so much as it doesn't really do much; the program exit logic was broken; the turning logic was broken and incomplete.

In my rework below, I've made some simplifications for example purposes: I've moved the main() routine into Turtle to avoid the extra file/class; I've implemented simpler colors since what you had wasn't working -- you can add back your RGB model; I've simplified the pen up/down logic to make it more practical.

import java.awt.Color;
import java.util.Scanner;

public class Turtle {
    private double degAngle;
    public double xCoord;
    public double yCoord;
    private boolean penDown;
    private Color penColor;
    private int steps;

    public Turtle() {
        xCoord = 0.5;
        yCoord = 0.5;
        penDown = true;
        penColor = StdDraw.BLACK;
        degAngle = 90;
        StdDraw.setPenRadius(0.01);
    }

    public void Draw() {
        if (penDown) {
            StdDraw.setPenColor(penColor);
            StdDraw.point(xCoord, yCoord);
        }
    }

    public void setColor(String color) {
        if (color.equalsIgnoreCase("red")) {
            penColor = StdDraw.RED;
        } else if (color.equalsIgnoreCase("green")) {
            penColor = StdDraw.GREEN;
        } else if (color.equalsIgnoreCase("blue")) {
            penColor = StdDraw.BLUE;
        } else if (color.equalsIgnoreCase("yellow")) {
            penColor = StdDraw.YELLOW;
        } else {
            penColor = StdDraw.BLACK;
        }

        this.Draw(); // show new color
    }

    public void moveForward(int steps) {

        double radAngle = Math.toRadians(degAngle);
        double newx = xCoord + (Math.cos(radAngle) * steps / 100);
        double newy = yCoord + (Math.sin(radAngle) * steps / 100);

        if (penDown) {
            StdDraw.setPenColor(penColor);
            StdDraw.line(xCoord, yCoord, newx, newy);
        }

        xCoord = newx;
        yCoord = newy;
    }

    public void turnRight(double angle) {
        degAngle += -angle;
    }

    public void turnLeft(double angle) {
        degAngle += angle;
    }

    public void putPenDown() {
        penDown = true;
    }

    public void putPenUp() {
        penDown = false;
    }

    public static void main(String[] args) {

        Turtle t = new Turtle();

        Scanner keyboard = new Scanner(System.in);
        String command;

        t.Draw(); // show turtle

        do {
            System.out.println("Enter a command: forward, right, left, penup, pendown, pencolor, or quit");

            command = keyboard.next();

            if (command.equalsIgnoreCase("quit")) {
                break;

            } else if (command.equalsIgnoreCase("pencolor")) {
                System.out.println("Enter a color: red, green, black, yellow, blue");
                t.setColor(keyboard.next());

            } else if (command.equalsIgnoreCase("forward")) {
                System.out.println("Enter the number of steps you would like to move");
                t.moveForward(keyboard.nextInt());

            } else if (command.equalsIgnoreCase("right")) {
                System.out.println("Enter the number of degrees you would like to turn");
                t.turnRight(keyboard.nextDouble());

            } else if (command.equalsIgnoreCase("left")) {
                System.out.println("Enter the number of degrees you would like to turn");
                t.turnLeft(keyboard.nextDouble());

            } else if (command.equalsIgnoreCase("penup")) {
                t.putPenUp();

            } else if (command.equalsIgnoreCase("pendown")) {
                t.putPenDown();
            }

        } while (true);

    System.exit(0);
    }
}

USAGE

% java Turtle
Enter a command: forward, right, left, penup, pendown, pencolor, or quit
pencolor 
Enter a color: red, green, black, yellow, blue
green
Enter a command: forward, right, left, penup, pendown, pencolor, or quit
forward
Enter the number of steps you would like to move
20
Enter a command: forward, right, left, penup, pendown, pencolor, or quit
left
Enter the number of degrees you would like to turn
120
Enter a command: forward, right, left, penup, pendown, pencolor, or quit
forward
Enter the number of steps you would like to move
20
Enter a command: forward, right, left, penup, pendown, pencolor, or quit
left
Enter the number of degrees you would like to turn
120
Enter a command: forward, right, left, penup, pendown, pencolor, or quit
forward
Enter the number of steps you would like to move
20
Enter a command: forward, right, left, penup, pendown, pencolor, or quit
quit
%

OUTPUT

enter image description here

cdlane
  • 40,441
  • 5
  • 32
  • 81