首页 > 解决方案 > 卡在 ruby​​ 上的多项选择游戏中

问题描述

我刚开始使用 ruby​​ 并尝试创建一个多项选择游戏。我似乎看不出我在哪里弄错了,这使得它要么重复房间的通用行而不是显示结果选项。

仅供参考,大厅里的选项是“北”、“看”或“退出”,然后在书房里,选项是“看”、“看桌子”、“南”、“退出”、“输入组合” 2451"

下面的代码:

def hall_begin
#first line you see
  puts "you can either look around or move north"
  gets.chomp
end

def look_hall
# first option to look around in the hall
  puts "You are standing in a hall with a marble floor. You see a door."
  hall_begin
end

def onwards_study
# second option to go forwards into the next room from the hall
  puts "You are in the study, you can either look around of move back south"
  gets.chomp
end

def back_to_hall
# moving back into the hall from the study
  puts "You are back in the hall, either look around or go north"
  gets.chomp
end

def look_study
# looking around the study to find the desk and safe
  puts "You are in a warm and cosy study. You see a safe. You see a desk."
  onwards_study
end

def study_desk
# looking on the study desk to find the combination
  puts "You see a piece of paper that reads, The combination is 2451."
  onwards_study
end

def study_safe
# if you open the safe with combination
  puts "You see some diamonds in the safe, pick them up and make your escape"
end


def first_choice
# all the choices whilst in the hall
  while true
    direction_1 = hall_begin
    if direction_1 == "look"
      look_hall
    elsif direction_1 == "north"
      onwards_study
    elsif direction_1 == "quit"
      break
    end
  end
end

while true
# start of the game
  first_choice
    while true
    # all choices you face whilst in the study
        direction_2 = onwards_study
        if direction_2 == "look"
          look_study
        elsif direction_2 == "south"
          back_to_hall
        elsif direction_2 == "look at desk"
          study_desk
        elsif direction_2 == "enter combination 2451"
          study_safe
          break
        elsif direction_2 == "quit"
          break
        end
    break
    end
end

标签: rubymultiple-choice

解决方案


正如 Chandan 指出的那样,有这么多循环是不必要的。我认为您所追求的可以通过拥有一个获取用户输入然后处理它的主循环来实现。

由于您刚刚开始,我不想提出太复杂的建议,但我认为跟踪“current_room”变量是有帮助的(以后可以转换为 2D 房间阵列或其他东西上的坐标)。

也给你几个例子,这就是如何实现类似的东西。

def describe_room(current_room)
  if current_room == "hall"
    puts "You are standing in the hall, either look around or go north"
  elsif current_room == "study"
    puts "You are in the study, you can either look around of move back south"
  end
end

def examine_room(current_room)
  if current_room == "hall"
    puts "You are standing in a hall with a marble floor. You see a door."
  elsif current_room == "study"
    puts "You are in a warm and cosy study. You see a safe. You see a desk."
  end
end

def move(current_room, direction)
  if current_room == "hall" and direction == "north"
    "study"
  elsif current_room == "study" and direction == "south"
    "hall"
  else
    puts "You cannot go that way"
    current_room
  end
end

def hall_commands(command)
  # No hall specific commands at this points
  puts "Unknown command"

  # Return "hall" since we are never moving anywhere else
  "hall"
end

def study_commands(command)
  if command == "look at desk"
    puts "You see a piece of paper that reads, The combination is 2451."

  elsif command == "enter combination 2451"
    puts "You see some diamonds in the safe, pick them up and make your escape"
    return nil # Use explicit return statement to avoid default return at the end of the method

  else
    puts "Unknown command"
  end

  # Return "study" as the default
  "study"
end

# Starting position
current_room = "hall"

while true
  break if current_room == nil

  # Start each loop by a brief description of the current room.
  describe_room(current_room)

  # Get the user input in the main loop
  command = gets.chomp

  # Check for global commands (i.e. movements, look, etc.) first
  # and then move on to check for room specific commands. 
  if command.in?(["north", "east", "south", "west"])
    current_room = move(current_room, command)

  elsif command == "look"
    examine_room(current_room)

  elsif command == "quit"
    break

  elsif current_room == "hall"
    current_room = hall_commands(command)

  elsif current_room == "study"
    current_room = study_commands(command)
  
  else
    puts "Unknown command"
  end
end

基本上我已经将它简化为一个循环,如前所述,然后将无论您在哪个房间都可以使用的“全局命令”和仅适用于某些房间的“房间特定命令”分开。

我希望这可以帮助你进入 Ruby。当您感觉更舒服时,我建议您查看 case/when 语句作为 if/elsif/else 语句的替代方法,以及用于跟踪房间和位置的数组。


推荐阅读