Class: Karel::World
- Inherits:
-
Object
- Object
- Karel::World
- Defined in:
- lib/karel/world.rb
Overview
The world in which Karel operates.
Instance Attribute Summary collapse
-
#height ⇒ Object
readonly
Returns the value of attribute height.
-
#karel ⇒ Object
Returns the value of attribute karel.
-
#width ⇒ Object
readonly
Returns the value of attribute width.
Instance Method Summary collapse
-
#[](row, column) ⇒ Object
Provides access to the square at the given row/column.
-
#add_beeper(row, column) ⇒ Object
Adds a beeper tot he world at the given row/column.
-
#clear?(row, column) ⇒ Boolean
True if the square could be moved into by Karel.
-
#create_from_string(definition, karel) ⇒ Object
Creates the world from a string that is a multi-line representation of the world.
-
#remove_beeper(row, column) ⇒ Object
Removes a beeper from the world at the given row/column.
-
#to_s ⇒ Object
Returns the string representation, which looks like the the string used to construct this.
Instance Attribute Details
#height ⇒ Object (readonly)
Returns the value of attribute height.
7 8 9 |
# File 'lib/karel/world.rb', line 7 def height @height end |
#karel ⇒ Object
Returns the value of attribute karel.
5 6 7 |
# File 'lib/karel/world.rb', line 5 def karel @karel end |
#width ⇒ Object (readonly)
Returns the value of attribute width.
6 7 8 |
# File 'lib/karel/world.rb', line 6 def width @width end |
Instance Method Details
#[](row, column) ⇒ Object
Provides access to the square at the given row/column
Returns a Square instance, on which you can call handy methods like beeper? and wall?
75 76 77 |
# File 'lib/karel/world.rb', line 75 def [](row,column) @world[row][column] end |
#add_beeper(row, column) ⇒ Object
Adds a beeper tot he world at the given row/column
raises a SquareOccupied exception if there is a beeper or wall at those coordinates.
48 49 50 |
# File 'lib/karel/world.rb', line 48 def add_beeper(row,column) self[row,column].put_beeper end |
#clear?(row, column) ⇒ Boolean
True if the square could be moved into by Karel
64 65 66 67 68 69 70 |
# File 'lib/karel/world.rb', line 64 def clear?(row,column) if in_bounds?(row,column) !self[row,column].wall? else false end end |
#create_from_string(definition, karel) ⇒ Object
Creates the world from a string that is a multi-line representation of the world. This isn’t the constructor because our DSL requires uppercase and we don’t want warnings by creating the world twice (once when declaring it as a constant in the Karel module and once inside the module method WORLD).
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/karel/world.rb', line 13 def create_from_string(definition,karel) @karel = nil @karel_instance = karel @karel_instance.reset @world = [] @width = 0 row = 0 column = 0 rows = definition.split(/\n/) @height = rows.size rows.each do |row_data| @width = row_data.length if row_data.length > @width row_data.split(//).each do |square| @karel = [row,column] if square == 'K' @world[row] ||= [] @world[row][column] = square_for(square) column += 1 end column = 0 row += 1 end raise InvalidWorld,"There is no Karel in this World" unless @karel fill_in_empty_spaces end |
#remove_beeper(row, column) ⇒ Object
Removes a beeper from the world at the given row/column
raises a NoBeeper exception if there is no beeper at those coordinates.
41 42 43 |
# File 'lib/karel/world.rb', line 41 def remove_beeper(row,column) self[row,column].pick_beeper end |
#to_s ⇒ Object
Returns the string representation, which looks like the the string used to construct this
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/karel/world.rb', line 81 def to_s string = "+" @width.times { string += '-' } string += "+\n" @height.times do |row| string += "|" @width.times do |column| kr,kc = karel if (kr == row) && (kc == column) string += @karel_instance.to_s else string += self[row,column].to_s end end string += "|\n" end string += "+" @width.times { string += '-' } string += "+\n" string end |