-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path13_5.rb
More file actions
121 lines (104 loc) · 2.37 KB
/
13_5.rb
File metadata and controls
121 lines (104 loc) · 2.37 KB
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
class Dragon
def initialize name
@name = name
@asleep = false
@stuff_in_belly = 10 # He is full
@stuff_in_intestine = 0 # He doesn't need to go
puts "#{@name} is born."
end
def feed
puts "You feed #{@name}"
@stuff_in_belly = 10
passage_of_time
end
def walk
puts "You walk #{@name}"
@stuff_in_intestine = 0
passage_of_time
end
def put_to_bed
puts "You put #{@name} to bed."
@asleep = true
3.times do
if @asleep
passage_of_time
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.'
passage_of_time
end
def rock
puts "You rock #{@name} gently."
@asleep = true
puts 'He briefly dozes off...'
passage_of_time
if @asleep
@asleep = false
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 whether he's hungry.)
def hungry?
# Method names can end with a "?".
# Usually we do this only if the method
# returns true or false like below.
@stuff_in_belly <= 2
end
def poopy?
@stuff_in_intestine >= 8
end
def passage_of_time
if @stuff_in_belly > 0
# Move food from belly to intestine
@stuff_in_belly = @stuff_in_belly - 1
@stuff_in_intestine = @stuff_in_intestine + 1
else # Our dragon is starving!
if @asleep
@asleep = false
puts 'He wakes up suddenly!'
end
puts "#{@name} is starving! In desparation, he ate YOU!"
exit # This quits the program
end
if @stuff_in_intestine >= 10
@stuff_in_intestine = 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
pet = Dragon.new 'Hitch'
pet.feed
pet.toss
pet.walk
pet.put_to_bed
pet.rock
pet.put_to_bed
pet.put_to_bed
pet.put_to_bed
pet.put_to_bed