TurtleGraphics: A Solution


import java.awt.*;
import java.applet.*;

public class TurtleGraphics extends Applet {
    // Program constants
    final int FLSIZE = 20, SQSIZE = 10;
    final int XOFF = 100, YOFF = 100;
    final int NORTH = 0, EAST = 1, SOUTH = 2, WEST = 3;
    final int UNMARKED = 0, MARKED = 1;
    final String headstr[] = {"North", "East", "South", "West"};

    // Instance variables
    Label commandLabel, operandLabel;
    TextField command, operand;
    int floor[][] = new int[FLSIZE][FLSIZE];
    int xPos = 0, yPos = 0;
    int heading = SOUTH;
    boolean penDown = false;


    public void init() {
        commandLabel = new Label("Command");
        operandLabel = new Label("Operand");
        command = new TextField(1);
        operand = new TextField(2);
        add(commandLabel);
        add(command);
        add(operandLabel);
        add(operand);
    }

    public void paint(Graphics g) {
        Color random;

        for (int i = 0; i < FLSIZE; i++) {
            for (int j = 0; j < FLSIZE; j++) {
                if (floor[i][j] == 0)
                    g.setColor(Color.black);
                else {
                    random = new Color( (float) Math.random(),
                                        (float) Math.random(),
                                        (float) Math.random() );
                    g.setColor(random);
                }
                g.fillRect(YOFF + SQSIZE*i, XOFF + SQSIZE*j, SQSIZE, SQSIZE);
            }
        }
    }

    public boolean action(Event event, Object o) {

        if (event.target == command) {
            machine(command.getText(),operand.getText());
            command.setText("");
            operand.setText("");
        }
        return true;
    }

    public void machine(String opcode, String opvalue) {
        int comcode = Integer.parseInt(opcode);
        int value = 0;

        switch (comcode) {
            case 1:
                penDown = false;
                break;
            case 2:
                penDown = true;
                floor[xPos][yPos] = MARKED;
                break;
            case 3:
                heading = (heading + 1) % 4; // turn right
                break;
            case 4:
                heading = (heading == 0) ? WEST: heading -1; // turn left
                break;
            case 5:
                value = Integer.parseInt(opvalue);
                switch (heading) {
                    case NORTH:
                        yPos = (yPos < value) ? FLSIZE - value + yPos : yPos - value;
                        if (penDown)
                            for (int i = 0; i <= value; i++)
                                floor[xPos][(yPos + i) % FLSIZE] = MARKED;
                        break;

                    case SOUTH:
                        if (penDown)
                            for (int i = 0; i <= value; i++)
                                floor[xPos][(yPos + i) % FLSIZE] = MARKED;
                        yPos = (yPos + value) % FLSIZE;
                        break;

                    case EAST:
                        if (penDown)
                            for (int i = 0; i <= value; i++)
                                floor[(xPos + i) % FLSIZE][yPos] = MARKED;
                            xPos = (xPos + value) % FLSIZE;
                        break;

                    default: // WEST
                        xPos = (xPos < value) ? FLSIZE - value + xPos : xPos - value;
                        if (penDown)
                            for (int i = 0; i <= value; i++)
                                floor[(xPos + i) % FLSIZE][yPos] = MARKED;
                        break;
                }
                break;
            case 6:
                repaint();
                break;
            case 7:
                for (int i = 0; i < FLSIZE; i++)
                    for (int j = 0; j < FLSIZE; j++)
                        floor[i][j] = UNMARKED;
                break;
        }
        if (comcode >= 1 && comcode <= 7)
            showStatus("Location: (" + xPos + "," + yPos + "); Heading = "
                    + headstr[heading] + (penDown ? "; Pen down" : "; Pen up"));
        else
            showStatus(String.valueOf(opcode) + " is an invalid command code");
    }
}