加载中...

习题 31: 做出决定


这本书的上半部分,你印出了一些东西,并且呼叫了函式,不过一切都是直线式进行的。你的脚本从最上面一行开始,一路运行到结束,但其中并没有决定程式流向的分支点。现在你已经学会了 ifelse和 elsif,你就可以开始建立包含条件判断的脚本了。

上一个脚本中你写了一系列的简单提问测试。这节的脚本中,你将需要向使用者提问,依据使用者的答案来做出决定。把脚本写下来,多多捣鼓一阵子,看看他的运作原理是什么。

  1. def prompt
  2. print "> "
  3. end
  4. puts "You enter a dark room with two doors. Do you go through door #1 or door #2?"
  5. prompt; door = gets.chomp
  6. if door == "1"
  7. puts "There's a giant bear here eating a cheese cake. What do you do?"
  8. puts "1\. Take the cake."
  9. puts "2\. Scream at the bear."
  10. prompt; bear = gets.chomp
  11. if bear == "1"
  12. puts "The bear eats your face off. Good job!"
  13. elsif bear == "2"
  14. puts "The bear eats your legs off. Good job!"
  15. else
  16. puts "Well, doing #{bear} is probably better. Bear runs away."
  17. end
  18. elsif door == "2"
  19. puts "You stare into the endless abyss at Cthuhlu's retina."
  20. puts "1\. Blueberries."
  21. puts "2\. Yellow jacket clothespins."
  22. puts "3\. Understanding revolvers yelling melodies."
  23. prompt; insanity = gets.chomp
  24. if insanity == "1" or insanity == "2"
  25. puts "Your body survives powered by a mind of jello. Good job!"
  26. else
  27. puts "The insanity rots your eyes into a pool of muck. Good job!"
  28. end
  29. else
  30. puts "You stumble around and fall on a knife and die. Good job!"
  31. end

这里的重点是你可以在 if 语句中内部再放一个 if 语句。这是一个很强大的功能,可以用来建立“巢状(nested)”的决定(decision)。

你需要理解 if 语句包含 if 语句的概念。做一下加分习题,这样你会确信自己真正理解了它们。

你应该看到的结果

我在玩一个小冒险游戏。我的水准不怎么样。

  1. $ ruby ex31.rb
  2. You enter a dark room with two doors. Do you go through door #1 or door #2?
  3. > 1
  4. There's a giant bear here eating a cheese cake. What do you do?
  5. 1\. Take the cake.
  6. 2\. Scream at the bear.
  7. > 2
  8. The bear eats your legs off. Good job!
  9. $ ruby ex31.rb
  10. You enter a dark room with two doors. Do you go through door #1 or door #2?
  11. > 1
  12. There's a giant bear here eating a cheese cake. What do you do?
  13. 1\. Take the cake.
  14. 2\. Scream at the bear.
  15. > 1
  16. The bear eats your face off. Good job!
  17. $ ruby ex31.rb
  18. You enter a dark room with two doors. Do you go through door #1 or door #2?
  19. > 2
  20. You stare into the endless abyss at Cthuhlu's retina.
  21. 1\. Blueberries.
  22. 2\. Yellow jacket clothespins.
  23. 3\. Understanding revolvers yelling melodies.
  24. > 1
  25. Your body survives powered by a mind of jello. Good job!
  26. $ ruby ex31.rb
  27. You enter a dark room with two doors. Do you go through door #1 or door #2?
  28. > 2
  29. You stare into the endless abyss at Cthuhlu's retina.
  30. 1\. Blueberries.
  31. 2\. Yellow jacket clothespins.
  32. 3\. Understanding revolvers yelling melodies.
  33. > 3
  34. The insanity rots your eyes into a pool of muck. Good job!
  35. $ ruby ex31.rb
  36. You enter a dark room with two doors. Do you go through door #1 or door #2?
  37. > stuff
  38. You stumble around and fall on a knife and die. Good job!
  39. $ ruby ex31.rb
  40. You enter a dark room with two doors. Do you go through door #1 or door #2?
  41. > 1
  42. There's a giant bear here eating a cheese cake. What do you do?
  43. 1\. Take the cake.
  44. 2\. Scream at the bear.
  45. > apples
  46. Well, doing apples is probably better. Bear runs away.

加分习题

为游戏添加新的部分,改变玩家做决定的位置。尽自己能力扩充这个游戏,不过别把游戏弄得太诡异了。


还没有评论.