HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

ASCII Turtle Graphics

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
graphicsasciiturtle

Problem

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:
floo

Solution

Way too many comments. They should teach proper use of comments in school, instead of just saying "put comments everywhere!".

Good comments should say why. Comments that say what simply shouldn't exist.

This is probably the worst:

}   // end method main


The comment basically says "this is a scope-ending brace".

Single-letter identifiers aren't all that great:

public static void showPosition(Direction d)


I would have named it direction, and showPosition shouldn't be responsible for getting the string for the direction - there should be a function dedicated to that, and the string returned shouldn't include the punctuation / dot from the "you are facing ..." sentence - you have mixed concerns here.

Code Snippets

}   // end method main
public static void showPosition(Direction d)

Context

StackExchange Code Review Q#75365, answer score: 8

Revisions (0)

No revisions yet.