This is a Toy Robot Simulator application built using Clean Architecture and the Command Design Pattern. The application simulates a toy robot moving on a 5x5 grid tabletop. Users can issue commands to place, move, rotate, and report the robot's position.
The project follows Clean Architecture, which separates the application into distinct layers:
- Contains the core business logic and entities.
- Robot.cs represents the toy robot and encapsulates its state (position, facing direction) and behavior (move, rotate, report).
- Direction.cs defines the Direction enum (
NORTH
,EAST
,SOUTH
,WEST
).
- Contains the application's use cases and commands.
- Commands contains the command classes (
PlaceCommand
,MoveCommand
,LeftCommand
,RightCommand
,ReportCommand
) that implement theICommand
orIReport
interfaces. - Interfaces contains the interfaces (
IBaseCommand
,ICommand
,IReport
) that define the contract for commands and reporting.
- Handles external concerns like parsing user input.
- CommandParser.cs parses user input into command objects.
- Handles user interaction.
- Program.cs is the entry point of the application. It reads user input, parses it into commands, and executes them.
- Contains unit tests for the application.
- ToyRobotSimulator.Tests contains tests for the
Robot
class and command classes.
The application uses the Command Design Pattern to encapsulate each command (PLACE, MOVE, LEFT, RIGHT, REPORT) as an object. This pattern allows us to:
- Decouple the command execution logic from the main program.
- Easily extend the application by adding new commands without modifying existing code.
- Support undo/redo functionality (if needed in the future).
- ICommand Interface: Defines the
Execute
method that all commands must implement. - IReport Interface: Defines the
GetReport
method for commands that generate output (e.g.,ReportCommand
).
- Places the robot on the table at position
(X, Y)
facingNORTH
,SOUTH
,EAST
, orWEST
. - Example:
PLACE 0,0,NORTH
- Moves the robot one unit in the direction it is currently facing.
- Example:
MOVE
- Rotates the robot 90° anticlockwise.
- Example:
LEFT
- Rotates the robot 90° clockwise.
- Example:
RIGHT
- Outputs the robot's current position and facing direction.
- Example:
REPORT
=> Output:0, 0, NORTH
Input:
PLACE 0,0,NORTH
MOVE
REPORT
Output:
0, 1, NORTH
- Clone the repository:
git clone https://github.com/hasitha93/ToyRobotSimulator.git
- Navigate to the
ToyRobotSimulator.ConsoleApp
folder:
cd ToyRobotSimulator/ToyRobotSimulator.ConsoleApp
- Run the application:
dotnet run
- Enter commands like PLACE 0,0,NORTH, MOVE, LEFT, RIGHT, and REPORT to control the robot.
- Navigate to the
ToyRobotSimulator.Tests
folder:
cd ToyRobotSimulator/ToyRobotSimulator.Tests
- Run the tests:
dotnet test
- Clean Architecture: The application is divided into layers (
Domain
,Application
,Infrastructure
,Presentation
) to ensure separation of concerns and maintainability. - Command Design Pattern: Each command is encapsulated as an object, making the code modular and extensible.
- Validation: The robot is prevented from falling off the table. Invalid commands are ignored.
- Unit Tests: Comprehensive unit tests ensure the application behaves as expected.