问题描述
我刚开始使用 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
解决方法
正如 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 语句的替代方案,以及用于跟踪房间和位置的数组。