Robozzle simulator
Description:
Introduction
Robozzle is a puzzle game which requires the player to write programs to get a robot through a maze, collecting all stars on it.
The best way to understand the game is probably to play it, please have a try on http://www.robozzle.com/
Your ultimate goal in the life is now to write a Robozzle simulator.
The task
You'll write a robozzle function which will take the board, the robot initial state and the program in paramters.
The board will be a 2 dimensions array containing cells. A cell is a Hash containing 2 elements:
- color: the color of the cell (one of
blue
,green
orred
) - star: a boolean indicating if the cell contains a star
The robot will be a Hash containing the following elements:
- x: the x position of the robot on the board
- y: the y position on the roboat board
- direction: one of
right
,left
,up
,down
Coordinates start at 0; 0 at the top left, the y axix goes "down" and the x axis goes to the "right", so if the robot is moving down
, it's y coordinate will increase.
The program will be an array of subprograms, containing at least one subprogram. Each subprogram is a list of actions, an action is a hash containing the following elements:
- action: the instruction to perform (see below for the possible instructions)
- if (optional): the color condition to perform the action (see "conditionals")
Your simulator will run turn by turn, until the end of the game, popping an action from the stack and executing it.
At the end of the game (see "end of the game" section), you will return a 2 elements array containg the final state of the board and the final state of the robot.
Your function might need to abort prematurely if there is an error (see "end of the game")
Instructions
Your simulator will support the following instructions:
forward
: move the robot 1 cell forward, following it's current directionturn_right
: turn the robot to the rightturn_left
: turn the robot to the leftpaint_blue
: change the color of the current cell to bluepaint_green
: change the color of the current cell to greenpaint_red
: change the color of the current cell to red- an integer: stack the instructions of the subprogram at the integer's index in the given program
Conditionals
Actions can be conditional, meaning they will be executed only if the current cell is of the same color as their if
element.
If an action does not fulfil the conditional, it's skipped.
The stack
Your simulator will use a stack to store the instructions to execute. You will initialize it with the instructions of the first subprogram.
The stack is Last-in, First-out, that means the last inserted instruction will be executed next.
Each turn, you'll have to pop the most recently inserted action from the stack and execute it.
Please note the subprogams are meant to be executed in the same order as given, so take care when putting their instructions on the stack.
End of the game
The game ends when the robot has collected all the stars on the board or if an error occurs.
You have to take care of the following errors:
- There is no more instructions to execute on the stack
- The robot moved out of the board (either outside of the board array boudaries or on a
nil
cell)
You have to raise an exception to signal it with a meaningful error message if any errors occurs.
Other remarks
The requirements might be harder to understand if you haven't yet play the game (and the game is really fun to play, you should really give it a try).
I have included 2 sample puzzles in the test cases so you can check how the simulator is expected to work, especially about dealing with the inputs and outputs.
Feel free to let me know in the comments if anything is unclear or if there is an issue with the Kata.
Happy coding!
Similar Kata:
Stats:
Created | Jul 25, 2017 |
Published | Jul 25, 2017 |
Warriors Trained | 637 |
Total Skips | 163 |
Total Code Submissions | 913 |
Total Times Completed | 89 |
Ruby Completions | 17 |
CoffeeScript Completions | 3 |
JavaScript Completions | 73 |
Total Stars | 43 |
% of votes with a positive feedback rating | 95% of 37 |
Total "Very Satisfied" Votes | 33 |
Total "Somewhat Satisfied" Votes | 4 |
Total "Not Satisfied" Votes | 0 |
Total Rank Assessments | 3 |
Average Assessed Rank | 4 kyu |
Highest Assessed Rank | 3 kyu |
Lowest Assessed Rank | 5 kyu |