Last active
January 1, 2020 12:31
-
-
Save tvandervossen/0134bbda3a68365d96a4ec4f93ba23da to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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