-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.pl
1785 lines (1644 loc) · 110 KB
/
game.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
/*
* Author: Shaked Manes
* Purpose: Game Project for MAMAN 17
* Date: 31/10/2020
*
* This program was developed using SWI-Prolog, so it depends
* on the SWI-Prolog Prolog implementation standards.
*
* Also note that this programs requires SWI-Prolog font size of 36
* to run properly.
*/
/** Modules **/
:- use_module(library(random)).
:- use_module('game.config').
:- use_module('game.utils').
/** Dynamic predicates **/
:- dynamic(game_state/1).
:- dynamic(game_board/1).
:- dynamic(game_boneyard/1).
:- dynamic(game_boneyard_count/1).
:- dynamic(game_result/1).
:- dynamic(curr_player_turn/1).
:- dynamic(last_tile_right/1).
:- dynamic(last_tile_left/1).
:- dynamic(player_identity/2).
:- dynamic(player_hand/2).
:- dynamic(player_hand_count/2).
:- dynamic(player_hand_weight/2).
:- dynamic(player_decision_matrix/2).
:- dynamic(computer_tiles_visible/1).
/** Game Flow & Logic Predicates **/
% Start game predicate, used for starting the game.
start:-
see(user),
tell(user),
cleanup,
cut_wrapper(print_open_game_message),
cut_wrapper(set_default_settings),
cut_wrapper(open_game_menu),
seen,
told.
% Open game menu, which shows the main options for the game.
open_game_menu:-
cut_wrapper(print_message('')),
cut_wrapper(print_main_options),
cut_wrapper(get_user_selection(1-4, Selection)),
cut_wrapper(run_menu_selection(Selection)).
% Perform game main menu option selection.
run_menu_selection(Selection):-
(
(
Selection == 1,
start_new_game,
open_game_menu
)
;
(
Selection == 2,
print_instructions,
open_game_menu
)
;
(
Selection == 3,
enter_settings,
open_game_menu
)
;
(
Selection == 4,
exit_game
)
).
% Enter the settings screen and control the selection made by the user.
enter_settings:-
print_settings_screen,
control_settings.
% Control the settings options given for the user in the main section
% of the settings screen.
control_settings:-
print_settings_main_choices,
get_user_selection(1-4, Selection),
run_settings_main_selection(Selection).
% Run the settings selection operation by the selected option by the
% user.
run_settings_main_selection(Selection):-
(
(
Selection == 1,
enter_player_selection(player_one),
enter_settings
)
;
(
Selection == 2,
enter_player_selection(player_two),
enter_settings
)
;
(
Selection == 3,
toggle_computer_tiles_visible,
enter_settings
)
;
(
print_message('Exiting Settings'),
print_message('')
)
).
% Select a particular player type for a given player by the user.
% Prints all the player types and set the player type selected by the
% user.
enter_player_selection(Player):-
print_message_without_nl('Please, select one of the below options for '),
displayable_player(Player, DisplayPlayer),
print_message_without_nl(DisplayPlayer),
print_message(':'),
print_player_options,
get_user_selection(1-5, Selection),
control_player_selection(Player, Selection).
% Control the selection option for a player selected by the user for a
% given player type.
control_player_selection(Player, Selection):-
(
(
Selection == 1,
set_player_identity(Player, user_player)
)
;
(
Selection == 2,
set_player_identity(Player, computer_random)
)
;
(
Selection == 3,
set_player_identity(Player, computer_greedy)
)
;
(
Selection == 4,
set_player_identity(Player, computer_statistical)
)
;
(
print_message('Exit player selection.'),
print_message('')
)
).
% Exits the game by printing exit game and perform cleanup.
exit_game:-
cleanup,
print_exit_game.
% Starts a brand new game between 2 players.
start_new_game:-
cleanup_game_states,
set_game_state(in_game),
prepare_player(player_one),
prepare_player(player_two),
print_message('Starting new game...'),
sleep(2),
print_message('Shuffling bone tiles...'),
sleep(2),
generate_boneyard(GeneratedBoneyard),
set_game_boneyard(GeneratedBoneyard),
set_game_boneyard_count(28),
pick_tiles_from_boneyard(7, PlayerOnePickedTiles),
pick_tiles_from_boneyard(7, PlayerTwoPickedTiles),
print_message('Drawing bones for each player...'),
sleep(2),
print_message('Player 1 Bones:'),
print_player_full_hand(player_one, PlayerOnePickedTiles),
print_message(''),
print_message('Player 2 Bones:'),
print_player_full_hand(player_two, PlayerTwoPickedTiles),
print_message(''),
assert(player_hand(player_one, PlayerOnePickedTiles)),
assert(player_hand(player_two, PlayerTwoPickedTiles)),
assert(player_hand_count(player_one, 7)),
assert(player_hand_count(player_two, 7)),
calculate_and_set_player_hand_weight(player_one, PlayerOnePickedTiles),
calculate_and_set_player_hand_weight(player_two, PlayerTwoPickedTiles),
sleep(10),
determine_starter_player(
PlayerOnePickedTiles, PlayerTwoPickedTiles, PlayerStarter, MaxTile
),
play_automatic_starter_player(PlayerStarter, MaxTile),
update_player_decision_matrix(player_one, MaxTile),
update_player_decision_matrix(player_two, MaxTile),
sleep(3),
print_message('Organizing game table...'),
sleep(3),
start_game_loop.
/**
* determine_starter_player(
* PlayerOnePickedTiles,
* PlayerTwoPickedTiles,
* PlayerStarter,
* MaxTile
* ):-
*
* Determine between the two players, who will start the game by
* the max tiles in both players hands.
*
* INPUT:
* PlayerOnePickedTiles - Player one hand tiles.
* PlayerTwoPickedTiles - Player two hand tiles.
*
* OUTPUT:
* PlayerStarter - The player which start the game between the two.
* MaxTile - The max tile found in the players hands.
*/
determine_starter_player(
PlayerOnePickedTiles,
PlayerTwoPickedTiles,
PlayerStarter,
MaxTile
):-
print_message('Determining first turn...'),
sleep(1),
get_starter_player_and_tile(
PlayerOnePickedTiles,
PlayerTwoPickedTiles,
MaxTile,
PlayerStarter
),
sleep(1),
print_message('Max tile found: '),
print_visible_tile(MaxTile),
print_message(''),
sleep(1),
print_message('The player which starts the game is: '),
print_player_desc(PlayerStarter),
print_message(''),
set_current_turn(PlayerStarter).
% Estimates the bigger tile between the two tiles given.
% The predicate unify only if the first tile is the maximum between the
% two.
estimate_bigger_tile(
bone(BiggerLeft, BiggerRight),
bone(LowerLeft, LowerRight)
):-
(
(
BiggerLeft == BiggerRight,
(
(
LowerLeft == LowerRight,
BiggerLeft >= LowerLeft
);
LowerLeft \== LowerRight
)
)
;
(
BiggerLeft \== BiggerRight,
LowerLeft \== LowerRight,
BiggerSum is BiggerLeft + BiggerRight,
LowerSum is LowerRight + LowerLeft,
BiggerSum >= LowerSum
)
).
% Getting the maximum of player hand (list of tile), using
% estimate_bigger_tile which unify if the first tile is bigger than the
% other.
get_max_tile_of_player([Tile], Tile):- !.
get_max_tile_of_player([Tile | RestPlayerHand], MaxTile):-
get_max_tile_of_player(RestPlayerHand, RestMaxTile),
!,
(
(
estimate_bigger_tile(Tile, RestMaxTile),
MaxTile = Tile
)
;
(
MaxTile = RestMaxTile
)
).
% Gets the max tile between the two players and the player which
% should start to play (The player which has the max tile in his hand,
% should place it in the game board).
% Starter values are player_one/player_two.
get_starter_player_and_tile(PlayerOneTiles, PlayerTwoTiles, MaxTile, Starter):-
get_max_tile_of_player(PlayerOneTiles, PlayerOneMaxTile),
get_max_tile_of_player(PlayerTwoTiles, PlayerTwoMaxTile),
(
(
estimate_bigger_tile(PlayerOneMaxTile, PlayerTwoMaxTile),
MaxTile = PlayerOneMaxTile,
Starter = player_one
)
;
(
MaxTile = PlayerTwoMaxTile,
Starter = player_two
)
).
% Play automatically for starter player.
% Basically put the tile on the game board and move the turn to the next
% player.
play_automatic_starter_player(PlayerStarter, MaxTile):-
print_message(''),
print_message_without_nl('Playing automatically for starter player: '),
print_player_desc(PlayerStarter),
print_message(''),
sleep(5),
print_player_desc(PlayerStarter),
print_message_without_nl(' Put the tile '),
print_board_tile(MaxTile),
print_message_without_nl(' on the board'),
print_message(''),
assert(game_board([MaxTile | T1]-T1)),
assert(last_tile_left(MaxTile)),
assert(last_tile_right(MaxTile)),
remove_tile_from_player_hand(PlayerStarter, MaxTile),
set_next_turn.
% Main game loop - controls the game loop which manages the game.
start_game_loop:-
cut_wrapper(print_game_screen),
cut_wrapper(play_player_turn),
cut_wrapper(set_next_turn),
cut_wrapper(update_game_state),
cut_wrapper(continue_game_loop).
% Play player turn for each player identity.
% If the player is a user, asking him for a possible move.
% If the player is a computer, calling for the computer move predicate.
play_player_turn:-
curr_player_turn(CurrentPlayer),
player_identity(CurrentPlayer, PlayerIdentity),
print_message_without_nl('Current turn: '),
print_player_desc(CurrentPlayer),
print_message(''),
(
(
PlayerIdentity == user_player,
!,
print_message('Calculating possible moves...'),
sleep(3),
ask_user_play(CurrentPlayer)
)
;
(
!,
perform_computer_play(CurrentPlayer)
)
).
% Handles the game loop, due to the current game state.
continue_game_loop:-
game_state(CurrentGameState),
(
(
CurrentGameState == in_game,
start_game_loop
)
;
(
CurrentGameState == end_game,
print_game_result
)
).
% Updating current game state due to the last play performed.
update_game_state:-
player_hand_count(player_one, PlayerOneCount),
player_hand_count(player_two, PlayerTwoCount),
(
(
PlayerOneCount == 0,
!,
set_game_state(end_game),
set_game_result(player_one_win)
)
;
(
PlayerTwoCount == 0,
!,
set_game_state(end_game),
set_game_result(player_two_win)
)
;
(
check_possible_tie(IsTie),
!,
IsTie == yes,
(
player_hand_weight(player_one, PlayerOneWeight),
player_hand_weight(player_two, PlayerTwoWeight),
(
(
PlayerOneWeight > PlayerTwoWeight,
GameResult = player_two_win
)
;
(
PlayerTwoWeight > PlayerOneWeight,
GameResult = player_one_win
)
;
(
GameResult = tie
)
)
),
set_game_state(end_game),
set_game_result(GameResult)
)
;
(
true
)
).
% Checking possible tie option between the players and return if it is
% really a tie.
check_possible_tie(IsTie):-
player_hand(player_one, PlayerOneHand),
player_hand(player_two, PlayerTwoHand),
get_appendable_tiles(PlayerOneHand, PlayerOneMoves),
get_appendable_tiles(PlayerTwoHand, PlayerTwoMoves),
game_boneyard_count(BoneyardCount),
(
(
PlayerOneMoves == [],
PlayerTwoMoves == [],
BoneyardCount == 0,
IsTie = yes
)
;
(
IsTie = no
)
).
% Asking the user for playing selection or automatic draw from boneyard.
ask_user_play(Player):-
player_hand(Player, PlayerHand),
get_appendable_tiles(PlayerHand, PossibleMoves),
(
(
PossibleMoves == [],
!,
show_automatic_draw_boneyard(Player)
)
;
(
!,
print_message('Please select one of the following options to play: '),
show_possible_moves_selection(PossibleMoves, 1-Range),
get_user_selection(1-Range, Selection),
nth1(Selection, PossibleMoves, Move),
perform_user_play(Player, Move)
)
).
/**
* show_automatic_draw_boneyard(Player):-
* Shows automtatic draw from boneyard scenario for a given
* player.
*
* INPUT:
* Player - The player to perform on automatic
* drawing from boneyard.
*/
show_automatic_draw_boneyard(Player):-
print_message_without_nl('Sorry, no possible moves found for '),
print_player_desc(Player),
print_message(''),
print_message('Automatically drawing bone from boneyard...'),
sleep(5),
pick_tiles_from_boneyard(1, [Tile]),
add_tile_to_player_hand(Player, Tile).
/**
* perform_user_play(Player, PossibleMove):-
* Perform a possible move by a given player.
*
* INPUT:
* Player - The player which plays the given move.
* PossibleMove - The move which the player will play.
*/
perform_user_play(Player, possible_move(bone(LeftValue, RightValue), Side, Reversed)):-
(
(
Reversed == yes,
!,
ParsedTile = bone(RightValue, LeftValue)
)
;
(
!,
ParsedTile = bone(LeftValue, RightValue)
)
),
print_selection_performed(Player, ParsedTile, Side),
game_board(Board),
append_tile_on_board(Board, ParsedTile, Side, _),
remove_tile_from_player_hand(Player, bone(LeftValue, RightValue)),
update_player_decision_matrix(player_one, ParsedTile),
update_player_decision_matrix(player_two, ParsedTile).
/**
* perform_computer_play(Player):-
* Perform a computer play move.
*
* INPUT:
* Player - The actual player which plays as computer.
*/
perform_computer_play(Player):-
player_hand(Player, PlayerHand),
get_appendable_tiles(PlayerHand, PossibleMoves),
print_message_without_nl('Player '),
print_player_desc(Player),
print_message(' is playing...'),
sleep(3),
(
(
PossibleMoves == [],
!,
show_automatic_draw_boneyard(Player)
)
;
(
!,
perform_specific_computer_move(Player, PossibleMoves)
)
).
/**
* perform_specific_computer_move(Player, PossibleMoves):-
* Perform a specific computer move, by a given player and
* possible moves.
*
* INPUT:
* Player - the player which is the computer player.
* PossibleMoves - List possible moves for the given player.
*/
perform_specific_computer_move(Player, PossibleMoves):-
player_identity(Player, PlayerIdentity),
(
(
PlayerIdentity == computer_random,
!,
computer_random_move(Player, PossibleMoves)
)
;
(
PlayerIdentity == computer_greedy,
!,
computer_greedy_move(Player, PossibleMoves)
)
;
(
PlayerIdentity == computer_statistical,
!,
computer_statistical_move(Player, PossibleMoves)
)
).
/**
* computer_statistical_move(Player, PossibleMoves):-
* Playing a computer statistical move.
*
* The computer statistical bot, plays by the following strategy:
*
* First, he sorts all his possible moves by the weight
* of each bone tile.
* He does that because in situation of tie, the player with
* the lowest weight of tiles is winning.
*
* Second, he sorts his probability row in his
* decision matrix for getting indication
* of which of the bone tiles has the lowest probability to be
* drawn by the other player.
*
* He does that because the decision matrix holds in the last row
* a number which indicates the number of time a number in bone
* tile is showed in the game. (Each row indicates a number in a tile)
*
* Then, he choose the possible move which has the
* higher possible probability to now be drawn from
* boneyard, and in the same time the most weighted
* bone tile (If possible).
*
* INPUT:
* Player - The player which plays the computer statistical move.
* PossibleMoves - List possible moves of the player.
*
*/
computer_statistical_move(Player, PossibleMoves):-
matrix_dimensions(RowSize, _),
ActualRowIndex is RowSize - 1,
player_decision_matrix(Player, DecisionMatrix),
get_row_from_decision_matrix(ActualRowIndex, DecisionMatrix, ProbabilityRow),
get_best_move(ProbabilityRow, PossibleMoves, BestMove),
perform_user_play(Player, BestMove).
/**
* computer_random_move(Player, PossibleMoves):-
* Playing a computer random move.
*
* The computer random bot, playes by the following strategy:
*
* He choose a random selection from his possible moves, and
* plays it.
*
* INPUT:
* Player - The player which plays the computer random move.
* PossibleMoves - List possible moves of the player.
*/
computer_random_move(Player, PossibleMoves):-
list_length(PossibleMoves, Range),
random_between(1, Range, RandomSelection),
nth1(RandomSelection, PossibleMoves, Move),
%perform_user_play(Player, PossibleMoves, RandomSelection).
perform_user_play(Player, Move).
/**
* computer_greedy_move(Player, PossibleMoves):-
* Playing a computer greedy move.
*
* The computer greedy bot, plays by the following strategy:
*
* He choose the possible move with the maximum weight and
* plays with it.
*
* INPUT:
* Player - The player which plays the computer greedy move.
* PossibleMoves - List possible moves of the player.
*/
computer_greedy_move(Player, PossibleMoves):-
max_in_list(weight_estimator, PossibleMoves, MaxWeightMove),
perform_user_play(Player, MaxWeightMove).
/**
* get_best_move(ProbabilityRow, PossibleMoves, BestMove):-
* Gets the best move a computer statistical bot can perform.
*
* Takes the probability row of the player,
* Sorts it and the possible moves of the player
* (In descending order),
* Chooses the best move by backtracking on the both
* sorted lists.
*
* INPUT:
* ProbabilityRow - The probability row of the player.
* PossibleMoves - List of possible moves of the player.
*
* OUTPUT:
* BestMove - The best move the player can perform.
*/
get_best_move(ProbabilityRow, PossibleMoves, BestMove):-
probability_parser(ProbabilityRow, ParsedProbabilityRow),
merge_sort(weight_estimator, PossibleMoves, SortedPossibleMoves),
merge_sort(probability_estimator, ParsedProbabilityRow, SortedProbabilityRow),
cut_wrapper(find_best_probability_move(SortedProbabilityRow, SortedPossibleMoves, BestMove)).
/**
* find_best_probability_move(ProbRow, PosMoves, Move):-
* Find the best probability move by sorted probability row
* and sorted possible moves lists.
*
* INPUT:
* ProbRow - The probability row of the player.
* PosMoves - List of possible moves of the player.
*
* OUTPUT:
* Move - The best move the player can perform.
*/
find_best_probability_move(ProbRow, PosMoves, Move):-
(
member((_, Index), ProbRow),
member(possible_move(bone(Left, Right), Side, Reversed), PosMoves),
(
(
Left == Index,
(
(
Side == left,
Reversed == no
)
;
(
Side == right,
Reversed == yes
)
)
)
;
(
Right == Index,
(
(
Side == right,
Reversed == no
)
;
(
Side == left,
Reversed == yes
)
)
)
)
),
Move = possible_move(bone(Left, Right), Side, Reversed).
/**
* probability_parser(ProbabilityRow, ParsedProbabilityRow):-
* Parses a probability row to a list of pairs, which
* each pair is in the following format:
* (Value, Index):
* Value - The value in the probability row.
* Index - The row index in the decision matrix.
*
* INPUT:
* ProbabilityRow - The probability row to parse.
*
* OUTPUT:
* ParsedProbabilityRow - The parsed probability row.
*/
probability_parser(ProbabilityRow, ParsedProbabilityRow):-
probability_parser(ProbabilityRow, 0, ParsedProbabilityRow).
/**
* probability_parser(ProbabilityRow, CurrIndex, ParsedProbabilityRow):-
* Helper for the `probability_parser` predicate.
*
* INPUT:
* ProbabilityRow - The probability row to parse.
* CurrIndex - The current index in the probability row.
*
* OUTPUT:
* ParsedProbabilityRow - The parsed probability row.
*/
% When there's only one value, the parsed list will be the pair of the
% value and the current index.
probability_parser([Value], Index, [(Value, Index)]):- !.
% Set each probability value, with it corresponding index in the parsed
% probability row.
probability_parser(
[ProbabilityValue | RestRow],
Index,
[(ProbabilityValue, Index) | RestParsedRow]
):-
!,
NextIndex is Index + 1,
probability_parser(RestRow, NextIndex, RestParsedRow).
/**
* weight_estimator(PossibleMove1, PossibleMove2, MaxPossibleMove):-
* Weight estimator for the merge sort to unify the
* 'greather-than' condition to sorts the possible moves lists.
*
* It uses the weight of the bone as the estimator.
* The possible move with the bone with the higher weight will be the
* greather possible move.
*
* INPUT:
* PossibleMove1 - A possible move.
* PossibleMove2 - Another possible move
*
* OUTPUT:
* MaxPossibleMove - The maximum possible move between the two.
*/
weight_estimator(
possible_move(bone(FirstLeft, FirstRight), FirstSide, FirstReversed),
possible_move(bone(SecondLeft, SecondRight), SecondSide, SecondReversed),
MaxPossibleMove
):-
FirstSum is FirstLeft + FirstRight,
SecondSum is SecondLeft + SecondRight,
(
(
FirstSum >= SecondSum,
MaxPossibleMove = possible_move(bone(FirstLeft, FirstRight), FirstSide, FirstReversed)
)
;
(
SecondSum > FirstSum,
MaxPossibleMove = possible_move(bone(SecondLeft, SecondRight), SecondSide, SecondReversed)
)
).
/**
* probability_estimator(
* (Value1, Index1),
* (Value2, Index2),
* (MaxValue, MaxIndex)
* ):-
* Probability estimator for the probability row elements to unify
* the 'greather-than' condition to sorts the probability
* row elements.
*
* It uses the value of each row index as the estimator.
* The higher pair of of Value & Index will be the greather
* pair.
*
* INPUT:
* (Value1, Index1) - A pair of value and index.
* (Value2, Index2) - Another pair of value and index.
*
* OUTPUT:
* (MaxValue, MaxValue) - The max pair between the two.
*
*/
probability_estimator(
(FirstValue, FirstIndex),
(SecondValue, SecondIndex),
(MaxValue, MaxIndex)
):-
(
(
FirstValue >= SecondValue,
MaxValue = FirstValue,
MaxIndex = FirstIndex
)
;
(
MaxValue = SecondValue,
MaxIndex = SecondIndex
)
).
% Removes given tile from a given player's hand.
remove_tile_from_player_hand(Player, Tile):-
player_hand(Player, PlayerHand),
remove_element_from_list(Tile, PlayerHand, NewPlayerHand),
retractall(player_hand(Player, _)),
assert(player_hand(Player, NewPlayerHand)),
change_player_hand_count(Player, -1),
recalculate_and_set_player_hand_weight(Player, Tile, remove).
% Adds given tile to a given player's hand.
add_tile_to_player_hand(Player, Tile):-
player_hand(Player, PlayerHand),
append([Tile], PlayerHand, NewPlayerHand),
retractall(player_hand(Player, _)),
assert(player_hand(Player, NewPlayerHand)),
change_player_hand_count(Player, 1),
recalculate_and_set_player_hand_weight(Player, Tile, add).
/**
* get_appendable_tiles(Tiles, AppendableTiles):-
* For a given tiles list, return the possible
* appendable tiles to the game board by a possible_move
* predicate which structured in the following manner:
*
* possible_move(Tile, Side, Reversed):
* Tile - The bone tile which is part of the possible move.
* Side - The side which the bone can be placed in.
* Reversed - Indicates if the bone need to be in reverse
* Order or not, in the possible move.
*
* INPUT:
* Tiles - A list of bone tiles.
*
* OUTPUT:
* AppendableTiles - List of appendable tiles structured as
* possible_move predicate which indicates
* for the tiles given, which tile can be
* placed in the current game board, and how.
*/
get_appendable_tiles(Tiles, AppendableTiles):-
(
setof(
possible_move(Tile, Side, Reversed),
(
member(Tile, Tiles),
check_appendable_tile(Tile, Side, Reversed, Validity),
Validity \== novalid
),
AppendableTiles
)
; % No one of the tiles given can be placed in the board.
AppendableTiles = []
).
/**
* check_appendable_tile(Tile, Side, Reversed, Validity):-
* Checking if a bone can be placed in the game board, and how.
*
* INPUT:
* Tile - The bone tile to check if it can be placed in
* the game board.
*
* OUTPUT:
* Side - Which side the tile can be placed in the game board.
* Values are: left/right.
* Reversed - Indicates if the tile need to be placed in
* reverse order.
* Values are: yes/no.
* Validity - Indicates if the bone can be actually placed
* in the game board.
* Values are: valid/novalid.
*/
check_appendable_tile(Tile, Side, Reversed, Validity):-
% Getting the last tile placed in the game board.
get_last_tiles(bone(LeftTileLeftValue, _), bone(_, RightTileRightValue)),
Tile = bone(CheckLeftValue, CheckRightValue),
(
(
% If the left value and the right value of the tile
% are equals, than the bone is not needed to be reversed.
CheckLeftValue =:= CheckRightValue,
!,
Reversed = no,
(
(
% Current side is right, and it equals to the
% number in the tile.
Side = right,
RightTileRightValue =:= CheckRightValue,
Validity = valid
);
(
% Current side is left, and it equals to the
% number in the tile.
Side = left,
LeftTileLeftValue =:= CheckLeftValue,
Validity = valid
);
(
% The tile not matched any of the numbers
% in the board last tiles.
% The bone cannot be placed in the game board.
Side = uninitialized,
Validity = novalid
)
)
);
(
(
% Regular bone, current side is right
Side = right,
(
(
% Last tile right value is the same as
% the current tile left value.
% The tile can be placed in the board at
% right side regulary.
RightTileRightValue =:= CheckLeftValue,
Reversed = no,
Validity = valid
);
(
% Last tile right value is the same as
% the current tile right value.
% The tile can be placed in the board at
% right side but in reverse order.
RightTileRightValue =:= CheckRightValue,
Reversed = yes,
Validity = valid
)
)
);
(
% Regular bone, current side is left
Side = left,
(
(
% Last tile left value is the same as
% the current tile right value.