Public feed #726383

Untitled

public java v1 · immutable
Raw Edit
·published 2007-10-05 03:59 UTC
rendered paste body
import java.util.*;/** * LOGO parser. * Parses commands and holds the array of turtles. */public class Parser{	/** the array of turtles */	private Turtle[] turtles;	/** the current (active) turtle. If set to null, all turtles are active */	private Turtle currentTurtle;	static final int MAX_TURTLES = 20;	/**	 * Constructs a new Parser object.	 * Initializes the turtle array.	 */	public Parser(){		turtles = new Turtle[MAX_TURTLES];	}	/**	 * Gets the turtle array.	 * @return the turtle array	 */	public Turtle[] getTurtles(){		return turtles;	}	/**	 * Parses each line for commands.	 * @param line the line to parse	 */	public void parseLine(String line){		Scanner scan = new Scanner(line);		String cmd = scan.next();		if(cmd.equalsIgnoreCase("forward") || cmd.equalsIgnoreCase("fwd") || cmd.equalsIgnoreCase("fd"))			forward(scan.nextInt());		else if(cmd.equalsIgnoreCase("rotate") || cmd.equalsIgnoreCase("rot"))			rotate(scan.nextInt());		else if(cmd.equalsIgnoreCase("left") || cmd.equalsIgnoreCase("lt") || cmd.equalsIgnoreCase("lft"))			rotate(90);		else if(cmd.equalsIgnoreCase("right") || cmd.equalsIgnoreCase("rt"))			rotate(-90);		else if(cmd.equalsIgnoreCase("back") || cmd.equalsIgnoreCase("bk"))			forward(-scan.nextInt());		else if(cmd.equalsIgnoreCase("turtle"))			newTurtle(scan.next());		else if(cmd.equalsIgnoreCase("activate"))			activate(scan.next());		else if(cmd.equalsIgnoreCase("remove"))			remove(scan.next());		else if(cmd.equalsIgnoreCase("quit"))			System.exit(0);	}	/**	 * Moves the turtle(s) forward.	 * @param dist the distance to move.	 */	public void forward(int dist){		if(currentTurtle == null){			for(Turtle turtle : turtles){				if(turtle != null)					turtle.move(dist);			}		}		else			currentTurtle.move(dist);	}	/**	 * Rotates the turtle(s).	 * @param angle the angle to rotate, in degrees	 */	public void rotate(int angle){		if(currentTurtle == null){			for(Turtle turtle : turtles){				if(turtle != null)					turtle.rotate(angle);			}		}		else			currentTurtle.rotate(angle);	}	/**	 * Creates a new turtle.	 * Calls the Turtle constructor, passing the name as a parameter.	 * @param name the name of the turtle	 */	public void newTurtle(String name){		int sentinel = 0;		if(!name.equalsIgnoreCase("ALL")){			for(Turtle turtle : turtles){				if(turtle != null)					if(turtle.getName() == name)						sentinel = 1;			}			if(sentinel == 0){				for(Turtle turtle : turtles){					if(turtle == null){						turtle = new Turtle(name);						System.out.println("Constructed");						break;					}				}			}		}	}	/**	 * Activates a turtle.	 * Activating "ALL" activates all turtles.	 * @param name the turtle to activate	 */	public void activate(String name){		if(name.equalsIgnoreCase("ALL"))			currentTurtle = null;		else			for(Turtle turtle : turtles){				if(turtle.getName() == name)					currentTurtle = turtle;			}	}	/**	 * Deletes a turtle.	 * @param name the turtle to delete	 */	public void remove(String name){		for(Turtle turtle : turtles){			if(turtle.getName() == name)				turtle = null;		}	}}