Last active
August 29, 2015 14:13
-
-
Save mrowe/0f3e79cd8005458e7dd2 to your computer and use it in GitHub Desktop.
Toy Robot in Java 7
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.mikerowecode.robot; | |
import java.util.Scanner; | |
import java.util.StringTokenizer; | |
public class Robots { | |
public static void main(String[] args) { | |
Robot robot = new Robot(-1, -1, Direction.WEST); | |
Scanner in = new Scanner(System.in); | |
while (in.hasNextLine()) { | |
Command c = Command.fromString(in.nextLine()); | |
robot = c.go(robot); | |
} | |
} | |
} | |
enum Direction { | |
NORTH, EAST, SOUTH, WEST; | |
public Direction left() { return values()[((ordinal() + 3) % values().length)]; } | |
public Direction right() { return values()[((ordinal() + 1) % values().length)]; } | |
} | |
class Robot { | |
public static final int HEIGHT = 5; | |
public static final int WIDTH = 5; | |
public final int x; | |
public final int y; | |
public final Direction facing; | |
public Robot(int x, int y, Direction facing) { | |
this.x = x; | |
this.y = y; | |
this.facing = facing; | |
} | |
public boolean onTable() { | |
return x >= 0 && x < WIDTH && y >= 0 && y < HEIGHT; | |
} | |
} | |
abstract class Command { | |
public abstract Robot go(Robot robot); | |
public static Command fromString(String s) { | |
StringTokenizer tokenizer = new StringTokenizer(s); | |
String c = tokenizer.nextToken(); | |
if ("PLACE".equals(c)) { | |
if (tokenizer.hasMoreTokens()) { | |
String[] v = tokenizer.nextToken().split(","); | |
try { | |
return new Place(Integer.valueOf(v[0]), Integer.valueOf(v[1]), Direction.valueOf(v[2])); | |
} catch (Exception e) { | |
// fall through | |
} | |
} | |
} | |
if ("MOVE".equals(c)) return new Move(); | |
if ("LEFT".equals(c)) return new Left(); | |
if ("RIGHT".equals(c)) return new Right(); | |
if ("REPORT".equals(c)) return new Report(); | |
// default to noop | |
return new Command() { public Robot go(Robot robot) { return robot; } }; | |
} | |
} | |
class Place extends Command { | |
private final int x; | |
private final int y; | |
private final Direction dir; | |
public Place(int x, int y, Direction dir) { | |
this.x = x; | |
this.y = y; | |
this.dir = dir; | |
} | |
public Robot go(Robot robot) { | |
return new Robot(x, y, dir); | |
} | |
} | |
class Left extends Command { | |
public Robot go(Robot robot) { | |
return new Robot(robot.x, robot.y, robot.facing.left()); | |
} | |
} | |
class Right extends Command { | |
public Robot go(Robot robot) { | |
return new Robot(robot.x, robot.y, robot.facing.right()); | |
} | |
} | |
class Move extends Command { | |
public Robot go(Robot robot) { | |
if (robot.onTable()) { | |
Robot schrodingerRobot = null; | |
switch (robot.facing) { | |
case NORTH: | |
schrodingerRobot = new Robot(robot.x, robot.y + 1, robot.facing); | |
break; | |
case SOUTH: | |
schrodingerRobot = new Robot(robot.x, robot.y - 1, robot.facing); | |
break; | |
case EAST: | |
schrodingerRobot = new Robot(robot.x + 1, robot.y, robot.facing); | |
break; | |
case WEST: | |
schrodingerRobot = new Robot(robot.x + 1, robot.y, robot.facing); | |
break; | |
} | |
if (schrodingerRobot.onTable()) return schrodingerRobot; | |
} | |
return robot; | |
} | |
} | |
class Report extends Command { | |
public Robot go(Robot robot) { | |
System.out.printf("%d,%d,%s\n", robot.x, robot.y, robot.facing); | |
return robot; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment