-
Notifications
You must be signed in to change notification settings - Fork 10
/
smart_ui.rb
296 lines (229 loc) · 4.96 KB
/
smart_ui.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
# -*- coding: utf-8 -*-
class Living
attr :hp, true
attr :max_hp, true
attr :name, true
attr :attack_power, true
end
class Enemy < Living
attr :exp, true
end
class Slime < Enemy
def initialize
self.name = "SLIME"
self.max_hp = 10
self.hp = self.max_hp
self.attack_power = 4
self.exp = 1
end
end
class Dragon < Enemy
def initialize
self.name = "DRAGON"
self.max_hp = 20
self.hp = self.max_hp
self.attack_power = 8
self.exp = 3000
end
end
class Player < Living
def initialize
self.name = "PLAYER"
self.max_hp = 10
self.hp = self.max_hp
self.attack_power = 3
end
end
class Battle
def initialize
@enemy_classes = [Slime, Dragon]
@player = Player.new
@turn_count = 0
end
def finish_battle
puts "#{@enemy.name}をたおした"
puts
puts "==========================="
puts "かかったターン数#{@turn_count}"
puts "経験値#{@enemy.exp}かくとく"
gets
end
def check_enemy_hp
if @enemy.hp <= 0
@enemy.hp = 0
finish_battle()
else
enemy_attack()
end
end
def game_over
puts "#{@player.name}はたおれました"
puts
puts "==========================="
puts "ゲームオーバー"
gets
end
def check_player_hp
if @player.hp <= 0
@player.hp = 0
game_over()
else
query_command()
end
end
def enemy_attack
damage_point = rand(@enemy.attack_power) + 1
puts
puts "==========================="
puts "#{@enemy.name}のこうげき"
@player.hp -= damage_point
puts "#{@player.name}に#{damage_point}のダメージ"
gets
check_player_hp()
end
def player_attack
damage_point = rand(@player.attack_power) + 1
puts
puts "==========================="
puts "#{@player.name}のこうげき"
@enemy.hp -= damage_point
puts "#{@enemy.name}に#{damage_point}のダメージ"
gets
check_enemy_hp()
end
def player_hoimi
cure_point = rand(8) + 1
puts
puts "==========================="
puts "#{@player.name}はホイミをとなえた"
@player.hp = [@player.hp + cure_point, @player.max_hp].min
puts "HPが#{cure_point}回復した"
gets
enemy_attack()
end
def query_command
@turn_count += 1
puts
puts "==========================="
puts "#{@player.name}のHP: #{@player.hp}"
puts " 1 たたかう"
puts " 2 ホイミ"
puts "コマンド? [1]"
command = gets.chomp
case command
when "ホイミ", "2"
player_hoimi()
else
player_attack()
end
end
def encounter
@enemy = @enemy_classes.sample.new
puts "==========================="
puts "#{@enemy.name}があらわれた"
gets
query_command()
end
def start
encounter()
end
end
class BattleEnglish
def initialize
@enemy_classes = [Slime, Dragon]
@player = Player.new
@turn_count = 0
end
def finish_battle
puts "#{@player.name} win!"
puts
puts "==========================="
puts "Turn count: #{@turn_count}"
puts "#{@player.name} get #{@enemy.exp} EXP"
gets
end
def check_enemy_hp
if @enemy.hp <= 0
@enemy.hp = 0
finish_battle()
else
enemy_attack()
end
end
def game_over
puts "#{@player.name} lose..."
puts
puts "==========================="
puts "GAME OVER"
gets
end
def check_player_hp
if @player.hp <= 0
@player.hp = 0
game_over()
else
query_command()
end
end
def enemy_attack
damage_point = rand(@enemy.attack_power) + 1
puts
puts "==========================="
puts "#{@enemy.name} attack."
@player.hp -= damage_point
puts "#{@player.name} damaged #{damage_point} point(s)."
gets
check_player_hp()
end
def player_attack
damage_point = rand(@player.attack_power) + 1
puts
puts "==========================="
puts "#{@player.name} attack"
@enemy.hp -= damage_point
puts "#{@enemy.name} damaged #{damage_point} point(s)."
gets
check_enemy_hp()
end
def player_hoimi
cure_point = rand(8) + 1
puts
puts "==========================="
puts "#{@player.name} call hoimi."
@player.hp = [@player.hp + cure_point, @player.max_hp].min
puts "#{@player.name} cured #{cure_point} point(s)"
gets
enemy_attack()
end
def query_command
@turn_count += 1
puts
puts "==========================="
puts "#{@player.name} HP: #{@player.hp}"
puts " 1 Attack"
puts " 2 Hoimi"
puts "Command? [1]"
command = gets.chomp
case command
when "Hoimi", "2"
player_hoimi()
else
player_attack()
end
end
def encounter
@enemy = @enemy_classes.sample.new
puts "==========================="
puts "#{@player.name} encountered a #{@enemy.name}."
gets
query_command()
end
def start
encounter()
end
end
if ARGV.shift == "english"
BattleEnglish.new.start
else
Battle.new.start
end