-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsourceProlog.pl
More file actions
1005 lines (828 loc) · 28.7 KB
/
Copy pathsourceProlog.pl
File metadata and controls
1005 lines (828 loc) · 28.7 KB
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
/*
The following conventions are used in this program...
Single letter variables represent:
L - a list
N - a number, position, index, or counter
V - a value (usually a string)
A - an accumulator
H - the head of a list
T - the tail of a list
For this implementation, these single letter variables represent:
P - a player number (1 or 2)
B - the board (a 9 item list representing a 3x3 matrix)
each "square" on the board can contain one of 3 values: x ,o, or e (for empty)
S - the number of a square on the board (1 - 9)
M - a mark on a square (x or o)
E - the mark used to represent an empty square ('e').
U - the utility value of a board position
R - a random number
D - the depth of the minimax search tree (for outputting utility values, and for debugging)
Variables with a numeric suffix represent a variable based on another variable.
(e.g. B2 is a new board position based on B)
For predicates, the last variable is usually the "return" value.
(e.g. opponent_mark(P,M), returns the opposing mark in variable M)
Predicates with a numeric suffix represent a "nested" predicate.
e.g. myrule2(...) is meant to be called from myrule(...)
and myrule3(...) is meant to be called from myrule2(...)
There are only two assertions that are used in this implementation
asserta( board(B) ) - the current board
asserta( player(P, Type) ) - indicates which players are human/computer.
*/
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% MODULES
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
:- use_module(library(pce)).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% FACTS
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
next_player(1, 2). %%% determines the next player after the given player
next_player(2, 1).
inverse_mark('x', 'o'). %%% determines the opposite of the given mark
inverse_mark('o', 'x').
player_mark(1, 'x'). %%% the mark for the given player
player_mark(2, 'o').
opponent_mark(1, 'o'). %%% shorthand for the inverse mark of the given player
opponent_mark(2, 'x').
blank_mark('e'). %%% the mark used in an empty square
maximizing('x'). %%% the player playing x is always trying to maximize the utility of the board position
minimizing('o'). %%% the player playing o is always trying to minimize the utility of the board position
score_board([
[3, 4, 5, 5, 4, 3],
[4, 6, 8, 8, 6, 4],
[5, 8, 10, 10, 8, 5],
[7, 10, 13, 13, 10, 7],
[5, 8, 10, 10, 6, 4],
[4, 6, 8, 8, 6, 4],
[3, 4, 5, 5, 4, 3]
]).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% MAIN PROGRAM
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%.......................................
% Initialization & manage I/O
%.......................................
run :-
hello, %%% Display welcome message, initialize game
board(B), %%% Get the current board
create_board_window(W, B), % Create the board window
play(1, W), %%% Play the game starting with player 1
goodbye. %%% Display end of game message
run :-
goodbye.
hello :-
initialize,
nl, nl,
write('Welcome to Connect4IF.'),
read_players,
output_players.
initialize :-
blank_mark(E),
asserta(
board([
[E, E, E, E, E, E],
[E, E, E, E, E, E],
[E, E, E, E, E, E],
[E, E, E, E, E, E],
[E, E, E, E, E, E],
[E, E, E, E, E, E],
[E, E, E, E, E, E]
])
). %%% create a blank board
goodbye :-
board(B),
nl, nl,
write('Game over: '),
output_winner(B),
retract(board(_)),
retract(player(_,_,_)),
read_play_again(V), !,
(V == 'y'), !,
run.
% l'ordinateur lit les prédicats dans l'ordre. Quand il rencontre un ! il s'arrête et passe au prédicat suivant
read_play_again(V) :-
nl, nl,
write('Play again (y/n)? '),
read(V),
(V == 'y' ; V == 'n'), !.
read_play_again(V) :-
nl, nl,
write('Please enter y or n.'),
read_play_again(V). %appelle le premier prédicat qui a ce nom
read_players :-
nl, nl,
write('Number of human players? '),
read(N),
set_players(N).
set_players(0) :-
write('What AI do you want for the first AI ? (1, 2, 3, 4)\n'),
write('1: Random AI\n'),
write('2: Weighted-grid AI\n'),
write('3: Offensive AI\n'),
write('4: Defensive AI\n'),
read(AI1),
write('What AI do you want for the second AI ? (1, 2, 3, 4)\n'),
write('1: Random AI\n'),
write('2: Weighted-grid AI\n'),
write('3: Offensive AI\n'),
write('4: Defensive AI\n'),
read(AI2),
asserta( player(1, computer, AI1) ),
asserta( player(2, computer, AI2) ), !.
set_players(1) :-
nl,
write('Is human playing x or o (x moves first)? \n'),
read(M),
write('What AI do you want to play against? (1, 2, 3, 4)\n'),
write('1: Random AI\n'),
write('2: Weighted-grid AI\n'),
write('3: Offensive AI\n'),
write('4: Defensive AI\n'),
read(AI),
write('AI: '), write(AI),
human_playing(M, AI), !.
set_players(2) :-
asserta( player(1, human, 0) ),
asserta( player(2, human, 0) ), !.
set_players(_) :-
nl,
write('Please enter 0, 1, or 2.'),
read_players.
human_playing(M, AI) :-
(M == 'x'),
asserta( player(1, human, 0) ),
asserta( player(2, computer, AI) ), !.
human_playing(M, AI) :-
(M == 'o'),
asserta( player(1, computer, AI) ),
asserta( player(2, human, 0) ), !.
human_playing(_) :-
nl,
write('Please enter x or o.'),
set_players(1).
%.......................................
% play
%.......................................
% main function
play(P, W) :-
board(B), !,
output_board(B), !,
update_board(W, B), !,
not(game_over(P, B)), !,
make_move(P, B), !,
next_player(P, P2), !,
play(P2, W), !
.
%.......................................
% make_move
%.......................................
% Requests the next move from human/computer,
% then applies that move to the given board.
% Vérifie si une ligne ne contient pas 'e'
row_full([]).
row_full([H|T]) :- H \= 'e', row_full(T).
% Vérifie si tout le plateau est rempli (aucune ligne ne contient 'e')
board_full([]).
board_full([Row|Rest]) :- row_full(Row), board_full(Rest).
make_move(P, B) :-
player(P, Type, AI),
make_move2(Type, P, B, B2), % Delegate the move based on player type (human or computer).
retract(board(_)), % Update the current board.
asserta(board(B2)).
% Human player makes a move.
make_move2(human, P, B, B2) :-
nl, nl,
write('Player '), write(P), write(' move? '),
read(S), % Read the square index from the player.
moves(B, AvailableMoves), % Get the list of available moves.
member(S, AvailableMoves), % Check if the selected square is valid.
player_mark(P, M),
move(B, S, M, B2), !. % Apply the move to the board and cuts to prevent backtracking
% Handle invalid move by human player.
make_move2(human, P, B, B2) :-
nl, nl,
write('Invalid move. Please select a valid, empty column.'),
make_move2(human, P, B, B2). % Retry until the human makes a valid move.
% Computer player makes a move using minimax algorithm.
make_move2(computer, P, B, B2) :-
nl, nl,
write('Computer is thinking about its next move...'),
player_mark(P, M),
player(P,Z,IA),
% write(IA),
jeu_IA(IA, B, M, S, U),
write('Computer places '), write(M),
move(B, S, M, B2),
write(' in column '), write(S), write('.'),
nl, nl,
write('Computer places '), write(M),
write(' in column '), write(S), write('.').
jeu_IA(1, B, M, S, U):-
random_ia(B,S). %version 1: l'odinateur joue au hasard
jeu_IA(2, B, M, S, U):-
computer_best_score_move(B,S).
jeu_IA(_, B, M, S, U):-
minimax(0, B, M, S, U). %version 3 ou 4: l'ordinateur joue avec minimax
%.......................................
% moves
%.......................................
% Retrieves a list of available moves (empty squares) on the board.
moves(B, L) :-
\+ win(B, x), % If either player has already won, there are no valid moves.
\+ win(B, o),
blank_mark(E),
findall(N, rectangle(B,N,E), L), % Find all empty squares in the top row of each column.
L \= []. % Ensure the list of moves is not empty.
%.......................................
% rectangle
%.......................................
% Retrieves all the top values of the columns in a given board that match M.
rectangle(B, Col, M) :-
nth1(Col, B, Column), % Get the column at position Col.
nth1(6, Column, M). % Check the topmost value of the column.
%.......................................
% win
%.......................................
isconsecutive(List, Player, Length) :-
length(SubList, Length),
append(_, SubListRest, List),
append(SubList, _, SubListRest),
maplist(=(Player), SubList).
transpose([], []).
transpose([[] | _], []).
transpose(Matrix, [Row | TransposedTail]) :-
extract_column(Matrix, Row, RestMatrix),
transpose(RestMatrix, TransposedTail).
extract_column([], [], []).
extract_column([[H | T] | Rows], [H | Column], [T | RestRows]) :-
extract_column(Rows, Column, RestRows).
horizontal_win(B, M) :-
member(Row, B),
isconsecutive(Row, M, 4).
vertical_win(B, M) :-
transpose(B, TBoard),
member(Row, TBoard),
isconsecutive(Row, M, 4).
diagonal_win_check(B, M) :-
length(B, Rows),
length(B, Cols),
RowStartMax is Rows - 3,
ColStartMax is Cols - 3,
between(0, RowStartMax, RowStart),
between(0, ColStartMax, ColStart),
diagonal_win_from(B, RowStart, ColStart, M).
diagonal_win_check(B, M) :-
length(B, Rows),
length(B, Cols),
RowStartMax is Rows - 3,
ColStartMax is Cols - 3,
between(0, RowStartMax, RowStart),
ColEndMax is Cols - 1,
between(0, ColEndMax, ColEnd),
diagonal_win_from_reverse(B, RowStart, ColEnd, M).
diagonal_win_from(B, RowStart, ColStart, M) :-
nth0(RowStart, B, Row1),
nth0(ColStart, Row1, M),
Row2Start is RowStart + 1, Col2Start is ColStart + 1,
nth0(Row2Start, B, Row2),
nth0(Col2Start, Row2, M),
Row3Start is Row2Start + 1, Col3Start is Col2Start + 1,
nth0(Row3Start, B, Row3),
nth0(Col3Start, Row3, M),
Row4Start is Row3Start + 1, Col4Start is Col3Start + 1,
nth0(Row4Start, B, Row4),
nth0(Col4Start, Row4, M).
diagonal_win_from_reverse(B, RowStart, ColEnd, M) :-
nth0(RowStart, B, Row1),
nth0(ColEnd, Row1, M),
Row2Start is RowStart + 1, Col2Start is ColEnd - 1,
nth0(Row2Start, B, Row2),
nth0(Col2Start, Row2, M),
Row3Start is Row2Start + 1, Col3Start is Col2Start - 1,
nth0(Row3Start, B, Row3),
nth0(Col3Start, Row3, M),
Row4Start is Row3Start + 1, Col4Start is Col3Start - 1,
nth0(Row4Start, B, Row4),
nth0(Col4Start, Row4, M).
win(B, M) :-
horizontal_win(B, M);
vertical_win(B, M);
diagonal_win_check(B, M).
%.......................................
% game_over
%.......................................
% determines when the game is over
game_over(P, B) :-
game_over2(P, B).
% game is over if opponent wins
game_over2(P, B) :-
opponent_mark(P, M),
win(B, M).
% game is over if there are no blank squares left
game_over2(P, B) :-
moves(B, L),
% write(L),nl,
L == [].
%.......................................
% AI random
%.......................................
% The randomIA algorithm plays.
random_ia(B, S):-
random_int_1n(7,S), % Read the square index from the player.
moves(B, AvailableMoves), % Get the list of available moves.
member(S, AvailableMoves),
!. % Check if the selected square is valid.
% Handle invalid move by computer.
random_ia(B, S):-
nl, nl,
write('Invalid random number. New try.'),
random_ia(B, S). % Retry until the coputer makes a valid move.
%.......................................
% AI 2
% Computer plays best score
% On donne des scores aux cases pour savoir si elles offrent une grande possibilité de coups gagnants
% Peu efficace car très déterministe -> un peu mieux que l'aléatoire '
%.......................................
%
computer_best_score_move(B,S):-
moves(B, AvailableMoves), % Get the list of available moves.
get_list_scores(AvailableMoves, L),
maximumListe(L,Max), !, % find the maximum score in the list
nth1(Imax,L,Max), !, % find the index of the maximum score in the list
nth1(Imax,AvailableMoves,S) % find the best move
.
get_list_scores([],[]).
get_list_scores([Available_T|Available_Q], [Score|L]):-
get_list_scores(Available_Q, L),
calculer_score_location(Available_T, Score) % Pour chaque elemennt de AvailableMoves on renvoie le score associé à l'emplacement et on le stocke dans la liste L '
.
calculer_score_location(Available_T, Score):-
score_board(SB),
board(B),
nth1(Available_T, B, ColB), % Get the column number Available_T of the board
nth1(Available_T, SB, ColSB), % Get the column number Available_T of the score_board
nth1(I,ColB,e), !, % Get the first empty mark of the column ColB of the mark
nth1(I,ColSB,Score) % Get the score of the empty mark found above
.
%.......................................
% evaluation with two combination
%.......................................
% tools necessary to determine the evaluation of a given board position
%
horizontal_evaluation(Board, Comb, U, NewU) :-
findall(_, (member(Row, Board), append([_, Comb, _], Row)), Matches),
length(Matches, Count),
NewU is U + Count
.
vertical_evaluation(Board, Comb, U, NewU) :-
transpose(Board, TBoard),
findall(_, (member(Row, TBoard), append([_, Comb, _], Row)), Matches),
length(Matches, Count),
NewU is U + Count
.
diagonal_evaluation(Board, Comb, U, NewU) :-
diagonals(Board, Diags),
findall(_, (member(Diag, Diags), append([_, Comb, _], Diag)), Matches),
length(Matches, Count),
NewU is U + Count
.
% Récupération des diagonales d'une grille
diagonals(Board, Diags) :-
findall(Diag, diagonal(Board, Diag), Diags).
diagonal(Board, Diag) :-
length(Board, N),
between(0, N, K),
findall(Elem, (
between(0, N, I),
J is I + K,
nth0(I, Board, Row),
nth0(J, Row, Elem)
), Diag),
Diag \= [].
diagonal(Board, Diag) :-
length(Board, N),
between(0, N, K),
findall(Elem, (
between(0, N, I),
J is I - K,
nth0(I, Board, Row),
nth0(J, Row, Elem)
), Diag),
Diag \= [].
% .......................................
% evaluation compter les doubles et triples
% .......................................
% determines the value of a given board position
%
combinationX([ ['x','x','x'],
['x','x'] ]).
combinationO([ ['o','o','o'],
['o','o'] ]).
evaluate([],B).
evaluate([H|T],B) :-
% write('debut evaluation'),
valeurU(U1),
horizontal_evaluation(B,H,U1,NewU1),
vertical_evaluation(B,H,NewU1,NewU2),
diagonal_evaluation(B,H,NewU1,NewU3),
retract(valeurU(_)),
asserta(valeurU(NewU3)),
evaluate(T,B)
.
%.......................................
% defensive_evaluation
%.......................................
% Évalue un plateau en se basant sur une approche défensive.
% Valorise les positions qui bloquent les opportunités de l'adversaire.
% Détecte une combinaison dangereuse pour l'adversaire
combinationDanger(['e', 'e', M, M], M).
combinationDanger([M, M, 'e', 'e'], M).
combinationDanger(['e', M, M, 'e'], M).
combinationDanger([ M, 'e', M, 'e'], M).
combinationDanger(['e', M, 'e', M], M).
combinationDanger([M, 'e', 'e', M], M).
combinationDanger([M, M, M, 'e'], M).
combinationDanger(['e', M, M, M], M).
combinationDanger([M, 'e', M, M], M).
combinationDanger([M, M, 'e', M], M).
% Évaluation horizontale défensive
defensive_horizontal_evaluation(Board, Opponent, U, NewU) :-
findall(_, (
member(Row, Board),
append([_, Comb, _], Row),
combinationDanger(Comb, Opponent)
), Matches),
length(Matches, Count),
NewU is U + Count * 10.
% Évaluation verticale défensive
defensive_vertical_evaluation(Board, Opponent, U, NewU) :-
transpose(Board, TBoard),
defensive_horizontal_evaluation(TBoard, Opponent, U, NewU).
% Évaluation diagonale défensive
defensive_diagonal_evaluation(Board, Opponent, U, NewU) :-
findall(_, (
diagonals(Board, Diags),
member(Diag, Diags),
append([_, Comb, _], Diag),
combinationDanger(Comb, Opponent)
), Matches),
length(Matches, Count),
NewU is U + Count * 10.
% Évaluation globale défensive
defensive_evaluation(Board, U, M) :-
inverse_mark(M, Opponent),
U2 = 0,
defensive_horizontal_evaluation(Board, Opponent, U2, NewU1),
defensive_vertical_evaluation(Board, Opponent, NewU1, NewU),
defensive_diagonal_evaluation(Board, Opponent, NewU, U).
%.......................................
% utility
%.......................................
% determines the value of a given board position
%
utility(B,U,M,AI) :-
win(B,'x'),
U = 1000000,
!
.
utility(B,U,M,AI) :-
win(B,'o'),
U = (-1000000),
!
.
utility(B,U,M,AI) :-
board_full(B),
U = 0,
!
.
utility(B,U,'x',3) :-
asserta(valeurU(0)),
% write('utility 1'),nl,
retract(valeurU(_)),
asserta(valeurU(0)),
combinationX(C),
evaluate(C,B),
valeurU(U1),
U = U1,
!
.
utility(B,U,'o',3) :-
asserta(valeurU(0)),
% write('utility 2 first'),nl,
retract(valeurU(_)),
% write('utility 2'),nl,
asserta(valeurU(0)),
% write('utility 2'),nl,
combinationO(C),
% write(C),
evaluate(C,B),
valeurU(U1),
U = -U1,
!
.
utility(B, U, M, 4) :-
defensive_evaluation(B, U, M).
%.......................................
% minimax
%.......................................
% The minimax algorithm always assumes an optimal opponent.
% The best opening move is always dead center.
minimax(D,[ [E,E,E,E,E,E],
[E,E,E,E,E,E],
[E,E,E,E,E,E],
[E,E,E,E,E,E],
[E,E,E,E,E,E],
[E,E,E,E,E,E],
[E,E,E,E,E,E] ],
M,S,U) :-
blank_mark(E),
S = 4, !.
minimax(D,B,M,S,U) :-
% write('minimax 2'),nl,
D2 is D + 1,
moves(B,L), !, %%% get the list of available moves
best(D2,B,M,L,S,U), !. %%% recursively determine the best available move
% if there are no more available moves,
% then the minimax value is the utility of the given board position
minimax(D,B,M,S,U) :-
player(A, computer, AI),
utility(B,U,M,AI).
%.......................................
% best
%.......................................
% determines the best move in a given list of moves by recursively calling minimax
%
% if max depth is reached
% if there is only one move left in the list...
best(4,B,M,[S1],S,U) :-
% write('best 1'),nl,
move(B,S1,M,B2), %%% apply that move to the board,
inverse_mark(M,M2),
!,
player(A, computer, AI),
utility(B2,U,M2,AI), %%% then search for the utility value of that move.
S = S1, !,
% output_value(D,S,U),
!
.
% if there is more than one move in the list...
best(4,B,M,[S1|T],S,U) :-
% write('best 2'),nl,
move(B,S1,M,B2), %%% apply the first move (in the list) to the board,
inverse_mark(M,M2),
!,
player(A, computer, AI),
utility(B2,U1,M2,AI), %%% recursively search for the utility value of that move,
best(4,B,M,T,S2,U2), %%% determine the best move of the remaining moves,
% output_value(D,S1,U1),
better(D,M,S1,U1,S2,U2,S,U) %%% and choose the better of the two moves (based on their respective utility values)
.
% if there is only one move left in the list...
best(D,B,M,[S1],S,U) :-
% write('best 3'),nl,
move(B,S1,M,B2), %%% apply that move to the board,
inverse_mark(M,M2),
!,
minimax(D,B2,M2,_S,U), %%% then recursively search for the utility value of that move.
S = S1, !,
% output_value(D,S,U),
!
.
% if there is more than one move in the list...
best(D,B,M,[S1|T],S,U) :-
% write('best 4'),nl,
move(B,S1,M,B2), %%% apply the first move (in the list) to the board,
% write(B),nl,nl,
inverse_mark(M,M2),
!,
minimax(D,B2,M2,_S,U1), %%% recursively search for the utility value of that move,
best(D,B,M,T,S2,U2), %%% determine the best move of the remaining moves,
% output_value(D,S1,U1),
better(D,M,S1,U1,S2,U2,S,U) %%% and choose the better of the two moves (based on their respective utility values)
.
%.......................................
% better
%.......................................
% returns the better of two moves based on their respective utility values.
%
% if both moves have the same utility value, then one is chosen at random.
better(D,M,S1,U1,S2,U2, S,U) :-
maximizing(M), %%% if the player is maximizing
U1 > U2, %%% then greater is better.
S = S1,
U = U1,
!
.
better(D,M,S1,U1,S2,U2, S,U) :-
minimizing(M), %%% if the player is minimizing,
U1 < U2, %%% then lesser is better.
S = S1,
U = U1,
!
.
better(D,M,S1,U1,S2,U2, S,U) :-
U1 == U2, %%% if moves have equal utility,
random_int_1n(10,R), %%% then pick one of them at random
better2(D,R,M,S1,U1,S2,U2,S,U),
!
.
better(D,M,S1,U1,S2,U2, S,U) :- %%% otherwise, second move is better
S = S2,
U = U2,
!
.
%.......................................
% better2
%.......................................
% randomly selects two squares of the same utility value given a single probability
%
better2(D,R,M,S1,U1,S2,U2, S,U) :-
R < 6,
S = S1,
U = U1,
!
.
better2(D,R,M,S1,U1,S2,U2, S,U) :-
S = S2,
U = U2,
!
.
%.......................................
% move
%.......................................
% applies a move on the given board
% (put mark M in column S on board B and return the resulting board B2)
move(B, ColNum, M, Newboard) :-
nth1(ColNum, B, Col), % Get the `ColNum`th column from the board.
place_in_column(Col, M, NewCol), % Place the mark `M` in the column.
replace_column(B, ColNum, NewCol, Newboard). % Update the board with the new column.
% Place in column - places the mark `M` in the first empty slot in the column.
place_in_column([E|Rest], M, [M|Rest]) :- % If the head is empty, place the mark `M`.
E == 'e', !. % Only replace empty slot ('e').
place_in_column([H|T], M, [H|NewT]) :- % Otherwise, recursively check the tail.
place_in_column(T, M, NewT).
% replaces an entier column in a board
replace_column([_|T], 1, NewCol, [NewCol|T]) :- !. % Replace the first column (Nth = 1).
replace_column([H|T], N, NewCol, [H|NewT]) :-
N > 1,
N1 is N - 1,
replace_column(T, N1, NewCol, NewT). % Recurse to find the correct column.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% OUTPUT
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
output_players :-
write('Player 1: '),
player(1, Type1, AI1),
write(Type1), write(' '), write(AI1), nl,
write('Player 2: '),
player(2, Type2, AI2),
write(Type2), write(' '), write(AI2), nl.
output_winner(B) :-
win(B,x),
write('X wins.'), !.
output_winner(B) :-
win(B,o),
write('O wins.'), !.
output_winner(_) :-
write('No winner.').
% Affiche le plateau de jeu
output_board(B) :-
nl,
output_rows(B, 6), % On commence par la 6ème ligne (le bas) sinon ça s'affiche à l'envert
output_column_numbers, % Affiche les numéros de colonnes
nl.
% Affiche les lignes du bas vers le haut
output_rows(_, 0) :- !.
output_rows(B, Row) :-
output_row(B, Row),
NextRow is Row - 1,
output_rows(B, NextRow).
% Affiche une ligne donnée
output_row(B, Row) :-
write('|'),
output_cells(B, Row, 1).
% Parcourt les colonnes et affiche la cellule correspondante
output_cells(_, _, 8) :-
nl, !.
output_cells(B, Row, Col) :-
nth1(Col, B, Column), % Récupère la colonne actuelle
nth1(Row, Column, Cell), % Récupère la cellule (contenu) à la ligne Row
output_square(Cell), % Affiche le contenu de la cellule
write('|'),
NextCol is Col + 1,
output_cells(B, Row, NextCol).
% Affiche une cellule (X, O, ou vide)
% Si la cellule est vide, affiche un espace
% Sinon, si la cellule est un X, affiche en rouge
% Sinon, si la cellule est un O, affiche en bleu
output_square(X) :-
X == x, !,
ansi_format([fg(red)], 'x', []). % Affiche un X en rouge
output_square(O) :-
O == o, !,
ansi_format([fg(blue)], 'o', []). % Affiche un O en bleu
output_square(E) :-
E == e, !,
write(' '). % Affiche un espace
% Affiche les numéros des colonnes (1 à 7)
output_column_numbers :-
write('---------------'), nl,
write(' 1 2 3 4 5 6 7'), nl.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% GUI
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
create_menu_window :-
new(W, dialog('Connect4IF')),
send(W, size, size(550, 150)),
add_colored_circles(W, 50),
send(W, background, colour(black)),
send(W, append, new(Titre, text('Connect4IF'))),
send(Titre, font, font(helvetica, bold, 26)),
send(Titre, colour, colour(white)),
send(W, append, new(Button1, button('Start', message(@prolog, start_game, W)))),
send(W, append, new(Button4, button('Quit', message(W, destroy)))),
% Ouvrir la fenêtre
send(W, open).
start_game(W) :-
send(W, destroy),
run.
% Ajouter des cercles alternant entre rouge et bleu
add_colored_circles(W, N) :-
add_colored_circles(W, N, 0).
add_colored_circles(_, 0, _) :- !. % Arrêter quand N atteint 0
add_colored_circles(W, N, Counter) :-
RandomX is random(550), % Générer une position X aléatoire
RandomY is random(150), % Générer une position Y aléatoire
send(W, display, new(Circle, circle(10))), % Créer un cercle de rayon 10
% Alterner les couleurs entre rouge et bleu
( 0 is Counter mod 2
-> send(Circle, fill_pattern, colour(red)) % Rouge pour les cercles pairs
; send(Circle, fill_pattern, colour(blue)) % Bleu pour les cercles impairs
),
send(Circle, move, point(RandomX, RandomY)), % Déplacer à la position aléatoire
NewCounter is Counter + 1,
NewN is N - 1,
add_colored_circles(W, NewN, NewCounter). % Rappel pour ajouter le prochain cercle
% Crée une fenêtre pour afficher le plateau
create_board_window(W, B) :-
new(W, picture('Connect4IF')),
send(W, size, size(355, 305)),
send(W, open),
send(W, background, colour(black)),
draw_board(W, B, 6, 7).
% Dessine le plateau sur la fenêtre
draw_board(W, B, Rows, Cols) :-
draw_cells(W, B, Rows, Cols, Rows, 1).
% Parcourt les cellules pour les dessiner
draw_cells(_, _, 0, _, _, _) :- !.
draw_cells(W, B, Row, Cols, TotalRows, TotalCols) :-
draw_row(W, B, Row, Cols, TotalRows, TotalCols),
NextRow is Row - 1,
draw_cells(W, B, NextRow, Cols, TotalRows, TotalCols).
% Dessine une ligne donnée
draw_row(_, _, _, 0, _, _) :- !.
draw_row(W, B, Row, Col, TotalRows, TotalCols) :-
nth1(Col, B, Column),
nth1(Row, Column, M),
CellY is (TotalRows - Row) * 50 + 15,
CellX is (Col - 1) * 50 + 15,
draw_cell(W, M, CellX, CellY),
NextCol is Col - 1,
draw_row(W, B, Row, NextCol, TotalRows, TotalCols).
% Dessine une cellule donnée
draw_cell(W, M, X, Y) :-
( M == x
-> new(Circle, circle(40)),
send(Circle, fill_pattern, colour(red)),
send(W, display, Circle, point(X, Y))
; M == o
-> new(Circle, circle(40)),
send(Circle, fill_pattern, colour(blue)),
send(W, display, Circle, point(X, Y))
; new(Box, box(40, 40)),
send(Box, fill_pattern, colour(grey20)),
send(W, display, Box, point(X, Y))
).
update_board(W, B) :-
send(W, clear),
draw_board(W, B, 6, 7),
send(W, expose).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% UTILS
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%.......................................
% generate random number for the AI to play
% random_int_1n
%.......................................
% returns a random integer from 1 to N
% eg : random_int_1n(7,R).
random_int_1n(N, V) :-
V is random(N) + 1, !.
%.......................................
% find the maximum value in a list
%.......................................
maximumListe([X],X). %vrai si X est le seul element de la liste
maximumListe([T|Q],X):-maximumListe(Q,M), (M<T -> X=T ; X=M). %X est assigné au plus grand des deux (à chaque fois)