-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchap_09_e.rb
148 lines (123 loc) · 3.12 KB
/
chap_09_e.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
=begin
Write a program so that you can interact with your baby dragon. You should be
able to enter commands like feed and walk, and have those methods be called on
your dragon. Of course, since what you are inputting are just strings, you will
have to have some sort of method dispatch, where your program checks which
string was entered, and then calls the appropriate method.
=end
class Dragon
def initialize name
@name = name
@asleep = false
@stuffInBelly = 10 # He's full.
@stuffInIntestine = 0 # He doesn't need to go.
puts @name + ' is born.'
end
def feed
puts 'You feed ' + @name + '.'
@stuffInBelly = 10
passageOfTime
end
def walk
puts 'You walk ' + @name + '.'
@stuffInIntestine = 0
passageOfTime
end
def putToBed
puts 'You put ' + @name + ' to bed.'
@asleep = true
3.times do
if @asleep
passageOfTime
end
if @asleep
puts @name + ' snores, filling the room with smoke.'
end
end
if @asleep
@asleep = false
puts @name + ' wakes up slowly.'
end
end
def toss
puts 'You toss ' + @name + ' up into the air.'
puts 'He giggles, which singes your eyebrows.'
passageOfTime
end
def rock
puts 'You rock ' + @name + ' gently.'
@asleep = true
puts 'He briefly dozes off...'
passageOfTime
if @asleep
@asleep = false
puts '...but wakes when you stop.'
end
end
private
# "private" means that the methods defined here are
# methods internal to the object. (You can feed
# your dragon, but you can't ask him if he's hungry.)
def hungry?
# Method names can end with "?".
# Usually, we only do this if the method
# returns true or false, like this:
@stuffInBelly <= 2
end
def poopy?
@stuffInIntestine >= 8
end
def passageOfTime
if @stuffInBelly > 0
# Move food from belly to intestine.
@stuffInBelly = @stuffInBelly - 1
@stuffInIntestine = @stuffInIntestine + 1
else # Our dragon is starving!
if @asleep
@asleep = false
puts 'He wakes up suddenly!'
end
puts @name + ' is starving! In desperation, he ate YOU!'
exit # This quits the program.
end
if @stuffInIntestine >= 10
@stuffInIntestine = 0
puts 'Whoops! ' + @name + ' had an accident...'
end
if hungry?
if @asleep
@asleep = false
puts 'He wakes up suddenly!'
end
puts @name + '\'s stomach grumbles...'
end
if poopy?
if @asleep
@asleep = false
puts 'He wakes up suddenly!'
end
puts @name + ' does the potty dance...'
end
end
end
puts "Please name your dragon:"
name = gets.chomp
my_dragon = Dragon.new name
game_over = 0
while game_over == 0
puts "Please enter a command:"
command = gets.chomp.downcase
if command == "feed"
my_dragon.feed
elsif command == "walk"
my_dragon.walk
elsif command == "put to bed"
my_dragon.putToBed
elsif command == "toss"
my_dragon.toss
elsif command == "rock"
my_dragon.rock
else
puts "That is not a valid dragon command!"
end
end