加载中...

习题 35: 分支 (Branches) 和函式 (Functions)


你已经学会了 if 语句、函式、还有阵列。现在你要练习扭转一下思维了。把下面的代码写下来,看你是否能弄懂它实现的是什么功能。

  1. def prompt()
  2. print "> "
  3. end
  4. def gold_room()
  5. puts "This room is full of gold. How much do you take?"
  6. prompt; next_move = gets.chomp
  7. if next_move.include? "0" or next_move.include? "1"
  8. how_much = next_move.to_i()
  9. else
  10. dead("Man, learn to type a number.")
  11. end
  12. if how_much < 50
  13. puts "Nice, you're not greedy, you win!"
  14. Process.exit(0)
  15. else
  16. dead("You greedy bastard!")
  17. end
  18. end
  19. def bear_room()
  20. puts "There is a bear here."
  21. puts "The bear has a bunch of honey."
  22. puts "The fat bear is in front of another door."
  23. puts "How are you going to move the bear?"
  24. bear_moved = false
  25. while true
  26. prompt; next_move = gets.chomp
  27. if next_move == "take honey"
  28. dead("The bear looks at you then slaps your face off.")
  29. elsif next_move == "taunt bear" and not bear_moved
  30. puts "The bear has moved from the door. You can go through it now."
  31. bear_moved = true
  32. elsif next_move == "taunt bear" and bear_moved
  33. dead("The bear gets pissed off and chews your leg off.")
  34. elsif next_move == "open door" and bear_moved
  35. gold_room()
  36. else
  37. puts "I got no idea what that means."
  38. end
  39. end
  40. end
  41. def cthulu_room()
  42. puts "Here you see the great evil Cthulu."
  43. puts "He, it, whatever stares at you and you go insane."
  44. puts "Do you flee for your life or eat your head?"
  45. prompt; next_move = gets.chomp
  46. if next_move.include? "flee"
  47. start()
  48. elsif next_move.include? "head"
  49. dead("Well that was tasty!")
  50. else
  51. cthulu_room()
  52. end
  53. end
  54. def dead(why)
  55. puts "#{why} Good job!"
  56. Process.exit(0)
  57. end
  58. def start()
  59. puts "You are in a dark room."
  60. puts "There is a door to your right and left."
  61. puts "Which one do you take?"
  62. prompt; next_move = gets.chomp
  63. if next_move == "left"
  64. bear_room()
  65. elsif next_move == "right"
  66. cthulu_room()
  67. else
  68. dead("You stumble around the room until you starve.")
  69. end
  70. end
  71. start()

你应该看到的结果

你可以结果:

  1. $ ruby ex35.rb
  2. You are in a dark room.
  3. There is a door to your right and left.
  4. Which one do you take?
  5. > left
  6. There is a bear here.
  7. The bear has a bunch of honey.
  8. The fat bear is in front of another door.
  9. How are you going to move the bear?
  10. > taunt bear
  11. The bear has moved from the door. You can go through it now.
  12. > open door
  13. This room is full of gold. How much do you take?
  14. > asf
  15. Man, learn to type a number. Good job!
  16. $

加分习题

  1. 把这个游戏的地图画出来,把自己的路线也画出来。
  2. 改正你所有的错误,包括拼写错误。
  3. 为你不懂的函式写注解。记得 RDoc 中的注释吗?
  4. 为游戏添加更多元素。通过怎样的方式可以简化并且扩充游戏的功能呢?
  5. 这个 gold_room 游戏使用了奇怪的方式让你键入一个数字。这种方式会导致什么样的bug?你可以用比检查 0、1更好的方式判断输入是否是数字吗? to_i() 这个函式可以给你一些头绪。

还没有评论.