-
Notifications
You must be signed in to change notification settings - Fork 10
/
glee_min.pl
1841 lines (1440 loc) · 126 KB
/
glee_min.pl
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
% First saved: 17/03/11
% Last saved: 25/04/11
%
% Status:
% matchup strategy is OK but rudimentary
%
% Doing:
% Adding combat heuristic
% Done for blocking.
% ...and just attacks with everything for now!
% adding minimax
% need to add active player information to moves/4
% Done for land_set
% Done for castable_set
%
% Todo
% add game state evaluation -based tactics to strategy
% early_game
% mid_game
% late_game
% a.k.a The Plan
%
% NOTES:
/*
matchup strategy follows deck strategy closely,
for now. The important thing is that the inference engine
does recognise the matchup properly, though that is not
yet apparent in its game choices. If there is time I'll
add some more obvious tactic.
*/
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%% Swi Compatibility %%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
:- set_prolog_flag(backquoted_string,true).
% Swi LPA backtic string compatibility.
:- style_check(-singleton).
% Stop Swi warning on singleton vars.
:- style_check(-discontiguous).
% Stop Swi warning on discontiguous clauses.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%% AI Facts %%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Inference engine operators
:- dynamic(with/2).
:- dynamic(present/1).
:- dynamic minimaxing/0.
:- dynamic blocks/2.
% Deck tactics:
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%land_tactic/2 17/03/11
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
/* land_tactic(Deck, Tactic) */
% land Tactic(s) for Deck.
land_tactic('Raspberry', play_any_land).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%spell_tactic/2 ?/03/11
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
/* spell_tactic(Deck, Tactic) */
% spell-casting Tactic(s) for Deck.
spell_tactic('Raspberry', cast_any_creature).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%combat_tactic/2 24/03/11
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
/* combat_tactic(Deck, Tactic) */
% combat Tactic(s) for Deck.
% Simple tatctic to attack with everything except utility creatures.
combat_tactic('Raspberry', conserve_utility).
% Matchup tactics:
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%land_tactic/3 17/03/11
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
/* land_tactic(Matchup, Deck, Tactic) */
% land Tactic(s) for Deck vs Matchup.
% Undefined matchup- normally, beginning of game
land_tactic( [], _,play_any_land).
land_tactic('Raspberry', 'Raspberry', play_any_land).
land_tactic('Pistachio', 'Raspberry', play_any_land).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%spell_tactic/3 ?/03/11
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
/* spell_tactic(Matchup, Deck, Tactic) */
% spell-casting Tactic(s) for Deck vs Matchup.
% Undefined matchup- normally, beginning of game
spell_tactic([], _, cast_any_creature).
spell_tactic('Raspberry', 'Raspberry', cast_any_creature).
spell_tactic('Pistachio', 'Raspberry', cast_any_creature).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%combat_tactic/3 24/03/11
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
/* combat_tactic(Matchup, Deck, Tactic) */
% combat Tactic(s) for Deck vs Matchup.
% Undefined matchup- normally, beginning of game
combat_tactic([], _, conserve_utility).
combat_tactic('Raspberry', 'Raspberry', conserve_utility).
combat_tactic('Pistachio', 'Raspberry', conserve_utility).
% General facts:
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%utility_creatures/2 26/03/11
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
/* utility_creatures(Deck, Utility) */
% The utility creatures to avoid declaring as attackers or
% blockers with when employing the tactic "conserve_utility"
utility_creatures('Raspberry', ['Prodigal Pyromancer']).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%% AI Rules %%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%glee_min/4 17/03/11
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% glee_min(combat, +Step, +Context, -Decision).
% Combat decisions.
glee_min(combat, Step, Context, Decision):-
deck('Glee-min', Deck),
strategy([combat, Deck, Step, Context], Decision).
% ^ OK, better naming of this "context" business needed.
% Non-combat decisions
glee_min(Active, Step, Phase, Play):-
deck('Glee-min', Deck),
strategy([Deck, Active, Step, Phase], Action_context),
action(Active, Step, Phase, Action_context, Play).
% where Action_context: [Action_name, Context],
% eg [cast_spell, Context]
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%strategy 17/03/11
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
/* strategy(+Context, -Action) */
% top-level decision-making goal for the computer opponent.
%%%% Notes %%%%
%%%%%%%%%%%%%%%
/*
strategy/2 takes in a list of facts about the game state
and returns an action to be taken by the compupter opponent.
The action is one of the actions available to players, ie, an
input to player_actions/5: play a land, cast a spell, activate
an ability, take a special action (other than playing a land),
pass the turn or inspect the state of the game- or concede!
The decision-making process has two steps: first a deck_strategy
goal is evaluated and an optimal action is returned. Then a
matchup strategy goal is evaluated and an action returned. If the
two actions don't match, Prolog is allowed to backtrack into
the deck-strategy evaluation goal, to attempt to find a new action.
if that fails, a minimax function is called to attempt to
brute-force its way through the problem.
In other words, the overall strategy can be described as a simple
implication relationship:
A AND B -> C OR D
Where:
A: the deck strategy rules
B: the matchup strategy rules
C: the action to be taken
D: the statistical evaluation alternative
That's the plan in any case!
(things that are likely to change: whether matchup_strategy
takes in the board position only as an input, or the deck_strategy
output too; whether the two strategy sub-clauses' output is
attemtped to be matched directly, or a further evaluation of their
combination is made).
Context information:
Own_deck (deck name)
Opponent_deck (")
*/
%%%%%%%%%%%%%%strategy (1) 15/03/11
strategy(Context, Action):-
deck_strategy(Context, Action),
matchup_strategy(Context, Action).
%%%%%%%%%%%%%%strategy (2) 15/03/11
strategy(Context, Action):-
minimax(Context, Action).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%deck_strategy 15/03/11
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
/* deck_strategy([+Deck_name, +Tactic | +Context], Play) */
deck_strategy(Context, Action):-
land_tactic(deck, Context, Action),
spell_tactic(deck, Context, Action),
/*activated_tactic,
specials_tactic,*/
combat_tactic(deck, Context, Action).
%%%%%%%%%%%%%%land_tactic/3 18/03/11
%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%land_tactic/3 (0) 19/03/11
% During combat it's probably best to stop evaluation here.
land_tactic(X, [combat | _rest], _Action):-
X = deck ; X = matchup.
% Made ^ explicit to distinguish from spell_tactic/3 fact
%%%%%%%%%%%%%%land_tactic/3 (0) 10/04/11
% During combat it's probably best to stop evaluation here.
land_tactic(X, [_Matchup, combat | _rest], _Action):-
X = deck ; X = matchup.
% Made ^ explicit to distinguish from spell_tactic/3 fact
%%%%%%%%%%%%%%land_tactic/3 (1) 19/03/11
% Finds the land_tactic for the deck and calls it
land_tactic(deck, [Deck_name | Context], Action):-
land_tactic(Deck_name, Tactic),
% ^ so that tactics for a deck can be specified separately
% Tactic(Context, Action). % eg Tactic = always_play_land
TActic =.. [Tactic, Context, Action],
TActic.
% ^ Note this separates the name of the deck from the rest
% of the contex, by the way
%%%%%%%%%%%%%%land_tactic/3 (2) 10/04/11
% If the matchup could not be defined, leave Action unbound to
% follow deck tactics, ie: "goldfish"
land_tactic(matchup, [ [], _Deck_name | _Context], _Action).
% State as a rule to distinguish from spell_tactic/3 fact
%%%%%%%%%%%%%%land_tactic/3 (3) 10/04/11
land_tactic(matchup, [Matchup, Deck_name | Context], Action):-
land_tactic(Matchup, Deck_name, Tactic),
%Tactic(Context, Action)
TActic =.. [Tactic, Context, Action],
TActic.
% eg [Map, Switches]:
% [[cancel - c,'Forest' - 'F'],['[c]ancel','[F]orest']]
%%%%%%%%%%%%%%spell_tactic/3 19/03/11
%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%spell_tactic/3 (0) 19/03/11
% ^ Proper tactics should play spells during combat
spell_tactic(X, [combat | _rest], _Action):-
X = deck ; X = matchup.
%%%%%%%%%%%%%%spell_tactic/3 (0) 19/03/11
% Gleemin has chosen to play a land.
spell_tactic(X, _Context, Action ):-
(X = deck ; X = matchup),
\+ type(Action, 0)/*,
Action = 108*/.
%%%%%%%%%%%%%%spell_tactic/3 (1) 19/03/11
% There's no choice of play yet.
spell_tactic(deck, [Deck_name | Context], Action):-
spell_tactic(Deck_name, Tactic),
%Tactic(Context, Action)
TActic =.. [Tactic, Context, Action],
TActic.
%%%%%%%%%%%%%%spell_tactic/3 (2) 10/04/11
% Matchup undefined: goldfish
spell_tactic(matchup, [ [], _Deck_name | _Context], _Action).
%%%%%%%%%%%%%%spell_tactic/3 (3) 10/04/11
spell_tactic(matchup, [Matchup, Deck_name | Context], Action):-
spell_tactic(Matchup, Deck_name, Tactic),
%Tactic(Context, Action),
TActic =.. [Tactic, Context, Action],
TActic.
%%%%%%%%%%%%%%combat_tactic/3 19/03/11
%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%combat_tactic/3 (0) 24/03/11
% Stops evaluation here if a decision has been made already
combat_tactic(X, _Context, Action ):-
(X = deck ; X = matchup),
\+ type(Action, 0).
%%%%%%%%%%%%%%combat_tactic/3 (1) 24/03/11
combat_tactic(deck, [combat, Deck_name | Context], Action):-
combat_tactic(Deck_name, Tactic),
%Tactic(Context, Action)
TActic =.. [Tactic, Context, Action],
TActic.
% Where Context = [Step, Identified]
% Note that Step is only the name of the step,
% not its State(Name) because the State (begins/ongoing/ends)
% is not really needed...
% I should really differentiate by Step also, because different
% tactics will need to apply to each combat step!
%%%%%%%%%%%%%%combat_tactic/3 (2) 10/04/11
combat_tactic(matchup, [ [], combat, _Deck | _Context], _Action).
%%%%%%%%%%%%%%combat_tactic/3 (3) 10/04/11
combat_tactic(matchup, [Matchup, combat, Deck_name | Context], Action):-
combat_tactic(Matchup, Deck_name, Tactic),
%Tactic(Context, Action)
TActic =.. [Tactic, Context, Action],
TActic.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%matchup_strategy 15/03/11
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Simply succeeds for now
%matchup_strategy(_Context, _Action).
matchup_strategy(Context, Action):-
Observation = (_X-_Object present),
Conclusion = (_Deck identified with _Factor certainty),
% ^ Patterns to look for or clear from the db
match_up(Observation, Conclusion, Matchup),
taunt(matchup_strategy, Matchup),
append([Matchup], Context, Full_context),
land_tactic(matchup, Full_context, Action),
spell_tactic(matchup, Full_context, Action),
/*activated_tactic,
specials_tactic,*/
combat_tactic(matchup, Full_context, Action).
%%%%%%%%%%%%%%match_up/3 10/04/11
%%%%%%%%%%%%%%%%%%%%%%%
/* match_up(+Observation, +Conclusion, -Deck) */
% Initialised the database and calls the forward chaining
% inference engine that tries to figure out the matchup,
% ie the opponent's deck. Observation and Conclusion
% are patterns of facts to be found in or cleared from the db.
% What deck is the opponent playing?
match_up(Observation, Conclusion, DEck):-
player(Player),
Player \= 'Glee-min',
database_primer(Player, Observation, Conclusion),
forward_chaining(Observation),
findall(X-Deck,
( Deck identified with X:Z certainty,
\+ Deck eliminated with _Y:Z certainty),
Certainties),
keysort(Certainties, Reverse), % get the highest factor
reverse(Reverse, [_X- DEck | _Rest]).
% ^ It should be possible to backtrack in here if there is
% no move agreement between deck and matchup strategy, and
% pick the next deck in the list of possibly identified matchups
% but only if there are no X:X certainties.
% Deck not yet identifiable (no plays yet from opponent?)
match_up(_Observation, _Conclusion, []).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%action/5 17/03/11
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%action/5 (1.1) 17/03/11
action(Active, Step, Phase, [play_land, Land_card], Play):-
play_land(Active, ['Glee-min', Land_card], Step, Phase, Play).
%%%%%%%%%%%%%%action/5 (2.0) 17/03/11
action('Glee-min',Step,Phase,[cast_spell, Context],Play ):-
cast_spell('Glee-min', Context, Step, Phase, Play).
action(_Active, _Step, _Phase, ['Glee-min' | [] ], _):- fail, !.
% ^ not casting, fall to the do-nothing clause below
%%%%%%%%%%%%%%action/5 (0) 17/03/11
action(_, _, _, _, 112):-
tab(25),write(`Glee-min says: Go`), nl,
tab(35), write(`* * *`), nl.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%resource_management/ 5 19/03/11
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
/* resource_management(+Resource, +Context) */
% How to spend a resource, such as Life, Cards, Mana etc.
% Currently only tapping permanents for mana is handled.
resource_management(tap_for_mana, Context):-
tap_any_sources(Context).
%%%%%%%%%%%%%%tap_any_sources/1 19/03/11
%%%%%%%%%%%%%%%%%%%%%%%
/* tap_any_sources(+Context) */
% Simple tactic to tap any available source
% for its full mana.
tap_any_sources(Context):-
Context = ['Glee-min', Spell, _, _, Switches, Identified, Sources, Cost],
mana_sources('Glee-min', Switches, Identified),
card([card_name Spell, mana_cost Cost, _, _, _, _, _, _]),
tap_each_source(Cost, Identified, [], [], Sources).
%%%%%%%%%%%%%%tap_each_source/5 19/03/11
%%%%%%%%%%%%%%%%%%%%%%%
/* tap_each_source(+Cost, +Identified, -Mana, [], -Sources ) */
% Simple mana tactic, to tap all available sources until the mana
% required is acquired.
% Cost: mana string; Mana: mana list; Sources: [Switch_1, ...]
% eg Identified: ['Island' - 'I',3],['Island' - l,2],['Island' - a,1]
%%%%%%%%%%%%%%tap_each_source/5 (0) 19/03/11
tap_each_source(_Cost, [], [], Sources, Sources).
%%%%%%%%%%%%%%tap_each_source/5 (1) 19/03/11
% The total mana from tapping the sources chosen so far will not
% be enough to satisfy the cost. Go on.
tap_each_source(Cost, [[Source - Switch, _Id] | Identified], Total, Temp, Sources):-
% mana_ability(Source, _Ability, Mana), % To Swi 09/10/11
mana_ability(Source, _Ability, Mana) ->
append(Total, [Mana], New_total),
% ^ add the mana from the current source to the total
atom_to_list(String, New_total),
\+ match_cost(Cost, String),
append(Temp, [Switch], New_temp),
tap_each_source(Cost, Identified, New_total, New_temp, Sources).
%%%%%%%%%%%%%%tap_each_source/5 (2) 19/03/11
% The total mana from tapping the sources chosen so far will be enough
% to satisfy the cost. Wrap things up.
tap_each_source(Cost, [[Source - Switch, _Id] | _Identified], Total, Temp, Sources):-
mana_ability(Source, _Ability, Mana),
append(Total, [Mana], New_total),
atom_to_list(String, New_total),
match_cost(Cost, String),
% ^ The mana total so far satisfies the cost
append(Temp, [Switch], New_temp),
tap_each_source(Cost, [], [], New_temp, Sources).
%%%%%%%%%%%%%%tap_each_source/5 (2) 19/03/11
tap_each_source(Cost, _Identified, _Mana, _Temp, _Sources):-
tab(25), write(`No mana sources to tap for ` - Cost), nl.
% ^ This is OK (now that I fixed Goblin Balloon Brigade >_<
% It takes care of tap_for_mana. Now I need to fix spend_mana also.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%% Statistic Evaluation %%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%minimax/X 22/04/11
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
/* init_alphabeta:-
reset_game,
populate_libraries,
shuffle_libraries,
draw_starting_hand.
*/
% minimax([combat, Deck, Step, Context], Action):-
minimax([combat, _Deck, one_blocker, Identified], Switch):-
\+ _X blocks _Y,
attackers(Attackers),
findall(Blocker-ID,
member([Blocker - _Switch, ID], Identified),
Blockers),
combat_heuristic(Attackers, Blockers, [], Blocks),
findall(X blocks Y,
( member(X blocks Y, Blocks),
asserta(X blocks Y) ),
_Nevermind),
Name-Id blocks _Attacker,
member([Name - Switch, Id], Identified).
minimax([combat, _Deck, one_blocker, Identified], Switch):-
Name-Id blocks _Attacker,
member([Name - Switch, Id], Identified).
% We should only fall here when there are no more blockers
% to declare.
minimax([combat, _Deck, one_blocker, _Identified], _Switch).
minimax([combat, _Deck, one_attacker, [Object, Identified]], Switch):-
%Object = object(NAme-ID, _State),
object_handle(Object, Blocker),
Blocker blocks (Name-Id),
member([Name - Switch, Id], Identified),
retract(Blocker blocks (Name-Id)).
% We fall here if something went wrong and there is no
% suitable X blocks Y fact in the db
minimax([combat, _Deck, one_attacker, Identified], _):-
write(`Problem in one_attacker!`),nl, fail,!.
%minimax([combat,'Tutorial Red',declare_attackers,[['Vulshok Berserker+' - 'V',1],['Goblin Balloon Brigade' - 'G',1]]],_489)
% Currently, attack with everything. For the Emperor!
minimax([combat, _Deck, declare_attackers, Identified], List_of_attackers):-
% conserve_utility([declare_attackers, Identified], List_of_attackers):-
findall(Switch,
(member([Attacker - Switch, _Id], Identified)),
List_of_attackers).
/*
conserve_utility([one_blocker, Identified], Switch):-
deck('Glee-min', Deck),
utility_creatures(Deck, Utility),
member([Blocker - Switch, _Id], Identified),
\+ member(Blocker, Utility).
% Identified: list of [Name - Switch, Id]
*/
% minimax([_Deck, _Active, _State(_Step), Phase], 112):-
minimax([_Deck, _Active, _Step, Phase], 112):-
Phase \= 'First Main'.
% ['Glee-min', State([]), 'First Main']
% minimax(['Testdeck Blue','Glee-min',begins([]),'First Main'],_12133)
% minimax([_Deck, Active, _State(_Step), Phase], Action):- %Swi, 18/06/11
minimax([_Deck, Active, _Step, Phase], Action):-
player(Next), Next \= 'Glee-min',
minimax(Phase, Active, 'Glee-min', Next, Action, 2, 8, 7).
minimax(Phase, Active, Playing, Next, BestMove, Depth, Alpha, Beta):-
moves(Phase, Active, Playing, Next, Moves),
save_player(Playing, Saved1),
save_player(Next, Saved2),
save_game(Game),
asserta(minimaxing),
evaluate_and_choose(Moves,[Phase,Active, Playing, Next],Depth,Alpha,Beta,112,(BestMove, _Value)),
restore_player(Playing, Saved1),
restore_player(Next, Saved2),
restore_game(Game),
retractall(minimaxing).
% plays land, casts spells? May break spells now.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%evaluate_and_choose/7 18/04/11
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
/* evaluate_and_choose(+Moves, +Position, +Depth, +Alpha, +Beta, Record, -BestMove) */
% Alphabeta minimax implementation from Sterling & Shapiro (1986), pg 301
/*
Chooses the BestMove from the set of Moves from the current Position
using the minimax algorithm with alpha-beta cutoff searching
Depth ply ahead. Alpha and Beta are the parameters of the algorithm.
Record records the current best move
*/
%%%%%%%%%%%%%%evaluate_and_choose/7 (1) 18/04/11
% Choose a move; find the new position; evaluate it; stop evaluation
% if upper or lower evaluation bounds are reached.
evaluate_and_choose( [Move_set | Moves ],Position,D,Alpha,Beta,Move1,BestMove):-
move(Position,Move_set,Move,Position1),
% ^ only need Move, not Move_set. Move_set is just one Move anyway.
alpha_beta(D,Position1,Alpha,Beta,_MoveX,Value),
Value1 is -Value,
% ^ Ah, no. You need to take their strategic advantage, ie, tempo
% if you're playing control, ca if you're playing beatdown.
cutoff(Move,Value1,D,Alpha,Beta,Moves,Position,Move1,BestMove).
%%%%%%%%%%%%%%evaluate_and_choose/7 (1) 18/04/11
% No more moves- "return" best move found so far.
evaluate_and_choose([],_Position,_D,_Alpha,_Beta,Move,(Move,_Alpha)).
%%%%%%%%%%%%%%alpha_beta/6 18/04/11
%%%%%%%%%%%%%%%%%%%%%%%
/* alpha_beta(+D,+Position,+Alpha,+Beta,+Move,+Value) */
%%%%%%%%%%%%%%alpha_beta/6 (0) 18/04/11
% Zero depth reached- evaluate position.
alpha_beta(0,[_Phase,_Active, Playing, Next],_Alpha,_Beta,_Move,Value):-
deck(Playing, Deck),
deck_type(Deck, _Colour, Type),
value(Playing, Next, Type, Value, _Their_value).
%%%%%%%%%%%%%%alpha_beta/6 (1) 18/04/11
alpha_beta(D,Position,Alpha,Beta,Move,Value):-
Position = [Phase, Active, Playing, Next],
moves(Phase, Active, Playing, Next, Moves),
Alpha1 is -Beta,
Beta1 is -Alpha,
D1 is D-1,
evaluate_and_choose(Moves,Position,D1,Alpha1,Beta1,nil,(Move,Value)).
%%%%%%%%%%%%%%cutoff/6 18/04/11
%%%%%%%%%%%%%%%%%%%%%%%
/* cutoff(+Move,+Value,+D,+Alpha,+Beta,+Moves,+Position,+Move1,-BestMove) */
%%%%%%%%%%%%%%cutoff/6 (0) 18/04/11
% Beta bound reached; stop evaluation
cutoff(Move,Value,_D,_Alpha,Beta,_Moves,_Position,_Move1,(Move,Value)):-
Value >= Beta.
%%%%%%%%%%%%%%cutoff/6 (1) 18/04/11
% Move value within bounds; continue evaluating.
cutoff(Move,Value,D,Alpha,Beta,Moves,Position,_Move1,BestMove):-
Alpha < Value, Value < Beta,
evaluate_and_choose(Moves,Position,D,Value,Beta,Move,BestMove).
%%%%%%%%%%%%%%cutoff/6 (2) 18/04/11
% Alpha bound reached; evaluate new move
cutoff(_Move,Value,D,Alpha,Beta,Moves,Position,Move1,BestMove):-
Value =< Alpha,
evaluate_and_choose(Moves,Position,D,Alpha,Beta,Move1,BestMove).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%move/4 18/04/11
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% move(Position, Move, Next_Position).
% The active player plays land and passes priority
move(['First Main', Playing, Playing, Next], Land, [play_land, Land], ['First Main', Playing, Next, Playing]):-
check_type(Land, _Supertype, ['Land'], _Subtype),
played_land('Glee-min', 'no'),
action('Glee-min', _Step, 'First Main', [play_land, Land], _Play).
%write(`Playing land`),nl,nl.
move(['First Main', Playing, Playing, Next], Land, 112, ['First Main', Playing, Next, Playing]):-
check_type(Land, _Supertype, ['Land'], _Subtype).
% The active player casts a spell and passes priority
% Context = [_cast_spell, [_Gleemin, Spell, _Choices, _Targets, Switches, Identified, Sources, Payment] ] ,
move(['First Main', Playing, Playing, Next], Context, Context, ['First Main', Playing, Next, Playing]):-
action('Glee-min',_Step,_Phase, Context, _Play),
zone('Stack', [Object | _rest]),
generate_effect(Object, 'Glee-min').
%write(`Casting spell`),nl,nl.
/* eg spell context:
[
[cast_spell, ['Glee-min','Goblin Piker',_9980,_9986,
[[cancel - c,skip - s,'Mountain' - 'M','Mountain' - o],
['[c]ancel','[s]kip','[M]ountain','M[o]untain']],
[['Mountain' - 'M',2],['Mountain' - o,1]],
['M',o],'1r']]
]
*/
% The Nonactive player passes priority
move([Phase, Active, Playing, Next], pass, 112, [Phase, Active, Next, Playing]).
% write(`Passing` - Playing),nl,nl.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%moves/4 14/04/11
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
/* moves(+Position, +Acive_player, +Opponent, -Moves) */
% Generates lists of legal moves (land to play, spells to cast,
% attackers and blockers order) at the current position
% A Position is the current State(Step) (tuple) or Phase (atom)
% Position: name of a phase, or a step-state,
% eg, 'First Main', begins('Upkeep'), 'Combat'
moves(Position, Active, Playing, Opponent, Moves):-
% ^ For combat, Position is implied by Active-Opponent relation
land_set(Position, Active, Playing, Opponent, Land_set),
castable_set(Position, Active, Playing, Opponent, Castable_set),
% ^ Glee-min may be the non-active player but still need to cast
attackers_set(Position, Active, Attackers),
attackers_sets(Attackers, Attackers_sets),
blockers_set(Position, Opponent, Blockers),
blockers_sets(Blockers, Blockers_sets),
blocking_sets(Attackers_sets, Blockers_sets, Blocking_sets),
append(Land_set, Castable_set, Set1),
append(Set1, Blocking_sets, Moves), !.
% Adding Active player arg:
/* land_set OK
castable_set OK
combat stuff broken- elsewhere?
*/
%%%%%%%%%%%%%%land_set/4 14/04/11
%%%%%%%%%%%%%%%%%%%%%%%
/* land_set(+Position, +Active_player, +Opponent, -Lands_to_play) */
% Generates a list of land that can be played
% by Active_player in this Position
% Already played a land this turn.
land_set('First Main', 'Glee-min', 'Glee-min', _Opponent, []):-
played_land('Glee-min', yes).
%%%%%%%%%%%%%%land_set/4 (1) 14/04/11
% Find all playable land:
land_set('First Main', 'Glee-min', 'Glee-min', _Opponent, Land_set):-
findall(Land,
( zone('Glee-min', 'Hand', Cards),
member(Land, Cards),
check_type(Land, _,['Land'],_) ),
Land_set).
%%%%%%%%%%%%%%land_set/4 (0) 14/04/11
% Not a main phase, or Gleemin is not the Active player.
% However note that future inference may involve this
land_set(_Position,_Active, _Playing, _Opponent, [pass]).
%%%%%%%%%%%%%%castable_set/4 14/04/11
%%%%%%%%%%%%%%%%%%%%%%%
/* castable_set(+Position, +Active_player, +Opponent, -Spells) */
% Generates a list of spells that can be cast by Active_player or
% his or her Opponent at the current Position.
%%%%%%%%%%%%%%castable_set/4 (1) 14/04/11
% This just finds the first castable creature for now.
castable_set('First Main', 'Glee-min', 'Glee-min', _Opponent, Castable_set):-
% ^ Player name needed for future inference of Hand contents
findall( [cast_spell, Context],
% ( cast_any_creature(['Glee-min', _State([]), 'First Main'], [cast_spell, Context]),! ), Swi, ??/06/11
( cast_any_creature(['Glee-min', _Step, 'First Main'], [cast_spell, Context]) ),
%Step =.. [_State, []],! ), % Swi, 04/07/11, no need to determine step for Main phases!
Castable_set).
% Context returns everything needed to call cast_spells directly:
% Context = ['Glee-min', Spell, _Choices, _Targets, Switches, Identified, Sources, Payment].
%%%%%%%%%%%%%%castable_set/4 (0) 14/04/11
% Not a main phase, or Gleemin is not the Active player.
castable_set(_Position, _Active, _Playing, _Opponent, []).
%%%%%%%%%%%%%%attackers_set/3 14/04/11
%%%%%%%%%%%%%%%%%%%%%%%
/* attackers_set(+Position, +Active_player, -Attackers) */
% Generates a list of Attackers controlled by Active_player
%%%%%%%%%%%%%%attackers_set/3 (1) 14/04/11
% All legal attackers on Player's side of the Battlefield:
attackers_set('Combat', Player, Permanents):-
zone(Player, 'Battlefield', Objects),
findall(Permanent - Id,
(member(object(Permanent -Id,State), Objects),
creature_type(Permanent),
legal_attacker(State)),
Permanents).
%%%%%%%%%%%%%%attackers_set/3 (0) 14/04/11
attackers_set(_Position, _Player, []).
% ^ No attacks outside of declare_attackers.
%%%%%%%%%%%%%%attackers_sets/2 14/04/11
%%%%%%%%%%%%%%%%%%%%%%%
/* attackers_sets(+Attackers, -Sets_of_attackers) */
% Generates attack combinations
%%%%%%%%%%%%%%attackers_sets/2 (0) 14/04/11
% No attackers declared.
attackers_sets([], []).
%%%%%%%%%%%%%%attackers_sets/2 (1) 14/04/11
% Generate all permutations of the attackers list
attackers_sets(Attackers, Sets):-
findall(Set, sublist(Set, Attackers), Sets).
% register_attackers_sets(Sets).
% ^ Why? I think it was to carry out minimax combat.
% I don't think it's a good idea- I should run the combat
% heuristic instead.
%%%%%%%%%%%%%%register_attackers_sets/2 14/04/11
%%%%%%%%%%%%%%%%%%%%%%%
/* register_attackers_sets(+Attackers_sets) */
% Adds each set of attackers to the database
register_attackers_sets([]).
register_attackers_sets([[] | Rest ]):-
register_attackers_sets(Rest).
register_attackers_sets([Attackers | Rest]):-
asserta(attackers(Attackers)),
% ^ There shouldn't be any duplicates at this point.
register_attackers_sets(Rest).
%%%%%%%%%%%%%%blockers_set/3 14/04/11
%%%%%%%%%%%%%%%%%%%%%%%
/* blockers_set(+Position, +Defending_player, -Blockers) */
% Generates a list of Blockers controlled by Defending_player
%%%%%%%%%%%%%%blockers_set/3 (1) 14/04/11
% All legal blockers on Player's side of the battlefield:
blockers_set('Combat', Player, Permanents):-
zone(Player, 'Battlefield', Objects),
findall(Permanent - Id,
(member(object(Permanent - Id,State), Objects),
creature_type(Permanent),
legal_blocker(State)),
Permanents).
%%%%%%%%%%%%%%blockers_set/3 (0) 14/04/11
% Not a combat phase or step
blockers_set(_Position, _Player, []).
%%%%%%%%%%%%%%blockers_sets/2 14/04/11
%%%%%%%%%%%%%%%%%%%%%%%
/* blockers_sets(+Blockers, -Sets_of_blockers) */
% Generates blocking combinations
%%%%%%%%%%%%%%blockers_sets/2 (0) 14/04/11
% No blockers declared.
blockers_sets([], []).
%%%%%%%%%%%%%%blockers_sets/2 (1) 14/04/11
% Generate all permutations of the blockers list:
blockers_sets(Blockers, Sets):-
findall(Set, sublist(Set, Blockers), Sets).
% register_blockers_sets(Sets).
%%%%%%%%%%%%%%register_blockers_sets/2 14/04/11
%%%%%%%%%%%%%%%%%%%%%%%
/* register_blockers_sets(+Blockers_sets) */
% Adds each set of blockers to the database
register_blockers_sets([]).
register_blockers_sets([[] | Rest ]):-
register_blockers_sets(Rest).
register_blockers_sets([Blockers | Rest]):-
asserta(blockers(Blockers)),
% ^ There shouldn't be any duplicates at this point.
register_blockers_sets(Rest).
%%%%%%%%%%%%%%blocking_sets/3 14/04/11
%%%%%%%%%%%%%%%%%%%%%%%
/* blocking_sets(+Attackers_sets, +Blockers_sets, -Ordered_blockers) */
% Generates combinations of attackers and their blockers,
% (currently only naivley) ordered for damage assignment
%%%%%%%%%%%%%%blocking_sets/3 (0) 14/04/11
% No attackers were declared.
blocking_sets([], _Blockers_sets, []).
% ^ Well, if the first arg is [],
% then the second will be too so the next clause
% will never be checked
%%%%%%%%%%%%%%blocking_sets/3 (0) 14/04/11
% ^ No blockers were declared
blocking_sets(_Attackers_sets, [], []).
%%%%%%%%%%%%%%blocking_sets/3 (1) 14/04/11
% Find all possible ordered blocks sets
blocking_sets(Attackers_sets, Blockers_sets, Blocks):-
findall(Set,
(member( Attacker_set, Attackers_sets ),
member(Blocker_set, Blockers_sets),
permutation(Blockers, Blocker_set),
member(Attacker, Attacker_set),
append([Attacker], [Blockers], Set)),
% ^ Takes the default order of blockers
Sets),
sort(Sets, Blocks).
%%%%%%%%%%%%%%attacking_sets/3 14/04/11
%%%%%%%%%%%%%%%%%%%%%%%
attacking_sets([], Attacks, Attacks).
attacking_sets([Blocking_set | Blocks], Temp, Attacks):-
%member(Blocking_set, Blocks),
Blocking_set = [Attacker, Blockers],
findall( [ Blocker, [Attacker] ],
( member( Blocker, Blockers ),
Blocker \= [] ),
Orderings),
append(Temp, Orderings, New_temp),
attacking_sets(Blocks, New_temp, Attacks).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%value/5 13/04/11
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
/* value(+Us,+Them,+Deck_type,+Value_to_maximise,Value_to_minimise) */
% Evaluates a board position according to a deck type, ie, whether its
% strategic objective is card advantage (control) or tempo (beatdown)
% Combo decks are not currently supported.
%%%%%%%%%%%%%%value/5 (1) 13/04/11
% Our side plays control, we maximise our card advantage
% and minimise their tempo
value(Us, Them, 'Control', Our_card_advantage, Their_tempo):-
card_advantage(Us, Our_card_advantage, Them, _Their_advantage),
tempo(Us, _Tempo, Them, Their_tempo).
%%%%%%%%%%%%%%value/5 (1) 13/04/11
% Our side plays beatdown, we maximise our tempo
% and minimise their card advantage.
value(Us, Them, 'Beatdown', Our_tempo, Their_card_advantage):-
card_advantage(Us, _Our_card_Advantage, Them, Their_card_advantage),
tempo(Us, Our_tempo, Them, _Their_tempo).
%%%%%%%%%%%%%%card_advantage/4 13/04/11
%%%%%%%%%%%%%%%%%%%%%%%
/* card_advantage(+Player, -Advantage, +Opponent, -Advantage_0) */
% Evaluates each position's card advantage, ie counts cards in
% hand and the Battlefield.
card_advantage(Player, Advantage, Opponent, Advantage_0):-
findall(Object,
( zone(Player, Zone, Objects),
( Zone = 'Battlefield';
Zone = 'Hand'), % no other zones for now
member(Object, Objects) ),
Our_objects),
length(Our_objects, Advantage),
findall(Object,
( zone(Opponent, Zone, Objects),
( Zone = 'Battlefield';
Zone = 'Hand'),
member(Object, Objects) ),
Their_objects),
length(Their_objects, Advantage_0).
%%%%%%%%%%%%%%tempo/4 13/04/11
%%%%%%%%%%%%%%%%%%%%%%%
/* tempo(+Us, -Beats, +Them, -Their_beats) */
% Evaluates a side's tempo position, ie
% how close it is to ending the game.
% Beats: how many turns before the side loses,
% ie "beats to go". Think heartbeats.
tempo(_Us, Beats, Them, Their_beats):-
life_total(Them, 0),
deck_size(Them, Maximum1),
Beats = Maximum1,
Their_beats = 0.