Skip to content

Instantly share code, notes, and snippets.

@tvandervossen
Last active January 1, 2020 12:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tvandervossen/0134bbda3a68365d96a4ec4f93ba23da to your computer and use it in GitHub Desktop.
Save tvandervossen/0134bbda3a68365d96a4ec4f93ba23da to your computer and use it in GitHub Desktop.
TYPES = {
'c' => 'coin',
'w' => 'wall'
}
def map_to_list(map)
list = []
map.strip!.gsub!(/\n\s*/, "\n")
map.split("\n").each_with_index do |row, y|
row.split('').each_with_index do |character, x|
unless TYPES[character].nil?
list << {
x: x,
y: y,
type: TYPES[character]
}
end
end
end
list
end
require "test/unit"
class TestMapMaking < Test::Unit::TestCase
def test_map_to_list
map = "
wwwwwwww
w------w
w--cc--w
"
objects = [
{x: 0, y: 0, type: 'wall'},
{x: 1, y: 0, type: 'wall'},
{x: 2, y: 0, type: 'wall'},
{x: 3, y: 0, type: 'wall'},
{x: 4, y: 0, type: 'wall'},
{x: 5, y: 0, type: 'wall'},
{x: 6, y: 0, type: 'wall'},
{x: 7, y: 0, type: 'wall'},
{x: 0, y: 1, type: 'wall'},
{x: 7, y: 1, type: 'wall'},
{x: 0, y: 2, type: 'wall'},
{x: 3, y: 2, type: 'coin'},
{x: 4, y: 2, type: 'coin'},
{x: 7, y: 2, type: 'wall'}
]
assert_equal objects, map_to_list(map)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment