-
Notifications
You must be signed in to change notification settings - Fork 1
/
Player001Turing.oz
599 lines (562 loc) · 19.3 KB
/
Player001Turing.oz
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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
functor
import
Input
Browser
Projet2019util
OS
System
export
portPlayer:StartPlayer
define
% Main functions.
StartPlayer
TreatStream
% ?
Name = 'debug001turing'
% Handler functions.
GetId
GetState
AssignSpawn
SpawnF
DoAction
Add
GotHit
Info
% Helper functions.
UpdateMap
UpdateBombs
AddValMap
ZeroMap
IsGoodMap
UpdateDMap
DangerMap
BestMove
RemoveBad
NextToBox
Value
BFS
RandomNeighbour
RemoveSeen
BFSNeighbours
% Global variables
GoodValues = [0 4 5 6]
in
/**
* Initializes the player and launches it.
*
* @param ID: current player's <bomber> ID.
*/
fun {StartPlayer ID}
Stream Port OutputStream
in
thread % Filter to test validity of message sent to the player.
OutputStream = {Projet2019util.portPlayerChecker Name ID Stream}
end % thread
{NewPort Stream Port}
thread
% The player is initially off the board and has no spawn position, until (assign)spawn.
{TreatStream OutputStream summary(state:off id:ID lives:Input.nbLives sppos:nil pos:nil bombs:Input.nbBombs map:Input.map score:0 bomblist:nil)}
end % thread
Port
end % fun StartPlayer
/**
* Update the list of bombs when a bomb has exploded.
*
* @param BombList: list of bombs.
*/
fun {UpdateBombs BombList Pos}
fun {DoUpdateBombs BombList Acc}
case BombList
of nil then
Acc
[] bomb(pos:P)|T then
if P == Pos then
{Append Acc T}
else
{DoUpdateBombs T {Append Acc [bomb(pos:P)]}}
end % if
else
raise('UpdateBombs : pattern not recognized :'#BombList) end
end
end % fun DoUpdateBombs
in
{DoUpdateBombs BombList nil}
end % fun UpdateBombs
/**
* Change a specified tile's value.
*
* @param Map: the map of the game.
* @param X: the x value of the tile.
* @param Y: the y value of the tile.
* @param Value: the new value of the tile.
*/
fun {UpdateMap Map X Y Value}
fun {NewRow Row X ThisX}
case Row
of H|T then
if X == ThisX then
Value|T
else
H|{NewRow T X ThisX+1}
end % if
else
raise('Error in NewRow function: Row != H|T.') end
end % case Row
end % fun NewRow
fun {NewColumns Map X Y ThisY}
case Map
of H|T then
if ThisY == Y then
{NewRow H X 1}|T
else
H|{NewColumns T X Y ThisY+1}
end % if
else
raise('Error in NewColumns function: Row != H|T.') end
% raise('Error in NewColumns function: Map != H|T in TerenceTao.') end
end % case Map
end % fun NewColumns
in
% Works if 1 < X, Y < N, 1 and N being the borders.
if X < 1 orelse Y < 1 orelse Y > Input.nbRow orelse X > Input.nbColumn then
raise('Assertion error in UpdateMap function') end
end % if
{NewColumns Map X Y 1}
end % fun UpdateMap
/**
* Bind the ID of the player.
*
* @param Summary: summary of the game.
* @param ?RetID: unbound, set to ID by function.
*/
fun {GetId Summary RetID}
RetID = Summary.id
Summary
end % fun GetId
/**
* Bind the ID and State of the player.
*
* @param Summary: summary of the game.
* @param ?RetID: unbound, set to ID by function.
* @param ?RetState: unbound, set to State by function.
*/
fun {GetState Summary RetID RetState}
RetID = Summary.id
RetState = Summary.state
Summary
end % fun GetState
/**
* Assign a spawn position.
*
* @param Summary: summary of the game.
* @param SpPos: Spawn position of the current player.
*/
fun {AssignSpawn Summary SpPos}
{AdjoinList Summary [sppos#SpPos]}
end % fun AssignSpawn
/**
* Spawn the player in its assigned position if possible, i.e.:
* - the player has lives left;
* - the player is not already on the board.
* If any of these conditions are not met, then null is returned for both the ID and Pos.
*
* @param Summary: summary of the game.
* @param ?RetID: unbound, set to ID (or null) by function.
* @param ?RetPos: unbound, set to SpawnPos (or null) by function.
*/
fun {SpawnF Summary RetID RetSpawn}
if Summary.state == on then
RetID = null
RetSpawn = null
{System.show 'Tried spawning a player that was already on the board in SpawnF.'}
Summary
elseif Summary.lives =< 0 then
RetID = null
RetSpawn = null
{Summary.show 'No more lives left in SpawnF.'}
Summary
else
RetID = Summary.id
RetSpawn = Summary.sppos
{AdjoinList Summary [state#on pos#Summary.sppos]}
end % if
end % fun SpawnF
fun {AddValMap Map X Y Val}
{UpdateMap Map X Y Val+{Nth {Nth Map Y} X}}
end % fun AddValMap
fun {ZeroMap Map}
fun {DoZeroMap Map X Y}
if X > Input.nbColumn then
if Y == Input.nbRow then
Map
else
{DoZeroMap Map 1 Y+1}
end % if
else
{DoZeroMap {UpdateMap Map X Y 0} X+1 Y}
end % if
end % fun DoZeroMap
in
{DoZeroMap Map 1 1}
end % fun ZeroMap
fun {IsGoodMap Val}
{List.member Val GoodValues}
end % fun IsGoodMap
fun {UpdateDMap Map DMap X Y Dist Dir}
if X =< 1 orelse Y =< 1 orelse X >= Input.nbColumn orelse Y >= Input.nbRow orelse Dist > Input.fire orelse {Not {IsGoodMap {Nth {Nth Map Y} X}}} then
DMap
else
% We're either already checking in a given direction, in which case we continue,
% or we're still on the bomb's position, in which case we have to explore all directions.
if Dir == up then
{UpdateDMap Map {AddValMap DMap X Y Input.fire-Dist} X Y-1 Dist+1 up} % Moving up.
elseif Dir == down then
{UpdateDMap Map {AddValMap DMap X Y Input.fire-Dist} X Y+1 Dist+1 down} % Moving down.
elseif Dir == left then
{UpdateDMap Map {AddValMap DMap X Y Input.fire-Dist} X-1 Y Dist+1 left} % Moving left.
elseif Dir == right then
{UpdateDMap Map {AddValMap DMap X Y Input.fire-Dist} X+1 Y Dist+1 right} % Moving right.
else DMap1 DMap2 DMap3 in
DMap1 = {UpdateDMap Map {AddValMap DMap X Y Input.fire} X Y-1 Dist+1 up} % First branch.
DMap2 = {UpdateDMap Map DMap1 X Y+1 Dist+1 down} % Second branch.
DMap3 = {UpdateDMap Map DMap2 X-1 Y Dist+1 left} % Third branch.
{UpdateDMap Map DMap3 X+1 Y Dist+1 right} % Final branch.
end % if
end % if
end % fun UpdateDMap
fun {DangerMap Summary}
fun {DoDangerMap Map BombList}
case BombList
of nil then
Map
[] bomb(pos:pt(x:X y:Y))|Xr then
{DoDangerMap {UpdateDMap Summary.map Map X Y 0 all} Xr}
end % case BombList
end % fun DoDangerMap
in
{DoDangerMap {ZeroMap Summary.map} Summary.bomblist}
end % fun DangerMap
fun {BestMove List BFSRes}
fun {DoBestMove List Best BFSRes}
case List
of nil then
Best
[] rec(x:X y:Y val:Val)|T then
if Val < Best.val orelse (Val == Best.val andthen X == BFSRes.x andthen Y == BFSRes.y) then
{DoBestMove T rec(x:X y:Y val:Val) BFSRes}
else
{DoBestMove T Best BFSRes}
end % if
end % case List
end % fun DoBestMove
in
{DoBestMove List List.1 BFSRes}
end % fun BestMove
fun {RemoveBad L Map}
fun {DoRemoveBad L Map Acc}
case L
of nil then
Acc
[] rec(x:X y:Y val:Val)|T then
if {IsGoodMap {Nth {Nth Map Y} X}} then
{DoRemoveBad T Map {Append Acc [rec(x:X y:Y val:Val)]}}
else
{DoRemoveBad T Map Acc}
end % if
end % case L
end % fun DoRemoveBad
in
{DoRemoveBad L Map nil}
end % fun RemoveBad
fun {NextToBox PosList ValList}
case PosList
of nil then
false
[] rec(x:X y:Y val:Val)|T then
if {List.member Val ValList} then
true
else
{NextToBox T ValList}
end % if
end % case PosList
end % fun NextToBox
fun {Value Map X Y}
{Nth {Nth Map Y} X}
end % fun Value
fun {RemoveSeen L Seen}
fun {DoRemoveSeen L Seen Acc}
case L
of nil then
Acc
[] node(x:X y:Y dist:D)|T then
if {Value Seen X Y} == 1 then
{DoRemoveSeen T Seen Acc}
else
{DoRemoveSeen T Seen {Append Acc [node(x:X y:Y dist:D)]}}
end % if
end % case L
end % fun DoRemoveSeen
in
{DoRemoveSeen L Seen nil}
end % fun RemoveSeen
fun {BFS Map Start Target}
fun {DoBFS Map Start Queue Seen Target}
case Queue
of nil then
{System.show 'notarget'}
{System.show Start}
notarget
[] node(x:X y:Y dist:D)|T then
if X =< 1 orelse X >= Input.nbColumn orelse Y =< 1 orelse Y >= Input.nbRow orelse {Not {IsGoodMap {Value Map X Y}}} then
{DoBFS Map Start T {UpdateMap Seen X Y 1} Target}
elseif {Value Map X Y} == Target then
D
else
{DoBFS Map Start {Append T {RemoveSeen [node(x:X+1 y:Y dist:D+1) node(x:X y:Y+1 dist:D+1) node(x:X y:Y-1 dist:D+1) node(x:X-1 y:Y dist:D+1)] Seen}} {UpdateMap Seen X Y 1} Target}
end % if
end % case Queue
end % fun DoBFS
in
{DoBFS Map Start [node(x:Start.x y:Start.y dist:0)] {UpdateMap {ZeroMap Map} Start.x Start.y 1} Target}
end % fun BFS
fun {RandomNeighbour X Y}
local Rand1 in
Rand1 = {OS.rand} mod 4
case Rand1
of 0 then pt(x:X+1 y:Y)
[] 1 then pt(x:X-1 y:Y)
[] 2 then pt(x:X y:Y+1)
[] 3 then pt(x:X y:Y-1)
end % case Rand1
end % local
end % fun RandomNeighbour
fun {BFSNeighbours Map X Y Target Val}
if {BFS Map pt(x:X y:Y-1) Target} == Val - 1 then
pt(x:X y:Y-1)
elseif {BFS Map pt(x:X y:Y+1) Target} == Val - 1 then
pt(x:X y:Y+1)
elseif {BFS Map pt(x:X-1 y:Y) Target} == Val - 1 then
pt(x:X-1 y:Y)
else
pt(x:X+1 y:Y)
end % if
end % fun BFSNeighbours
/**
* Determine what action the player should perform.
*
* @param Summary: summary of the game.
* @param ?RetID: unbound, set to ID (or null) by function.
* @param ?RetAction: unbound, set to the action the player should perform (or null) by function.
*/
fun {DoAction Summary RetID RetAction}
if Summary.state == off then
RetID = null
RetAction = null
{System.show 'Off-board player tried to perform an action in DoAction.'}
Summary
elseif Summary.bombs =< 0 then
local DMap X Y Up Down Left Right Prio NewRec NewPos BFSPoint BFSBonus in
if Summary.pos \= nil then
RetID = Summary.id
X = Summary.pos.x
Y = Summary.pos.y
DMap = {DangerMap Summary}
Up = rec(x:X y:Y-1 val:{Nth {Nth DMap Y-1} X})
Down = rec(x:X y:Y+1 val:{Nth {Nth DMap Y+1} X})
Left = rec(x:X-1 y:Y val:{Nth {Nth DMap Y} X-1})
Right = rec(x:X+1 y:Y val:{Nth {Nth DMap Y} X+1})
Prio = {RemoveBad [Up Down Left Right] Summary.map}
BFSBonus = {BFS Summary.map pt(x:X y:Y) 6}
BFSPoint = {BFS Summary.map pt(x:X y:Y) 5}
if BFSBonus == notarget andthen BFSPoint == notarget then
NewRec = {BestMove Prio {RandomNeighbour X Y}}
elseif BFSBonus == notarget then
NewRec = {BestMove Prio {BFSNeighbours Summary.map X Y 5 BFSPoint}}
elseif BFSPoint == notarget then
NewRec = {BestMove Prio {BFSNeighbours Summary.map X Y 6 BFSBonus}}
elseif BFSBonus > 2 * BFSPoint then
NewRec = {BestMove Prio {BFSNeighbours Summary.map X Y 5 BFSPoint}}
else
NewRec = {BestMove Prio {BFSNeighbours Summary.map X Y 6 BFSBonus}}
end % if
NewPos = pt(x:NewRec.x y:NewRec.y)
RetAction = move(NewPos)
{AdjoinList Summary [pos#NewPos]}
else
RetID = null
RetAction = null
Summary
end % if
end % local
else DMap X Y UpM DownM LeftM RightM in
if Summary.pos \= nil then
RetID = Summary.id
X = Summary.pos.x
Y = Summary.pos.y
DMap = {DangerMap Summary}
UpM = rec(x:X y:Y-1 val:{Nth {Nth Summary.map Y-1} X})
DownM = rec(x:X y:Y+1 val:{Nth {Nth Summary.map Y+1} X})
LeftM = rec(x:X-1 y:Y val:{Nth {Nth Summary.map Y} X-1})
RightM = rec(x:X+1 y:Y val:{Nth {Nth Summary.map Y} X+1})
if {NextToBox [UpM DownM LeftM RightM] [2 3]} andthen {Not {List.member bomb(pos:pt(x:X y:Y)) Summary.bomblist}} then
RetID = Summary.id
RetAction = bomb(Summary.pos)
{AdjoinList Summary [bombs#Summary.bombs-1]}
else Up Down Left Right Prio NewRec NewPos BFSPoint BFSBonus in
Up = rec(x:X y:Y-1 val:{Nth {Nth DMap Y-1} X})
Down = rec(x:X y:Y+1 val:{Nth {Nth DMap Y+1} X})
Left = rec(x:X-1 y:Y val:{Nth {Nth DMap Y} X-1})
Right = rec(x:X+1 y:Y val:{Nth {Nth DMap Y} X+1})
Prio = {RemoveBad [Up Down Left Right] Summary.map}
BFSBonus = {BFS Summary.map pt(x:X y:Y) 6}
BFSPoint = {BFS Summary.map pt(x:X y:Y) 5}
if BFSBonus == notarget andthen BFSPoint == notarget then
NewRec = {BestMove Prio {RandomNeighbour X Y}}
elseif BFSBonus == notarget then
NewRec = {BestMove Prio {BFSNeighbours Summary.map X Y 5 BFSPoint}}
elseif BFSPoint == notarget then
NewRec = {BestMove Prio {BFSNeighbours Summary.map X Y 6 BFSBonus}}
elseif BFSBonus > 3 * BFSPoint then
NewRec = {BestMove Prio {BFSNeighbours Summary.map X Y 5 BFSPoint}}
else
NewRec = {BestMove Prio {BFSNeighbours Summary.map X Y 6 BFSBonus}}
end % if
NewPos = pt(x:NewRec.x y:NewRec.y)
RetAction = move(NewPos)
{AdjoinList Summary [pos#NewPos]}
end % if
else
RetID = null
RetAction = null
Summary
end % if
end % if
end % fun DoAction
/**
* Add an item to the player.
*
* @param Summary: summary of the game.
* @param Type: the type of the item.
* @param Option: the value of the item.
* @param ?RetResult: unbound, new value of the counter.
*/
fun {Add Summary Type Option ?RetResult}
if Summary.state == off then
RetResult = Summary.score
{System.show 'Tried adding item to off-board player in Add.'}
Summary
else
case Type
of bomb then
RetResult = Summary.bombs + Option
{AdjoinList Summary [bombs#RetResult]}
[] point then
RetResult = Summary.score+Option
{AdjoinList Summary [score#RetResult]}
[] life then
RetResult = Summary.lives+Option
{AdjoinList Summary [score#Summary.lives+Option]}
else
RetResult = 69
raise('Unknown type in Add.') end
Summary
end % case Type
end % if
end % fun Add
/**
* Handle getting hit by fire.
*
* @param Summary: summary of the game.
* @param ?RetID: unbound, <bomber> ID of the player.
* @param ?RetResult: unbound, result of getting hit.
*/
fun {GotHit Summary RetID RetResult}
if Summary.state == off then
RetID = null
RetResult = null
{System.show 'Off-board player received gotHit message in GotHit.'}
Summary
elseif Summary.lives =< 0 then
RetID = null
RetResult = null
{System.show 'Dead player received gotHit message in GotHit.'}
Summary
else NewLives in
RetID = Summary.id
NewLives = Summary.lives - 1
RetResult = death(NewLives)
{AdjoinList Summary [state#off lives#NewLives]}
end % if
end % fun GotHit
/**
* Handle informational messages.
*
* @param Summary: summary of the game.
* @param Message: informational message being handled.
*/
fun {Info Summary Message}
case Message
of spawnPlayer(ID SPPos) then
Summary
[] movePlayer(ID MPPos) then
if {List.member {Nth {Nth Summary.map MPPos.y} MPPos.x} [5 6]} then
{AdjoinList Summary [map#{UpdateMap Summary.map MPPos.x MPPos.y 0}]}
else
Summary
end % if
[] deadPlayer(ID) then
Summary
[] bombPlanted(BPPos) then
local NewBombList in
NewBombList = {Append Summary.bomblist [bomb(pos:BPPos)]}
{AdjoinList Summary [bomblist#NewBombList]}
end % local
[] bombExploded(BEPos) then
{AdjoinList Summary [bomblist#{UpdateBombs Summary.bomblist BEPos}]}
[] boxRemoved(BRPos) then
local NewMap in
case {Nth {Nth Summary.map BRPos.y} BRPos.x}
of 2 then NewMap = {UpdateMap Summary.map BRPos.x BRPos.y 5} % Tile with point.
[] 3 then NewMap = {UpdateMap Summary.map BRPos.x BRPos.y 6} % Tile with bonus.
end % case {Nth ...}
{AdjoinList Summary [map#NewMap]}
end % local
end % case Message
end % fun Info
/**
* Read the current stream to determine actions.
*
* @param Stream: input stream of bomber messages.
* @param Summary: summary of the game.
*/
proc {TreatStream Stream Summary}
case Stream
of nil then skip
[] getId(RetID)|S then
{TreatStream S {GetId Summary RetID}}
[] getState(RetID RetState)|S then
{TreatStream S {GetState Summary RetID RetState}}
[] assignSpawn(SpPos)|S then
{TreatStream S {AssignSpawn Summary SpPos}}
[] spawn(RetID RetPos)|S then
{TreatStream S {SpawnF Summary RetID RetPos}}
[] doaction(RetID RetAction)|S then
if Input.isTurnByTurn then
{TreatStream S {DoAction Summary RetID RetAction}}
else
thread
{Delay {OS.rand} mod (Input.thinkMax - Input.thinkMin + 1) + Input.thinkMin}
{TreatStream S {DoAction Summary RetID RetAction}}
end
end
[] add(Type Option RetResult)|S then
{TreatStream S {Add Summary Type Option RetResult}}
[] gotHit(RetID RetResult)|S then
{TreatStream S {GotHit Summary RetID RetResult}}
[] info(Message)|S then
{TreatStream S {Info Summary Message}}
else
skip
end % case Stream
end % proc TreatStream
end % functor