Skip to content

Commit 7697607

Browse files
committed
dragon program completed
1 parent 161b003 commit 7697607

File tree

1 file changed

+138
-0
lines changed
  • chapter_13_creating_new_classes_changing_existing_ones

1 file changed

+138
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
class Dragon
2+
def initialize(name)
3+
@name = name
4+
@asleep = false
5+
@stuff_in_belly = 10
6+
@stuff_in_intestines = 0
7+
8+
puts "#{@name} is born...."
9+
end
10+
11+
def feed
12+
puts "You feed #{@name}"
13+
@stuff_in_belly = 10
14+
passage_of_time
15+
end
16+
17+
def walk
18+
puts "You walk #{@name}"
19+
@stuff_in_intenstines = 0
20+
passage_of_time
21+
end
22+
23+
def put_to_bed
24+
puts "You put #{@name} to bed."
25+
@asleep = true
26+
27+
3.times do
28+
if @asleep
29+
passage_of_time
30+
end
31+
32+
if @alseep
33+
puts "#{@name} snores, filling the room withs smoke."
34+
end
35+
end
36+
37+
if @asleep
38+
@alseep = false
39+
puts "#{@name} wakes up slowly."
40+
end
41+
end
42+
43+
def toss
44+
puts "You toss #{@name} into the air."
45+
puts "He giggles, which singes your eyebrows."
46+
passage_of_time
47+
end
48+
49+
def rock
50+
puts "You rock #{@name} gently."
51+
@alseep = true
52+
puts "But he briefly doeses off."
53+
passage_of_time
54+
55+
if @asleep
56+
@asleep = false
57+
puts "...but wakes up when you stop."
58+
end
59+
end
60+
61+
private
62+
def hungry?
63+
@stuff_in_belly <= 2
64+
end
65+
66+
def poopy?
67+
@stuff_in_intestines >= 8
68+
end
69+
70+
def passage_of_time
71+
if @stuff_in_belly > 0
72+
@stuff_in_belly -= 1
73+
@stuff_in_intestines += 1
74+
else
75+
if @asleep
76+
@asleep = false
77+
puts "He wakes up suddenly!"
78+
end
79+
80+
puts "#{@name} is starving. In desperation, he eats you."
81+
exit
82+
end
83+
84+
if @stuff_in_intestines >= 10
85+
@stuff_in_intestines = 0
86+
puts "Whoops! #{@name} had an accident...."
87+
end
88+
89+
if hungry?
90+
if @alseep
91+
@asleep = false
92+
puts "He wakes up suddenly!"
93+
end
94+
puts "#{@name}'s stomach grumbles..."
95+
end
96+
97+
if poopy?
98+
if @asleep
99+
@asleep = false
100+
puts "He wakes up suddenly!"
101+
end
102+
103+
puts "#{@name} does the potty dance."
104+
end
105+
end
106+
end
107+
108+
print "\nPlease enter a name for your dragon >> "
109+
dragon_name = gets.chomp.capitalize
110+
puts
111+
pet = Dragon.new(dragon_name)
112+
113+
print "\nYou now have to take care of your new pet. Or you can stop by typing \"STOP\".\n"
114+
115+
loop do
116+
puts
117+
puts "You can FEED, WALK, TOSS, PUT_TO_BED, and ROCK"
118+
puts
119+
print "What would you like to do with Norbert? >> "
120+
input = gets.chomp.upcase
121+
122+
case input
123+
when "FEED"
124+
pet.feed
125+
when "WALK"
126+
pet.walk
127+
when "TOSS"
128+
pet.toss
129+
when "PUT_TO_BED"
130+
pet.put_to_bed
131+
when "ROCK"
132+
pet.rock
133+
when "STOP"
134+
break
135+
else
136+
puts "Norbert can't do that. Try again, or input STOP to stop the game"
137+
end
138+
end

0 commit comments

Comments
 (0)