Simulation of toy robot moving on a square tabletop
A robot is placed on a 5x5 tabletop. Instructions are issued to the robot via user input or via a text input file
Initially developed on
- Windows 10
- JRE 8 U73
- Maven 3.3.9
- JDK => 8U60
- Windows 10
- OS X El Capitan (10.11.4)
- Ubuntu 15.10
- Ubuntu 16.04
Load command prompt and change to working directory of robotsim class files
To run application in interactive mode:
java -jar jarfile
example.
$ java -jar robotsim-0.0.1-SNAPSHOT.jar
To run application from an input file:
java -jar jarfile pathtoinputfile
example.
$ java -jar robotsim-0.0.1-SNAPSHOT.jar c:\temp\instructions.txt
Note: Commands for interactive and input file are identical
#####PLACE X,Y,DIRECTION
Purpose: Places robot on tabletop. Only valid if robot is not on board already, will be discard otherwise.
X = X coordinate on tabletop to place robot. Valid inputs: 0-4 Y = Y coordinate on tabletop to place robot. Valid inputs: 0-4 DIRECTION = Direction for robot to face. Valid inputs are NORTH, SOUTH, EAST, WEST
#####MOVE
Purpose: Move robot one unit forward in the direction it is currently facing.
Only valid if movement does not take robot off the tabletop edge.
#####LEFT
Purpose: Rotate the robot 90 degrees to the left
example Robot is facing north, LEFT is issued, robot is now facing WEST
#####RIGHT
Purpose: Rotate the robot 90 degrees to the right
example Robot is facing north, RIGHT is issued, robot is now facing EAST
#####REPORT
Purpose: Reports the location of the robot and direction the robot is facing
example
REPORT 3,3,NORTH
Maven needs to be installed and working.
-
Download source (via zip or clone repository)
-
Browse to source root
-
Run the following to compile from source
$ mvn compile
-
Download source (via zip or clone repository)
-
Browse to source root
-
Run the following to compile and package from source
$ mvn package
-
Browse to source root
-
Run the following to execute tests
$ mvn test
For elaboration on the decision to dynamically generate commands at runtime and the advantages of this method see this document
Code is comprised of four main source files
Simulator.java
Contains main method, runs the simulator
Class does the following:
Takes and checks command line arguments passed to the program
Runs in either interactive mode or batch (file) mode depending on command line arguments passed
Initializes robot and table
Takes each command sequentially and processes them
Robot.java
Class representation of a robot
Does the following:
Takes a command and validates it against a given table
Executes a command against a given table
TabletTop.java
Class representation of Table Top
Does the following:
Initializes a table top with specific X and Y upper bounds
Direction.java
Direction class, uses hashtable internally to represent directions
This class was implemented to provide a way to arithmetically (via hashtable index) achieve rotation instructions
Does the following:
Returns string representation of direction based on hashtable index.
Command.java
Abstract class the all other commands in the game are derived from
Does the following:
Has three abstract methods that must be implemented by derived classes
- initializeCommand - takes command string, parses and validates string, populates internal state of command if valid.
- validateInstruction - validates a command against the current state of the game. Returns true if command is valid.
- executeInstruction - executes a valid command
CommandFactory.java
Maintains a list of valid commands and their relevant class representation in an internal hashtable. Used to dynamically generate command objects at runtime given string representation of a command
This project is licensed under the MIT License