Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby 在地牢游戏中,将玩家从错误的方向解救出来_Ruby_Rescue - Fatal编程技术网

Ruby 在地牢游戏中,将玩家从错误的方向解救出来

Ruby 在地牢游戏中,将玩家从错误的方向解救出来,ruby,rescue,Ruby,Rescue,如果玩家键入的方向在游戏中不可用,我会尝试使用rescue将我的地下城游戏从错误中解救出来,并会再次向他们重复他们的位置并询问去哪里。以下是相关代码: def go(direction) puts "You go " + direction.to_s @player.location = find_room_in_direction(direction) show_current_description end def show_current_description

如果玩家键入的方向在游戏中不可用,我会尝试使用rescue将我的地下城游戏从错误中解救出来,并会再次向他们重复他们的位置并询问去哪里。以下是相关代码:

def go(direction)
    puts "You go " + direction.to_s
    @player.location = find_room_in_direction(direction)
    show_current_description
end

def show_current_description
    puts find_room_in_dungeon(@player.location).full_description
    puts "Where will you go?"
    answer = gets.chomp.downcase
        if answer == "exit"
            puts "You somehow teleported out of the cave. Good work."
            exit
        else
            answer = answer.to_sym
            begin
                go(answer)
            rescue
                puts "You can't go that way!"
                show_current_description
            end 
        end
end
这是我输入不可接受答案后得到的结果:

你发现自己身处一个巨大的洞穴中。西边是一个小洞

你要去哪里

东边

你向东走

你不能走那条路! 你不能走那条路! 你不能走那条路

undgeon.rb:40:in
show_current_description:nil:NilClass(NoMethodError)的未定义方法
full_description' 来自地牢。rb:52:in
rescue in show_current_description'
来自地牢。rb:48:in
show_current_description' 来自地牢。rb:20:in
start'
来自地牢。rb:89:in
'


下面是所有的代码:

你的救援抓错了东西。您执行
go
操作,它将分配下一个房间;但它是
nil
。然后失败的事情是在
nil
上查找描述。如果房间不存在,您需要阻止分配,而不是捕获失败的房间描述查找错误

编辑:像这样的东西可能没问题

def show_current_description
  loop do
    puts find_room_in_dungeon(@player.location).full_description

    puts "Where will you go?"
    answer = gets.chomp.downcase

    if answer == "exit"
      puts "You somehow teleported out of the cave. Good work."
      exit
    end

    next_location = find_room_in_direction(direction)
    if next_location
      puts "You go " + direction
      @player.location = next_location
      break
    else
      puts "You can't go that way!"
    end
  end
end

你的救援抓错了东西。您执行
go
操作,它将分配下一个房间;但它是
nil
。然后失败的事情是在
nil
上查找描述。如果房间不存在,您需要阻止分配,而不是捕获失败的房间描述查找错误

编辑:像这样的东西可能没问题

def show_current_description
  loop do
    puts find_room_in_dungeon(@player.location).full_description

    puts "Where will you go?"
    answer = gets.chomp.downcase

    if answer == "exit"
      puts "You somehow teleported out of the cave. Good work."
      exit
    end

    next_location = find_room_in_direction(direction)
    if next_location
      puts "You go " + direction
      @player.location = next_location
      break
    else
      puts "You can't go that way!"
    end
  end
end

你的救援抓错了东西。您执行
go
操作,它将分配下一个房间;但它是
nil
。然后失败的事情是在
nil
上查找描述。如果房间不存在,您需要阻止分配,而不是捕获失败的房间描述查找错误

编辑:像这样的东西可能没问题

def show_current_description
  loop do
    puts find_room_in_dungeon(@player.location).full_description

    puts "Where will you go?"
    answer = gets.chomp.downcase

    if answer == "exit"
      puts "You somehow teleported out of the cave. Good work."
      exit
    end

    next_location = find_room_in_direction(direction)
    if next_location
      puts "You go " + direction
      @player.location = next_location
      break
    else
      puts "You can't go that way!"
    end
  end
end

你的救援抓错了东西。您执行
go
操作,它将分配下一个房间;但它是
nil
。然后失败的事情是在
nil
上查找描述。如果房间不存在,您需要阻止分配,而不是捕获失败的房间描述查找错误

编辑:像这样的东西可能没问题

def show_current_description
  loop do
    puts find_room_in_dungeon(@player.location).full_description

    puts "Where will you go?"
    answer = gets.chomp.downcase

    if answer == "exit"
      puts "You somehow teleported out of the cave. Good work."
      exit
    end

    next_location = find_room_in_direction(direction)
    if next_location
      puts "You go " + direction
      @player.location = next_location
      break
    else
      puts "You can't go that way!"
    end
  end
end

大家好,欢迎来到Stack Overflow。请确保在问题本身中有尽可能多的相关代码。在这个问题中,相关代码是
go
方法;我们不需要离开现场。谢谢@Amadan。如果我一开始就知道它是相关的代码,我就会把它包括进去。说明我知道多少!这还是新鲜事!大家好,欢迎来到Stack Overflow。请确保在问题本身中有尽可能多的相关代码。在这个问题中,相关代码是
go
方法;我们不需要离开现场。谢谢@Amadan。如果我一开始就知道它是相关的代码,我就会把它包括进去。说明我知道多少!这还是新鲜事!大家好,欢迎来到Stack Overflow。请确保在问题本身中有尽可能多的相关代码。在这个问题中,相关代码是
go
方法;我们不需要离开现场。谢谢@Amadan。如果我一开始就知道它是相关的代码,我就会把它包括进去。说明我知道多少!这还是新鲜事!大家好,欢迎来到Stack Overflow。请确保在问题本身中有尽可能多的相关代码。在这个问题中,相关代码是
go
方法;我们不需要离开现场。谢谢@Amadan。如果我一开始就知道它是相关的代码,我就会把它包括进去。说明我知道多少!这还是新鲜事!你好@Amadan谢谢你的回复。我已经尝试根据你的建议修复我的救援,但我只是收到更多的错误消息。老实说,这是我第一次使用rescue,所以我仍在试图弄清楚它是如何工作的。你能再描述一点和/或告诉我什么是拯救围棋法的有效方法吗?嗨@Amadan谢谢你的回答。我已经尝试根据你的建议修复我的救援,但我只是收到更多的错误消息。老实说,这是我第一次使用rescue,所以我仍在试图弄清楚它是如何工作的。你能再描述一点和/或告诉我什么是拯救围棋法的有效方法吗?嗨@Amadan谢谢你的回答。我已经尝试根据你的建议修复我的救援,但我只是收到更多的错误消息。老实说,这是我第一次使用rescue,所以我仍在试图弄清楚它是如何工作的。你能再描述一点和/或告诉我什么是拯救围棋法的有效方法吗?嗨@Amadan谢谢你的回答。我已经尝试根据你的建议修复我的救援,但我只是收到更多的错误消息。老实说,这是我第一次使用rescue,所以我仍在试图弄清楚它是如何工作的。你能再描述一点和/或告诉我什么是拯救go方法的有效方法吗?