Created
January 8, 2015 04:05
-
-
Save mrowe/4dbe46aff94532c0d73e to your computer and use it in GitHub Desktop.
Toy Robot in Java 8
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.robot8; | |
import java.util.Scanner; | |
import java.util.StringTokenizer; | |
import java.util.function.Function; | |
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()) { | |
robot = Commands.fromString(in.nextLine()).apply(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 Commands { | |
public static Function<Robot, Robot> 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 { | |
Integer x = Integer.valueOf(v[0]); | |
Integer y = Integer.valueOf(v[1]); | |
Direction f = Direction.valueOf(v[2]); | |
return robot -> new Robot(x, y, f); | |
} catch (Exception e) { | |
// fall through | |
} | |
} | |
} | |
if ("MOVE".equals(c)) return 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; | |
}; | |
if ("LEFT".equals(c)) return robot -> new Robot(robot.x, robot.y, robot.facing.left()); | |
if ("RIGHT".equals(c)) return robot -> new Robot(robot.x, robot.y, robot.facing.right()); | |
if ("REPORT".equals(c)) return robot -> { | |
System.out.printf("%d,%d,%s\n", robot.x, robot.y, robot.facing); | |
return robot; | |
}; | |
// default to noop | |
return robot -> robot; | |
} | |
} | |
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; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment