Ruby Robot


Collapse Content

These challenges are part of the Ruby and Rails Coding Contest. Solve coding challenges and win prizes! Ends Monday 10pm EDT

You are in charge of programming the new vacuum cleaner, the Ruby Robot 2000. The tiles in a square room can have varying levels of dirtiness from 0 to 3. The goal of the game is for the robot to clean every tile completely and return to base. The Robot loses charge every action it takes, but recharges fully when it returns to base. The base is always located at the starting position in the top left corner.


A Robotic Vacuum's Path (credit: Joe Jungmann)

Methods

In each challenge, you need to fill in the Ruby method do_stuff, which is invoked on every turn of the game. The Robot can only perform one action each turn. These are the two action methods (note the ! at the end):

  • robot.clean! - Cleans the current cell, reducing it's level of dirtiness by one unit.
  • robot.walk!(:direction) - The robot will walk one tile in the given direction.

There are four directions that you can pass as parameters:

    :up
:left  :right
   :down  

The Robot can call the following methods to find out more about the area:

  • robot.can_walk?(:direction) - This returns true if the robot can walk in the given direction (i.e. there's no wall there).
  • robot.energy - This returns an integer for the energy level of the robot. Every action the robot takes decreases its energy by 1.
  • robot.detects_dirt? - This returns true if the current tile (that the robot is on) is dirty.

Sample Code

This code below will have the robot clean it's way as it moves to the right wall, which it will then bump into until it dies. Improve this code to pass the challenge!

class Player

  def do_something(robot)
    if robot.detects_dirt?
        robot.clean!
    else
        robot.walk!(:right)
    end
  end

end

The Board

Here's the board after the first move:

☒☒☒☒☒☒☒
☒☆♟⚂⚀⚁☒
☒____⚀☒
☒____⚀☒
☒_____☒
☒_____☒
☒☒☒☒☒☒☒

The animation displays each dirty tile as a die numbered 1-3.

Challenge Specifics

  • The board is 7x7 including the walls and 5x5 excluding them.
  • All the dirty tiles are located adjacent to the walls.
  • The robot's full charge is 18 units of energy.
  • You must beat the level within 39 turns.

Challenge

Program the robot to clean the simple room below.

Please sign in or sign up to submit answers.

Alternatively, you can try out Learneroo before signing up.

Comments

Contact Us
Sign in or email us at [email protected]