O problema é no método start que da erro no if:
if !valid_position? map, new_position
next
end
Erro
N:/Ruby Project/fogefoge/fogefoge.rb:60:in `start': undefined local variable or method `position' for main:Object (NameError)
Did you mean? new_position
from N:/Ruby Project/fogefoge/fogefoge.rb:73:in `start_fogefoge'
from N:/Ruby Project/fogefoge/main.rb:3:in `<main>'
eu não sei se isso é forma errada de escrita "por mais que esteja igual ao exemplo da aula" ou pode ser porque estou usando uma versão atual do ruby ou IDE ruby mine.
Código completo aqui:
require_relative "ui"
def read_map(number)
archive = "map#{number}.txt"
text = File.read archive
map = text.split "\n"
end
def find_player(map)
hero_character = "H"
map.each_with_index do |current_line, line|
column_hero = current_line.index hero_character
if column_hero
return [line, column_hero]
end
end
end
def calculates_new_position(hero, direction)
hero = hero.dup
case direction
when "W"
hero[0] -= 1
when "S"
hero[0] += 1
when "A"
hero[1] -= 1
when "D"
hero[1] += 1
end
hero
end
def valid_position?(map, position)
lines = map.size
columns = map[0].size
broke_lines = position[0] < 0 || position[0] >= lines
broke_columns = position[1] < 0 || position[1] >= columns
if broke_lines || broke_columns
return false
end
if map[new_position[0]][new_position[1]] == "X"
return false
end
true
end
def start(nick)
map = read_map(1)
while true
drawing map
direction = asks_movement
hero = find_player map
new_position = calculates_new_position hero, direction
if !valid_position? map, new_position
next
end
map[hero[0]][hero[1]] =" "
map[new_position[0]][new_position[1]] = "H"
end
end
def start_fogefoge
nick = welcome
start nick
end