2
respostas

Heroi de movimento, mas apaga uma o muro

Olá pessoal!

Estou abrindo um novo tópico, pois a partir do Vídeo 8 ou 9, meu programa começou a não funcionar, ele fica assim:

Como funciona o erro:

-> Primeiro movimento, apaga o espaço

-> Segundo movimento, apaga o X

-> Assim, ele segue até erro quando estou saindo do mapa

C:/rubyIII/logic_game.rb:62:in []=': index 4 out of string (IndexError) from C:/rubyIII/logic_game.rb:62:indef_game' from C:/rubyIII/logic_game.rb:68:in init_game' from main.rb:3:in'

Segue o código da lógica:

require_relative 'ui'

def read_map(number)
    arquivo = "mapa#{number}.txt"
    texto =    File.read(arquivo)        
    map = texto.split "\n"
end

def find_player(map)
    letter_hero = "H"
    map.each_with_index do |line_actual , line|
        column = line_actual.index letter_hero
        if column
            return [line, column]
        end        
    end
end

def calc_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 position_valid?(map, position)
    lines = map.size
    column = map[0].size
    after_map_lines = (position[0] < 0) || (position[0] >= lines)
    after_map_column = (position[1] < 0) || (position[1] >= column)
    if map[position[0]][position[1]] == "X"
            return false
    end
    if after_map_lines ||  after_map_column
            return false
    end
    true
end

def def_game(name)
    map = read_map 1

    while true
        draw_map map
        direction = moviment_user
        hero = find_player map

        new_position = calc_new_position hero, direction

        if !position_valid? map, new_position
            next
        end
            map[hero[0]][hero[1]] = ""
            map[new_position[0]][new_position[1]] = "H"
    end
end

def init_game
    name = welcome_game     
    def_game name
end

Segue o ui.rb

def welcome_game
    puts "Seja bem vindo ao Pacman do Ruby"
    puts "Qual é o seu nome?"
    name = gets.strip
    puts "\n\n\n\n"
    puts "O jogo está sendo iniciado, #{name}\n"
    name
end
def draw_map(map)
    puts map
end
def moviment_user
    puts "Para onde deseja ir?"
    moviment = gets.strip
    moviment
end
2 respostas

Erro do espaço resolvido... Continua o erro:

C:/rubyIII/logic_game.rb:82:in []=': index 4 out of string (IndexError) from C:/rubyIII/logic_game.rb:82:indef_game' from C:/rubyIII/logic_game.rb:88:in init_game' from main.rb:3:in'

Cesar, pode passar o código completo dos arquivos main.rb e logic_game.rb? No seu post diz que o erro é na linha 82 do arquivo logic_game.rb, porém ao copiar pra um arquivo vi que o código compartilhado só tem 68 linhas.

Quer mergulhar em tecnologia e aprendizagem?

Receba a newsletter que o nosso CEO escreve pessoalmente, com insights do mercado de trabalho, ciência e desenvolvimento de software