-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathsfii.rb
357 lines (314 loc) · 7.32 KB
/
sfii.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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
#please excuse the CamelCase
class Move
def forward()
return false
end
def backward()
return false
end
def up()
return false
end
def down()
return false
end
def downBackward()
return false
end
def downForward()
return false
end
def upBackward()
return false
end
def upForward()
return false
end
end
class Up < Move
def up()
return true
end
end
class Down < Move
def down()
return true
end
end
class Forward < Move
def forward()
return true
end
end
class Back < Move
def backward()
return true
end
end
class Backward < Back
end
class DownBackward < Move
def downBackward()
return true
end
end
class DownForward < Move
def downForward()
return true
end
end
class UpBack < Move
def upBackward()
return true
end
end
class UpBackward < UpBack
end
class UpForward < Move
def upForward()
return true
end
end
class Action
def kick()
return false
end
def punch()
return false
end
end
class Kick < Action
def kick()
return true
end
end
class Punch < Action
def punch()
return true
end
end
class None < Move
end
class FireballMoveConditional
def initialize()
@state = :none
end
def say()
return "HaDoKen!"
end
# this example has a state value as a guard
# but if I mess up the conditional then all
# hell breaks loose
def nextState( context, move, action )
if (@state == :none && move.down()) then
@state = :ha
elsif (@state == :ha && move.down()) then
@state = :ha
elsif (@state == :ha && move.downForward()) then
@state = :do
elsif (@state == :do && move.forward() && action.punch()) then
# the body of the state machine is put into
# these conditional blocks
context.execute(FireballMove.new())
@state = :none
else
# the general default behaviour is here
# but sometimes different states have default
# behaviour too
@state = :none
end
return self
end
end
class MockContext
def execute(v)
puts(v)
end
end
class FireballMove
def say()
return "HaDoKen!"
end
def nextState( context, move, action )
if (move.down()) then
return FireballHa.new()
end
return FireballMove.new()
end
end
class FireballHa
def nextState( context, move, action )
if (move.downForward()) then
return FireballDo.new()
elsif (move.down()) then
return FireballHa.new()
end
return FireballMove.new()
end
end
class FireballDo
def nextState( context, move, action )
if (move.forward() && action.punch()) then
context.execute(FireballMove.new())
end
return FireballMove.new()
end
end
class DragonUppercutMove
def say()
return "ShoRyuKen!"
end
def nextState( context, move, action )
if (move.forward()) then
return DragonUppercutSho.new()
end
return DragonUppercutMove.new()
end
end
class DragonUppercutSho
def nextState( context, move, action )
if (move.downBackward()) then
return DragonUppercutRyu.new()
end
return DragonUppercutMove.new()
end
end
class DragonUppercutRyu
def nextState( context, move, action )
if (move.forward() && action.punch()) then
context.execute(DragonUppercutMove.new())
end
return DragonUppercutMove.new()
end
end
class WhirlwindKickMove
def say()
return "TaTsunaki!"
end
def nextState( context, move, action )
if (move.up()) then
return WhirlwindKickTa.new()
end
return WhirlwindKickMove.new()
end
end
class WhirlwindKickTa
def nextState( context, move, action )
if (move.down()) then
return WhirlwindKickTsun.new()
end
return WhirlwindKickMove.new()
end
end
class WhirlwindKickTsun
def nextState( context, move, action )
if (move.downBackward()) then
return WhirlwindKickAki.new()
end
return WhirlwindKickMove.new()
end
end
class WhirlwindKickAki
def nextState( context, move, action )
if (move.backward() && action.kick()) then
context.execute(WhirlwindKickMove.new())
end
return WhirlwindKickMove.new()
end
end
class RyuPlayer
def initialize()
@moves = [WhirlwindKickMove.new(), FireballMove.new(), DragonUppercutMove.new()]
@todo = []
end
def execute( move )
@todo = [ move ]
end
def move( move, action )
@todo = []
@moves = @moves.map {|specialMove| specialMove.nextState( self, move, action ) }
# @moves.each {|x| puts("\t" + x.class.name) }
executeMove()
end
def executeMove()
if (@todo.length > 0)
puts(@todo[0].say())
end
end
end
class RyuApp
def trySomeMoves()
player = RyuPlayer.new()
puts("# walking")
player.move( Forward.new(), Action.new() )
player.move( Forward.new(), Action.new() )
puts("# start fireball")
player.move( Down.new(), Action.new() )
player.move( DownForward.new(), Action.new() )
player.move( Forward.new(), Punch.new() ) # fireball
puts("# start fireball")
player.move( Down.new(), Action.new() )
player.move( DownForward.new(), Action.new() )
player.move( Forward.new(), Punch.new() ) # fireball
puts("# start fireball")
player.move( Down.new(), Action.new() )
player.move( DownForward.new(), Action.new() )
player.move( Forward.new(), Punch.new() ) # fireball
puts("# crouching")
player.move( Down.new(), Action.new() )
player.move( Down.new(), Action.new() )
player.move( Down.new(), Action.new() )
puts("# starting fireball")
player.move( Down.new(), Action.new() )
player.move( DownForward.new(), Action.new() )
player.move( Forward.new(), Punch.new() ) #fireball
puts("# converting to dragon uppercut")
player.move( DownBackward.new(), Action.new() )
player.move( Forward.new(), Punch.new() ) #DragonUppercut
puts("# charging whirlwind kick")
player.move( Up.new(), Action.new() )
player.move( Down.new(), Action.new() )
player.move( DownBackward.new(), Action.new() )
player.move( Backward.new(), Kick.new() ) #whirlwind
player.move( Forward.new(), Punch.new() )
end
def genKeymap()
keymap = {
"\e[C"=> [ Forward.new(), Action.new()],
"\e[D"=> [ Backward.new(), Action.new()],
"\e[B"=> [ Down.new(), Action.new()],
"\e[A"=> [ Up.new(), Action.new()],
"\e[C\e[B"=> [ DownForward.new(), Action.new()],
"\e[D\e[B"=> [ DownBackward.new(), Action.new()],
"\e[B\e[C"=> [ DownForward.new(), Action.new()],
"\e[B\e[D"=> [ DownBackward.new(), Action.new()],
"\e[C\e[A"=> [ UpForward.new(), Action.new()],
"\e[D\e[a"=> [ UpBackward.new(), Action.new()],
"\e[A\e[C"=> [ UpForward.new(), Action.new()],
"\e[A\e[D"=> [ UpBackward.new(), Action.new()],
"" => [ None.new(), Action.new() ]
}
keys = keymap.keys
keys.each { |key| keymap[key + 'x'] = [ keymap[key][0] , Kick.new()] }
keys.each { |key| keymap[key + 'z'] = [ keymap[key][0], Punch.new()] }
return keymap
end
def initialize()
@keymap = genKeymap()
@player = RyuPlayer.new()
end
def get_action()
end
def run()
while( keys = STDIN.readline().chomp() )
act = @keymap[keys]
if (act) then
puts(act.map {|x| "\t"+x.class.name })
@player.move( *act )
end
end
end
end
app = RyuApp.new()
app.trySomeMoves()
app.run()