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
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
|
"use strict"
const data = require("./data.js")
const P1 = "First"
const P2 = "Second"
var states = {}
var game = null
var view = null
const POOL = -1
exports.scenarios = [
... data.scenarios.map(s => s.number + " - " + s.name)
]
exports.roles = [ P1, P2 ]
exports.action = function (state, player, action, arg) {
game = state
let S = states[game.state]
if (action in S)
S[action](arg, player)
else if (action === "undo" && game.undo && game.undo.length > 0)
pop_undo()
else
throw new Error("Invalid action: " + action)
return game
}
exports.view = function (state, player) {
game = state
view = {
log: game.log,
prompt: null,
scenario: game.scenario,
dice: game.dice,
sticks: game.sticks,
cubes: game.cubes,
morale: game.morale,
front: game.front,
reserve: game.reserve,
}
if (game.state === "game_over") {
view.prompt = game.victory
} else if (player !== game.active) {
let inactive = states[game.state].inactive || game.state
view.prompt = `Waiting for ${player_name()} to ${inactive}.`
} else {
view.actions = {}
states[game.state].prompt()
if (game.undo && game.undo.length > 0)
view.actions.undo = 1
else
view.actions.undo = 0
}
return view
}
exports.resign = function (state, player) {
game = state
if (game.state !== 'game_over') {
if (player === P1)
goto_game_over(P2, P1 + " resigned.")
if (player === P2)
goto_game_over(P1, P2 + " resigned.")
}
return game
}
function goto_game_over(result, victory) {
game.state = "game_over"
game.active = "None"
game.result = result
game.victory = victory
log("")
log(game.victory)
return false
}
states.game_over = {
prompt() {
view.prompt = game.victory
},
}
// === SETUP ===
exports.setup = function (seed, scenario, options) {
// TODO: "Random"
scenario = parseInt(scenario)
scenario = data.scenarios.findIndex(s => s.number === scenario)
if (scenario < 0)
throw Error("cannot find scenario: " + scenario)
let info = data.scenarios[scenario]
game = {
seed: seed,
scenario: scenario,
log: [],
undo: [],
active: P1,
state: "roll",
// dice value and position
dice: [
1, POOL, 1, POOL, 1, POOL, 1, POOL, 1, POOL, 1, POOL,
2, POOL, 2, POOL, 2, POOL, 2, POOL, 2, POOL, 2, POOL,
],
// sticks (map normal formation -> count)
sticks: [],
// cubes (map special formation -> count)
cubes: [],
morale: [ info.players[0].morale, info.players[1].morale ],
front: [ [], [], ],
reserve: [ [], [] ],
// dice value placed on what card
placed: [],
}
function setup_formation(front, reserve, c) {
let card = data.cards[c]
if (card.reserve)
reserve.push(c)
else
front.push(c)
if (card.special)
add_cubes(c, 1)
else
add_sticks(c, card.strength)
}
for (let p = 0; p < 2; ++p) {
for (let c of info.players[p].cards)
setup_formation(game.front[p], game.reserve[p], c)
}
log(".h1 " + info.name)
log(".h2 " + info.date)
log("")
goto_action_phase()
return game
}
// === XXX ===
function card_number(c) {
return data.cards[c].number
}
function card_name(c) {
return data.cards[c].name
}
function player_name() {
let p = player_index()
return data.scenarios[game.scenario].players[p].name
}
function set_opponent_active() {
if (game.active === P1)
game.active = P2
else
game.active = P1
}
function player_index() {
if (game.active === P1)
return 0
return 1
}
function add_cubes(c, n) {
let limit = data.cards[c].special
let old = map_get(game.cubes, c, 0)
map_set(game.cubes, c, Math.min(limit, old + n))
}
function remove_cube(c) {
let old = map_get(game.cubes, c, 0)
map_set(game.cubes, c, Math.min(0, old - 1))
}
function add_sticks(c, n) {
let old = map_get(game.sticks, c, 0)
map_set(game.sticks, c, old + n)
}
function remove_sticks(c, n) {
let old = map_get(game.sticks, c, 0)
map_set(game.sticks, c, Math.max(0, old - n))
}
function remove_dice(c) {
for (let i = 0; i < 12; ++i)
if (get_dice_location(i) === c)
set_dice_location(i, POOL)
}
function pay_for_action(c) {
if (data.cards[c].special)
remove_cube(c)
else
remove_dice(c)
}
// === ROLL PHASE ===
function get_player_dice_value(p, i) {
return game.dice[p * 12 + i * 2]
}
function get_player_dice_location(p, i) {
return game.dice[p * 12 + i * 2 + 1]
}
function set_player_dice_value(p, i, v) {
game.dice[p * 12 + i * 2] = v
}
function set_player_dice_location(p, i, v) {
game.dice[p * 12 + i * 2 + 1] = v
}
function get_dice_value(d) {
return game.dice[d * 2]
}
function get_dice_location(i) {
return game.dice[i * 2 + 1]
}
function set_dice_location(d, v) {
game.dice[d * 2 + 1] = v
}
function is_pool_die(i, v) {
let p = player_index()
return get_player_dice_location(p, i) < 0 && get_player_dice_value(p, i) === v
}
function is_pool_die_range(i, lo, hi) {
let p = player_index()
if (get_player_dice_location(p, i) < 0) {
let v = get_player_dice_value(p, i)
return v >= lo && v <= hi
}
return false
}
const place_dice_once = {
"(1)": true,
"(2)": true,
"(3)": true,
"(4)": true,
"(5)": true,
"(6)": true,
"(1)/(2)": true,
"(1)-(3)": true,
"(1)-(4)": true,
"(1)-(5)": true,
"(2)/(3)": true,
"(2)-(4)": true,
"(2)-(5)": true,
"(2)-(6)": true,
"(3)/(4)": true,
"(3)-(5)": true,
"(3)-(6)": true,
"(4)/(5)": true,
"(4)-(6)": true,
"(5)/(6)": true,
}
const place_dice_check = {
"Straight 3": check_straight_3,
"Straight 4": check_straight_4,
"Doubles": check_doubles,
"Triples": check_triples,
"1": (c) => check_single(c, 1),
"2": (c) => check_single(c, 2),
"3": (c) => check_single(c, 3),
"4": (c) => check_single(c, 4),
"5": (c) => check_single(c, 5),
"6": (c) => check_single(c, 6),
"(1)": (c) => check_single(c, 1),
"(2)": (c) => check_single(c, 2),
"(3)": (c) => check_single(c, 3),
"(4)": (c) => check_single(c, 4),
"(5)": (c) => check_single(c, 5),
"(6)": (c) => check_single(c, 6),
"Any": (c) => check_range(c, 1, 6),
"1/2": (c) => check_range(c, 1, 2),
"1-3": (c) => check_range(c, 1, 3),
"1-4": (c) => check_range(c, 1, 4),
"1-5": (c) => check_range(c, 1, 5),
"2/3": (c) => check_range(c, 2, 2),
"2-4": (c) => check_range(c, 2, 4),
"2-5": (c) => check_range(c, 2, 5),
"2-6": (c) => check_range(c, 2, 6),
"3/4": (c) => check_range(c, 3, 4),
"3-5": (c) => check_range(c, 3, 5),
"3-6": (c) => check_range(c, 3, 6),
"4/5": (c) => check_range(c, 4, 5),
"4-6": (c) => check_range(c, 4, 6),
"5/6": (c) => check_range(c, 5, 6),
"(1)/(2)": (c) => check_range(c, 1, 2),
"(1)-(3)": (c) => check_range(c, 1, 3),
"(1)-(4)": (c) => check_range(c, 1, 4),
"(1)-(5)": (c) => check_range(c, 1, 5),
"(2)/(3)": (c) => check_range(c, 2, 2),
"(2)-(4)": (c) => check_range(c, 2, 4),
"(2)-(5)": (c) => check_range(c, 2, 5),
"(2)-(6)": (c) => check_range(c, 2, 6),
"(3)/(4)": (c) => check_range(c, 3, 4),
"(3)-(5)": (c) => check_range(c, 3, 5),
"(3)-(6)": (c) => check_range(c, 3, 6),
"(4)/(5)": (c) => check_range(c, 4, 5),
"(4)-(6)": (c) => check_range(c, 4, 6),
"(5)/(6)": (c) => check_range(c, 5, 6),
}
const place_dice_gen = {
"Straight 3": gen_straight_3,
"Straight 4": gen_straight_4,
"Doubles": gen_doubles,
"Triples": gen_triples,
"1": (c) => gen_single(c, 1),
"2": (c) => gen_single(c, 2),
"3": (c) => gen_single(c, 3),
"4": (c) => gen_single(c, 4),
"5": (c) => gen_single(c, 5),
"6": (c) => gen_single(c, 6),
"(1)": (c) => gen_single(c, 1),
"(2)": (c) => gen_single(c, 2),
"(3)": (c) => gen_single(c, 3),
"(4)": (c) => gen_single(c, 4),
"(5)": (c) => gen_single(c, 5),
"(6)": (c) => gen_single(c, 6),
"Any": (c) => gen_range(c, 1, 6),
"1/2": (c) => gen_range(c, 1, 2),
"1-3": (c) => gen_range(c, 1, 3),
"1-4": (c) => gen_range(c, 1, 4),
"1-5": (c) => gen_range(c, 1, 5),
"2/3": (c) => gen_range(c, 2, 2),
"2-4": (c) => gen_range(c, 2, 4),
"2-5": (c) => gen_range(c, 2, 5),
"2-6": (c) => gen_range(c, 2, 6),
"3/4": (c) => gen_range(c, 3, 4),
"3-5": (c) => gen_range(c, 3, 5),
"3-6": (c) => gen_range(c, 3, 6),
"4/5": (c) => gen_range(c, 4, 5),
"4-6": (c) => gen_range(c, 4, 6),
"5/6": (c) => gen_range(c, 5, 6),
"(1)/(2)": (c) => gen_range(c, 1, 2),
"(1)-(3)": (c) => gen_range(c, 1, 3),
"(1)-(4)": (c) => gen_range(c, 1, 4),
"(1)-(5)": (c) => gen_range(c, 1, 5),
"(2)/(3)": (c) => gen_range(c, 2, 2),
"(2)-(4)": (c) => gen_range(c, 2, 4),
"(2)-(5)": (c) => gen_range(c, 2, 5),
"(2)-(6)": (c) => gen_range(c, 2, 6),
"(3)/(4)": (c) => gen_range(c, 3, 4),
"(3)-(5)": (c) => gen_range(c, 3, 5),
"(3)-(6)": (c) => gen_range(c, 3, 6),
"(4)/(5)": (c) => gen_range(c, 4, 5),
"(4)-(6)": (c) => gen_range(c, 4, 6),
"(5)/(6)": (c) => gen_range(c, 5, 6),
}
const place_dice_take = {
"Straight 3": take_straight_3,
"Straight 4": take_straight_4,
"Doubles": take_doubles,
"Triples": take_triples,
"1": take_single,
"2": take_single,
"3": take_single,
"4": take_single,
"5": take_single,
"6": take_single,
"(1)": take_single,
"(2)": take_single,
"(3)": take_single,
"(4)": take_single,
"(5)": take_single,
"(6)": take_single,
"Any": (c, d) => take_single(c, d),
"1/2": (c, d) => take_single(c, d),
"1-3": (c, d) => take_single(c, d),
"1-4": (c, d) => take_single(c, d),
"1-5": (c, d) => take_single(c, d),
"2/3": (c, d) => take_single(c, d),
"2-4": (c, d) => take_single(c, d),
"2-5": (c, d) => take_single(c, d),
"2-6": (c, d) => take_single(c, d),
"3/4": (c, d) => take_single(c, d),
"3-5": (c, d) => take_single(c, d),
"3-6": (c, d) => take_single(c, d),
"4/5": (c, d) => take_single(c, d),
"4-6": (c, d) => take_single(c, d),
"5/6": (c, d) => take_single(c, d),
"(1)/(2)": (c, d) => take_single(c, d),
"(1)-(3)": (c, d) => take_single(c, d),
"(1)-(4)": (c, d) => take_single(c, d),
"(1)-(5)": (c, d) => take_single(c, d),
"(2)/(3)": (c, d) => take_single(c, d),
"(2)-(4)": (c, d) => take_single(c, d),
"(2)-(5)": (c, d) => take_single(c, d),
"(2)-(6)": (c, d) => take_single(c, d),
"(3)/(4)": (c, d) => take_single(c, d),
"(3)-(5)": (c, d) => take_single(c, d),
"(3)-(6)": (c, d) => take_single(c, d),
"(4)/(5)": (c, d) => take_single(c, d),
"(4)-(6)": (c, d) => take_single(c, d),
"(5)/(6)": (c, d) => take_single(c, d),
}
function can_place_dice(c) {
let pattern = data.cards[c].dice
if (!pattern)
throw Error("bad card definition: " + data.cards[c].number)
let pred = place_dice_check[pattern]
if (!pred)
throw Error("bad pattern definition: " + pattern)
let wing = data.cards[c].wing
for (let i = 0; i < game.placed.length; i += 2) {
let x = game.placed[i]
if (x !== c) {
// TODO: place_2_on_WING ability
let i_wing = data.cards[x].wing
if (i_wing === wing)
return false
}
}
if (place_dice_once[pattern]) {
if (map_has(game.placed, c))
return false
}
return pred(c)
}
function can_place_value(c, v) {
let old_v = map_get(game.placed, c, 0)
return old_v === 0 || old_v === v
}
function pool_has_single(v) {
for (let i = 0; i < 6; ++i)
if (is_pool_die(i, v))
return true
return false
}
function check_single_count(c, v, x) {
if (!can_place_value(c, v))
return false
let n = 0
for (let i = 0; i < 6; ++i)
if (is_pool_die(i, v) && ++n >= x)
return true
return false
}
function check_single(c, v) {
if (!can_place_value(c, v))
return false
for (let i = 0; i < 6; ++i)
if (is_pool_die(i, v))
return true
return false
}
function check_range(c, lo, hi) {
let old_v = map_get(game.placed, c, 0)
if (old_v > 0)
return pool_has_single(old_v)
for (let i = 0; i < 6; ++i)
if (is_pool_die_range(i, lo, hi))
return true
return false
}
function check_all_3(c, x, y, z) {
if (!can_place_value(c, x))
return false
return pool_has_single(x) && pool_has_single(y) && pool_has_single(z)
}
function check_all_4(c, x, y, z, w) {
if (!can_place_value(c, x))
return false
return pool_has_single(c, x) && pool_has_single(y) && pool_has_single(z) && pool_has_single(w)
}
function check_straight_3(c) {
return (
check_all_3(c, 1, 2, 3) ||
check_all_3(c, 2, 3, 4) ||
check_all_3(c, 3, 4, 5) ||
check_all_3(c, 4, 5, 6)
)
}
function check_straight_4(c) {
return (
check_all_4(c, 1, 2, 3, 4) ||
check_all_4(c, 2, 3, 4, 5) ||
check_all_4(c, 3, 4, 5, 6)
)
}
function check_doubles() {
return (
check_single_count(c, 1, 2) ||
check_single_count(c, 2, 2) ||
check_single_count(c, 3, 2) ||
check_single_count(c, 4, 2) ||
check_single_count(c, 5, 2) ||
check_single_count(c, 6, 2)
)
}
function check_triples() {
return (
check_single_count(c, 1, 3) ||
check_single_count(c, 2, 3) ||
check_single_count(c, 3, 3) ||
check_single_count(c, 4, 3) ||
check_single_count(c, 5, 3) ||
check_single_count(c, 6, 3)
)
}
function gen_pool_die(v) {
let p = player_index()
for (let i = 0; i < 6; ++i)
if (get_player_dice_location(p, i) < 0 && get_player_dice_value(p, i) === v)
gen_action_die(p * 6 + i)
}
function gen_single(c, v) {
if (!can_place_value(c, v))
return false
gen_pool_die(v)
}
function gen_range(c, lo, hi) {
for (let v = lo; v <= hi; ++v)
gen_single(c, v)
}
function gen_straight_3(c) {
if (check_all_3(c, 1, 2, 3))
gen_pool_die(1)
if (check_all_3(c, 2, 3, 4))
gen_pool_die(2)
if (check_all_3(c, 3, 4, 5))
gen_pool_die(3)
if (check_all_3(c, 4, 5, 6))
gen_pool_die(4)
}
function gen_straight_4(c) {
if (check_all_4(c, 1, 2, 3, 4))
gen_pool_die(1)
if (check_all_4(c, 2, 3, 4, 5))
gen_pool_die(2)
if (check_all_4(c, 3, 4, 5, 6))
gen_pool_die(3)
}
function gen_doubles(c) {
for (let v = 1; v <= 6; ++v)
if (check_single_count(c, v, 2))
gen_pool_die(v)
}
function gen_triples() {
for (let v = 1; v <= 6; ++v)
if (check_single_count(c, v, 3))
gen_pool_die(v)
}
function find_and_take_single(c, v) {
let p = player_index()
for (let i = 0; i < 6; ++i) {
if (get_player_dice_location(p, i) < 0 && get_player_dice_value(p, i) === v) {
set_player_dice_location(p, i, c)
return
}
}
throw new Error("cannot find die of value " + v)
}
function take_single(c, d) {
set_dice_location(d, c)
map_set(game.placed, c, get_dice_value(d))
}
function take_doubles(c, d) {
let v = get_dice_value(d)
take_single(c, d)
find_and_take_single(c, v)
}
function take_triples(c, d) {
let v = get_dice_value(d)
take_single(c, d)
find_and_take_single(c, v)
find_and_take_single(c, v)
}
function take_straight_3(c, d) {
let v = get_dice_value(d)
take_single(c, d)
find_and_take_single(c, v+1)
find_and_take_single(c, v+2)
}
function take_straight_4(c, d) {
let v = get_dice_value(d)
take_single(c, d)
find_and_take_single(c, v+1)
find_and_take_single(c, v+2)
find_and_take_single(c, v+3)
}
function goto_roll_phase() {
game.selected = -1
game.target = -1
game.action = 0
let p = player_index()
for (let i = 0; i < 6; ++i)
if (get_player_dice_location(p, i) < 0)
set_player_dice_value(p, i, 0)
game.state = "roll"
}
states.roll = {
prompt() {
view.prompt = "Roll the dice in your pool."
view.actions.roll = 1
},
roll() {
clear_undo()
let p = player_index()
for (let i = 0; i < 6; ++i)
if (get_player_dice_location(p, i) < 0)
set_player_dice_value(p, i, random(6) + 1)
game.state = "place"
},
}
function gen_place_dice_select_card() {
let p = player_index()
for (let c of game.front[p]) {
if (c === game.selected)
continue
if (can_place_dice(c))
gen_action_card(c)
}
}
states.place = {
prompt() {
view.prompt = "Place dice on your formations."
gen_place_dice_select_card()
view.actions.end_turn = 1
},
card(c) {
push_undo()
game.selected = c
game.state = "place_on_card"
},
end_turn() {
end_roll_phase()
},
}
states.place_on_card = {
prompt() {
let card = data.cards[game.selected]
view.selected = game.selected
view.prompt = "Place dice on " + card.name + "."
gen_place_dice_select_card()
place_dice_gen[card.dice](game.selected)
view.actions.end_turn = 1
},
card(c) {
if (c === game.selected) {
game.selected = -1
game.state = "place"
} else {
game.selected = c
game.state = "place_on_card"
}
},
die(d) {
push_undo()
place_dice_take[data.cards[game.selected].dice](game.selected, d)
if (!can_place_dice(game.selected))
game.state = "place"
},
end_turn() {
end_roll_phase()
},
}
function end_roll_phase() {
clear_undo()
map_clear(game.placed)
// Remove placed dice to add cube on special cards.
for (let c of game.front[player_index()]) {
let s = data.cards[c].special
if (s && has_any_dice_on_card(c)) {
map_set(game.cubes, c, Math.min(s, map_get(game.cubes, c, 0) + 1))
remove_dice(c)
}
}
set_opponent_active()
goto_action_phase()
}
// === ACTION PHASE ===
function has_any_dice_on_card(c) {
for (let i = 0; i < 12; ++i)
if (get_dice_location(i) === c)
return true
return false
}
function has_any_cubes_on_card(c) {
return map_get(game.cubes, c, 0) >= 1
}
function count_dice_on_card(c, v) {
let n = 0
for (let i = 0; i < 12; ++i)
if (get_dice_location(i) === c)
++n
return n
}
function count_dice_on_card_with_value(c, v) {
let n = 0
for (let i = 0; i < 12; ++i)
if (get_dice_location(i) === c && get_dice_value(i) === v)
++n
return n
}
function require_pair(c) {
for (let v = 1; v <= 6; ++v)
if (count_dice_on_card_with_value(c, v) >= 2)
return true
return false
}
function require_triplet(c) {
for (let v = 1; v <= 6; ++v)
if (count_dice_on_card_with_value(c, v) >= 3)
return true
return false
}
function require_full_house(c) {
let n3 = 0
let n2 = 0
for (let v = 1; v <= 6; ++v) {
let n = count_dice_on_card_with_value(c, v)
if (n >= 3)
++n3
else if (n >= 2)
++n2
}
return (n3 >= 2) || (n3 >= 1 && n2 >= 1)
}
function require_two_pairs(c) {
let n = 0
for (let v = 1; v <= 6; ++v)
if (count_dice_on_card_with_value(c, v) >= 2)
++n
return n >= 2
}
function check_cube_requirement(c, req) {
switch (req) {
case "3 cubes":
return map_get(game.cubes, c, 0) >= 3
case undefined:
return map_get(game.cubes, c, 0) >= 1
default:
throw new Error("invalid action requirement: " + req)
}
}
function check_dice_requirement(c, req) {
switch (req) {
case "Full House":
return require_full_house(c)
case "Pair":
case "Pair, Voluntary":
return require_pair(c)
case "Triplet":
return require_triplet(c)
case "Two Pairs":
return require_two_pairs(c)
case "Voluntary":
case undefined:
return has_any_dice_on_card(c)
default:
throw new Error("invalid action requirement: " + req)
}
}
function is_action(c, a) {
return (a.type === "Bombard" || a.type === "Attack" || a.type === "Command")
}
function is_reaction(c, a) {
return (a.type === "Screen" || a.type === "Counterattack" || a.type === "Absorb")
}
function can_take_action(c, a) {
if (a.type === "Bombard" || a.type === "Attack" || a.type === "Command") {
if (data.cards[c].special)
return check_cube_requirement(c, a.requirement)
else
return check_dice_requirement(c, a.requirement)
}
return false
}
function can_take_any_action() {
let p = player_index()
for (let c of game.front[p]) {
if (has_any_dice_on_card(c))
return true
if (has_any_cubes_on_card(c))
return true
}
return false
}
function goto_action_phase() {
if (game.reacted) {
game.reacted = 0
goto_roll_phase()
} else {
if (can_take_any_action())
game.state = "action"
else
goto_roll_phase()
}
}
states.action = {
prompt() {
view.prompt = "Take an action."
view.actions.pass = 1
let p = player_index()
for (let c of game.front[p]) {
let has_dice = has_any_dice_on_card(c)
let has_cube = has_any_cubes_on_card(c)
if (has_dice || has_cube) {
if (data.cards[c].actions.length >= 1) {
if (is_action(c, data.cards[c].actions[0])) {
if (can_take_action(c, data.cards[c].actions[0]))
gen_action_action1(c)
else if (has_dice)
gen_action_fizzle1(c)
}
}
if (data.cards[c].actions.length >= 2) {
if (is_action(c, data.cards[c].actions[1])) {
if (can_take_action(c, data.cards[c].actions[1]))
gen_action_action2(c)
else if (has_dice)
gen_action_fizzle2(c)
}
}
if (data.cards[c].retire)
gen_action_retire(c)
}
}
},
a1(c) {
push_undo()
goto_take_action(c, 0)
},
a2(c) {
push_undo()
goto_take_action(c, 1)
},
f1(c) {
push_undo()
goto_fizzle(c)
},
f2(c) {
push_undo()
goto_fizzle(c)
},
pass() {
push_undo()
goto_roll_phase()
},
}
function goto_fizzle(c) {
log("Fizzled " + card_number(c))
pay_for_action(c)
end_action_phase()
}
function goto_take_action(c, ix) {
let a = data.cards[c].actions[ix]
game.selected = c
game.action = ix
switch (a.type) {
case "Attack":
game.state = "attack"
break
case "Bombard":
game.state = "bombard"
break
case "Command":
game.state = "command"
break
}
}
function current_action() {
return data.cards[game.selected].actions[game.action]
}
function find_target_of_attack() {
let a = current_action()
for (let c of a.target_list) {
if (game.front[0].includes(c))
return c
if (game.front[1].includes(c))
return c
}
}
function find_target_of_command(c) {
let a = current_action()
for (let c of a.target_list) {
if (game.reserve[0].includes(c))
return c
if (game.reserve[1].includes(c))
return c
}
}
states.bombard = {
prompt() {
view.prompt = "Bombard."
view.actions.bombard = 1
},
bombard() {
let opp = 1 - player_index()
game.morale[opp] --
pay_for_action(game.selected)
end_action_phase()
},
}
states.attack = {
prompt() {
let t = find_target_of_attack()
view.prompt = "Attack " + card_name(t) + "."
view.selected = game.selected
gen_action_card(t)
},
card(c) {
game.target = c
apply_attack(current_action())
pay_for_action(game.selected)
end_action_phase()
},
}
states.command = {
prompt() {
let t = find_target_of_command()
view.prompt = "Bring " + card_name(t) + " out of reserve."
view.selected = game.selected
gen_action_card(t)
},
card(c) {
let p = player_index()
array_remove_item(game.reserve[p], c)
// TODO: insert where?
game.front[p].push(p)
pay_for_action(game.selected)
end_action_phase()
},
}
function end_action_phase() {
goto_roll_phase()
}
// === ATTACK EFFECTS ===
function apply_self() {
remove_sticks(game.selected, 1)
}
function apply_hit() {
remove_sticks(game.target, 1)
}
function apply_hit_per_die() {
remove_sticks(game.target, count_dice_on_card(game.selected))
}
function apply_hit_per_pair() {
remove_sticks(game.target, count_dice_on_card(game.selected) >> 1)
}
function apply_attack(a) {
switch (a.effect) {
default:
throw new Error("invalid attack effect: " + text)
break
case "1 hit.":
apply_hit()
break
case "1 hit. 1 self per action.":
apply_hit()
apply_self()
break
case "1 hit per die.":
apply_hit_per_die()
break
case "1 hit per die. 1 self per action.":
apply_hit_per_die()
apply_self()
break
case "1 hit per pair.":
apply_hit_per_pair()
break
case "1 hit per pair. 1 self per action.":
apply_hit_per_pair()
apply_self()
break
case "1 hit, PLUS 1 hit per die. 1 self per action.":
apply_hit()
apply_hit_per_die()
apply_self()
break
}
}
// === COMMON LIBRARY ===
function gen_action(action, argument) {
if (!(action in view.actions))
view.actions[action] = [ argument ]
else
view.actions[action].push(argument)
}
function gen_action_card(c) {
gen_action("card", c)
}
function gen_action_die(d) {
gen_action("die", d)
}
function gen_action_action1(c) {
gen_action("a1", c)
}
function gen_action_action2(c) {
gen_action("a2", c)
}
function gen_action_fizzle1(c) {
gen_action("f1", c)
}
function gen_action_fizzle2(c) {
gen_action("f2", c)
}
function gen_action_retire(c) {
gen_action("retire", c)
}
function log(msg) {
game.log.push(msg)
}
function clear_undo() {
if (game.undo) {
game.undo.length = 0
}
}
function push_undo() {
if (game.undo) {
let copy = {}
for (let k in game) {
let v = game[k]
if (k === "undo")
continue
else if (k === "log")
v = v.length
else if (typeof v === "object" && v !== null)
v = object_copy(v)
copy[k] = v
}
game.undo.push(copy)
}
}
function pop_undo() {
if (game.undo) {
let save_log = game.log
let save_undo = game.undo
game = save_undo.pop()
save_log.length = game.log
game.log = save_log
game.undo = save_undo
}
}
function random(range) {
// Largest MLCG that will fit its state in a double.
// Uses BigInt for arithmetic, so is an order of magnitude slower.
// https://www.ams.org/journals/mcom/1999-68-225/S0025-5718-99-00996-5/S0025-5718-99-00996-5.pdf
// m = 2**53 - 111
return (game.seed = Number(BigInt(game.seed) * 5667072534355537n % 9007199254740881n)) % range
}
function shuffle(list) {
// Fisher-Yates shuffle
for (let i = list.length - 1; i > 0; --i) {
let j = random(i + 1)
let tmp = list[j]
list[j] = list[i]
list[i] = tmp
}
}
// Fast deep copy for objects without cycles
function object_copy(original) {
if (Array.isArray(original)) {
let n = original.length
let copy = new Array(n)
for (let i = 0; i < n; ++i) {
let v = original[i]
if (typeof v === "object" && v !== null)
copy[i] = object_copy(v)
else
copy[i] = v
}
return copy
} else {
let copy = {}
for (let i in original) {
let v = original[i]
if (typeof v === "object" && v !== null)
copy[i] = object_copy(v)
else
copy[i] = v
}
return copy
}
}
// Array remove and insert (faster than splice)
function array_remove(array, index) {
let n = array.length
for (let i = index + 1; i < n; ++i)
array[i - 1] = array[i]
array.length = n - 1
}
function array_remove_item(array, item) {
let n = array.length
for (let i = 0; i < n; ++i)
if (array[i] === item)
return array_remove(array, i)
}
function array_insert(array, index, item) {
for (let i = array.length; i > index; --i)
array[i] = array[i - 1]
array[index] = item
}
function array_remove_pair(array, index) {
let n = array.length
for (let i = index + 2; i < n; ++i)
array[i - 2] = array[i]
array.length = n - 2
}
function array_insert_pair(array, index, key, value) {
for (let i = array.length; i > index; i -= 2) {
array[i] = array[i-2]
array[i+1] = array[i-1]
}
array[index] = key
array[index+1] = value
}
// Set as plain sorted array
function set_clear(set) {
set.length = 0
}
function set_has(set, item) {
let a = 0
let b = set.length - 1
while (a <= b) {
let m = (a + b) >> 1
let x = set[m]
if (item < x)
b = m - 1
else if (item > x)
a = m + 1
else
return true
}
return false
}
function set_add(set, item) {
let a = 0
let b = set.length - 1
while (a <= b) {
let m = (a + b) >> 1
let x = set[m]
if (item < x)
b = m - 1
else if (item > x)
a = m + 1
else
return
}
array_insert(set, a, item)
}
function set_delete(set, item) {
let a = 0
let b = set.length - 1
while (a <= b) {
let m = (a + b) >> 1
let x = set[m]
if (item < x)
b = m - 1
else if (item > x)
a = m + 1
else {
array_remove(set, m)
return
}
}
}
function set_toggle(set, item) {
let a = 0
let b = set.length - 1
while (a <= b) {
let m = (a + b) >> 1
let x = set[m]
if (item < x)
b = m - 1
else if (item > x)
a = m + 1
else {
array_remove(set, m)
return
}
}
array_insert(set, a, item)
}
// Map as plain sorted array of key/value pairs
function map_clear(map) {
map.length = 0
}
function map_has(map, key) {
let a = 0
let b = (map.length >> 1) - 1
while (a <= b) {
let m = (a + b) >> 1
let x = map[m<<1]
if (key < x)
b = m - 1
else if (key > x)
a = m + 1
else
return true
}
return false
}
function map_get(map, key, missing) {
let a = 0
let b = (map.length >> 1) - 1
while (a <= b) {
let m = (a + b) >> 1
let x = map[m<<1]
if (key < x)
b = m - 1
else if (key > x)
a = m + 1
else
return map[(m<<1)+1]
}
return missing
}
function map_set(map, key, value) {
let a = 0
let b = (map.length >> 1) - 1
while (a <= b) {
let m = (a + b) >> 1
let x = map[m<<1]
if (key < x)
b = m - 1
else if (key > x)
a = m + 1
else {
map[(m<<1)+1] = value
return
}
}
array_insert_pair(map, a<<1, key, value)
}
function map_delete(map, item) {
let a = 0
let b = (map.length >> 1) - 1
while (a <= b) {
let m = (a + b) >> 1
let x = map[m<<1]
if (item < x)
b = m - 1
else if (item > x)
a = m + 1
else {
array_remove_pair(map, m<<1)
return
}
}
}
function object_diff(a, b) {
if (a === b)
return false
if (a !== null && b !== null && typeof a === "object" && typeof b === "object") {
if (Array.isArray(a)) {
if (!Array.isArray(b))
return true
let a_length = a.length
if (b.length !== a_length)
return true
for (let i = 0; i < a_length; ++i)
if (object_diff(a[i], b[i]))
return true
return false
}
for (let key in a)
if (object_diff(a[key], b[key]))
return true
for (let key in b)
if (!(key in a))
return true
return false
}
return true
}
|