Guest User

Untitled

a guest
Jun 23rd, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.89 KB | None | 0 0
  1. #!/usr/bin/ruby
  2.  
  3.  
  4. class Dragon
  5.  
  6. def initialize name
  7. @name = name
  8. @asleep = false
  9. @stuffInBelly = 10 # He's full.
  10. @stuffInIntestine = 0 # He doesn't need to go.
  11.  
  12. puts @name + ' is born.'
  13. end
  14.  
  15. def feed
  16. puts 'You feed ' + @name + '.'
  17. @stuffInBelly = 10
  18. passageOfTime
  19. end
  20.  
  21. def walk
  22. puts 'You walk ' + @name + '.'
  23. @stuffInIntestine = 0
  24. passageOfTime
  25. end
  26.  
  27. def putToBed
  28. puts 'You put ' + @name + ' to bed.'
  29. @asleep = true
  30. 3.times do
  31. if @asleep
  32. passageOfTime
  33. end
  34. if @asleep
  35. puts @name + ' snores, filling the room with smoke.'
  36. end
  37. end
  38. if @asleep
  39. @asleep = false
  40. puts @name + ' wakes up slowly.'
  41. end
  42. end
  43.  
  44. def toss
  45. puts 'You toss ' + @name + ' up into the air.'
  46. puts 'He giggles, which singes your eyebrows.'
  47. passageOfTime
  48. end
  49.  
  50. def rock
  51. puts 'You rock ' + @name + ' gently.'
  52. @asleep = true
  53. puts 'He briefly dozes off...'
  54. passageOfTime
  55. if @asleep
  56. @asleep = false
  57. puts '...but wakes when you stop.'
  58. end
  59. end
  60.  
  61. private
  62.  
  63. # "private" means that the methods defined here are
  64. # methods internal to the object. (You can feed
  65. # your dragon, but you can't ask him if he's hungry.)
  66.  
  67. def hungry?
  68. # Method names can end with "?".
  69. # Usually, we only do this if the method
  70. # returns true or false, like this:
  71. @stuffInBelly <= 2
  72. end
  73.  
  74. def poopy?
  75. @stuffInIntestine >= 8
  76. end
  77.  
  78. def passageOfTime
  79. if @stuffInBelly > 0
  80. # Move food from belly to intestine.
  81. @stuffInBelly = @stuffInBelly - 1
  82. @stuffInIntestine = @stuffInIntestine + 1
  83. else # Our dragon is starving!
  84. if @asleep
  85. @asleep = false
  86. puts 'He wakes up suddenly!'
  87. end
  88. puts @name + ' is starving! In desperation, he ate YOU!'
  89. exit # This quits the program.
  90. end
  91.  
  92. if @stuffInIntestine >= 10
  93. @stuffInIntestine = 0
  94. puts 'Whoops! ' + @name + ' had an accident...'
  95. end
  96.  
  97. if hungry?
  98. if @asleep
  99. @asleep = false
  100. puts 'He wakes up suddenly!'
  101. end
  102. puts @name + '\'s stomach grumbles...'
  103. end
  104.  
  105. if poopy?
  106. if @asleep
  107. @asleep = false
  108. puts 'He wakes up suddenly!'
  109. end
  110. puts @name + ' does the potty dance...'
  111. end
  112. end
  113.  
  114. end
  115.  
  116. pet = Dragon.new 'Norbert'
  117.  
  118. while not @stuffInBelly == 0
  119. a = gets.chomp.downcase
  120. if a == 'walk'
  121. pet.walk
  122. elsif a == 'toss'
  123. pet.toss
  124. elsif a == 'rock'
  125. pet.rock
  126. elsif a == 'putToBed'
  127. pet.putToBed
  128. elsif a == 'feed'
  129. pet.feed
  130. elsif a == 'sleep'
  131. pet.putToBed
  132. elsif a == '?'
  133. puts 'available commands are as follows: walk,toss,rock,sleep,feed,quit'
  134. elsif a == 'quit'
  135. exit
  136. end
  137.  
  138. end
Add Comment
Please, Sign In to add comment