In my college Java course, I wrote an ASCII Turtle Graphics. My professor said the only thing he could suggest was to turn the turtle into a class and create an instance of it (I hadn't because we hadn't covered classes yet). What do you think?
package javaapplication74;
import java.util.Scanner;
import static java.lang.Integer.parseInt;
public class JavaApplication74 {
// create Scanner for input
static Scanner in = new Scanner(System.in);
// enumerator of commands
private enum Commands {RESET, UP, DOWN, RIGHT, LEFT, FORWARD, DISPLAY, ERASE, REVERSE, POSITION, STOP, ERROR};
// enumerator of directions turtle can face
private enum Direction {UP, DOWN, RIGHT, LEFT};
// array of valid commands
static final char[] commands = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'r' };
// width and height of room;
static final int ROOM = 20;
// turtle's position
static int[] position = {0, 0};
// number of squares to move
static int move = 0;
// Write a "turtle graphics" program
public static void main(String[] args)
{
// array of drawn lines
boolean[][] floor = new boolean[ROOM][ROOM];
// store command
Commands command;
// store current direction - default right
Direction direction = Direction.RIGHT;
// pen in drawing condition
boolean draw = false;
boolean erase = false;
// provide directions
directions();
// get and execute commands until stop
do {
// get command
command = inputCommand();
// analyse and execute command
switch(command) {
case RESET:
reset(floor);
case UP:
draw = false;
erase = false;
break;
case DOWN:
draw = true;
erase = false;
break;
case RIGHT:
case LEFT:
direction = turn(command, direction);
break;
case FORWARD:
floor = move(floor, direction, draw, erase);
break;
case DISPLAY:
display(draw, floor);
break;
case ERASE:
draw = false;
erase = true;
break;
case REVERSE:
reverse(floor);
break;
case POSITION:
showPosition(direction);
break;
case STOP:
case ERROR:
break;
} // end switch
} while(command != Commands.STOP);
// end do-while
} // end method main
// Provide directions
public static void directions()
{
System.out.println("This is a turtle graphics program.");
System.out.println("You have 11 commands available, use the number to the right to use that command:");
System.out.printf("%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n", "0: Clear floor.", "1: Pen up, eraser up (default position).", "2: Pen down.", "3: Turn right.", "4: Turn left.",
"5: Go forward x spaces (enter 5,x)", "6: Display current layout", "7: Erase", "8: Show turtle's position", "9: Quit program", "r: Draw or erase current square.");
System.out.println("Note: the turtle will draw one more symbol than the number of spaces entered\n\tbecause it will draw in the square it is initially in.");
} // end method main
// Input command
public static Commands inputCommand()
{
// store command as string
String command;
// input command
do {
System.out.print("Enter your command: ");
command = in.next();
} while(!valid(command.charAt(0)));
// end do-while
// return command as enum value
switch(command.charAt(0)) {
case '0':
return Commands.RESET;
case '1':
return Commands.UP;
case '2':
return Commands.DOWN;
case '3':
return Commands.RIGHT;
case '4':
return Commands.LEFT;
case '5':
// recover from bad input
if(command.substring(1).isEmpty() || command.charAt(1) != ',' || command.substring(2).isEmpty())
return Commands.ERROR;
move = parseInt(command.substring(2));
return Commands.FORWARD;
case '6':
return Commands.DISPLAY;
case '7':
return Commands.ERASE;
case '8':
return Commands.POSITION;
case '9':
return Commands.STOP;
case 'r':
return Commands.REVERSE;
default:
return Commands.ERROR;
} // end switch
} // end method inputCommand
// Validate command
public static boolean valid(char c)
{
for(int ch : commands) {
if(c == ch)
return true;
} // end for
// if reach this point, not valid
return false;
} // end method valid
// Turn turtle
public static Direction turn(Commands c, Direction d)
{
// turn turtle
switch(d) {
case UP:
if(c == Commands.RIGHT)
d = Direction.RIGHT;
else
d = Direction.LEFT;
break;
case RIGHT:
if(c == Commands.RIGHT)
d = Direction.DOWN;
else
d = Direction.UP;
break;
case DOWN:
if(c == Commands.RIGHT)
d = Direction.LEFT;
else
d = Direction.RIGHT;
break;
case LEFT:
if(c == Commands.RIGHT)
d = Direction.UP;
else
d = Direction.DOWN;
break;
} // end switch
return d;
} // end method turn
// move turtle
public static boolean[][] move(boolean[][] floor, Direction d, boolean draw, boolean erase)
{
// get direction to move turtle
switch(d) {
// move turtle up
case UP:
for(; move > 0; move--) {
// draw, erase, or leave square
draw(draw, erase, floor);
// change position
position[0]--;
// return to caller if turtle hits wall
if(position[0] <= 0) {
// reset value
position[0] = 0;
// print warning
System.out.println("Your turtle hit the wall and had to stop.");
// stop moving turtle
break;
} // end if
} // end for
break;
// move turtle down
case DOWN:
for(; move > 0; move--) {
// draw, erase, or leave square
draw(draw, erase, floor);
// change position
position[0]++;
// make sure turtle does not walk through wall
if(position[0] >= ROOM) {
// reset value
position[0] = ROOM-1;
// print warning
System.out.println("Your turtle hit the wall and had to stop.");
// stop moving turtle
break;
} // end if
} // end for
break;
// move turtle right
case RIGHT:
for(; move > 0; move--) {
// draw, erase, or leave square
draw(draw, erase, floor);
// change position
position[1]++;
// make sure turtle does not walk through wall
if(position[1] >= ROOM) {
// reset value
position[1] = ROOM-1;
// print warning
System.out.println("Your turtle hit the wall and had to stop.");
// stop moving turtle
break;
} // end if
} // end for
break;
// move turtle left
case LEFT:
for(; move > 0; move--) {
// draw, erase, or leave square
draw(draw, erase, floor);
// change position
position[1]--;
// make sure turtle does not walk through wall
if(position[1] <= 0) {
// reset value
position[1] = 0;
// print warning
System.out.println("Your turtle hit the wall and had to stop.");
// stop moving turtle
break;
} // end if
} // end for
break;
} // end switch
// draw, erase, or leave square
draw(draw, erase, floor);
return floor;
} // end method move
// Draw, erase, or leave square
public static boolean[][] draw(boolean draw, boolean erase, boolean[][] floor)
{
// if draw, turtle cannot leave square without drawing
if(draw)
floor[position[0]][position[1]] = true;
// if erase, turtle cannot leave square without erasing
if(erase)
floor[position[0]][position[1]] = false;
return floor;
} // end method draw
// Display array
public static void display(boolean draw, boolean[][] array)
{
// loop through array
for(boolean b[] : array) {
for(boolean bb : b) {
// draw picture
if(bb)
System.out.print("*");
else
System.out.print(" ");
} // end inner for
// return at end of row
System.out.println();
} // end outer for
} // end method display
// Reverse value (draw or erase square) of current position
public static void reverse(boolean floor[][])
{
floor[position[0]][position[1]] = !floor[position[0]][position[1]];
} // end method reverse
// Show turtle's position
public static void showPosition(Direction d)
{
// x-y coordinate
System.out.printf("(%d, %d)\n", position[1], position[0]);
// direction
System.out.print("You are facing ");
switch(d) {
case UP:
System.out.println("up.");
break;
case DOWN:
System.out.println("down.");
break;
case RIGHT:
System.out.println("right.");
break;
case LEFT:
System.out.println("left.");
break;
} // end switch
} // end method showPosition
// Reset floor to blank
public static boolean[][] reset(boolean[][] floor)
{
for(boolean b[] : floor)
for(int i = 0; i < b.length; i++)
b[i] = false;
return floor;
} // end method reset
} // end class JavaApplication74