-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackgammon.pl
1979 lines (1712 loc) · 52.4 KB
/
backgammon.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
:- use_module(library(pce)).
:-dynamic pieces/4. %pieces(Pos,Amount,Color,Top), Pos=Column,line ;Top=contains top circle in column
:-dynamic dice1/2. %dice1(Pic,Num) Num between 1 to 6
:-dynamic dice2/2. %dice2(Pic,Num)
:-dynamic piecepic/4. %piecepic(Circle,Height,Pos,Color)
:-dynamic marked/2. %marked(Circle,Amount)
:-dynamic turn/1. %turn(Turn) Turn=player,computer
:-dynamic double/2. %double(Flag,Flag), Flag=false,true
:-dynamic check/1. %check(Flag), Flag=false,true %checks if enters only 1 state in computer move
:-dynamic check1/1. %check1(Flag), Flag=false,true ;enters only one term in computer moves
:-dynamic check2/1. %check2(Flag), Flag=false,true ;checks if no move
:-dynamic check3/1. %check3(Flag), Flag=false,true ;if score to move is possible
:-dynamic check4/1. %check4(Flag), Flag=false,true ;for not clicking while no move
:-dynamic grade/3. %grade(Grade,Pos,Dice)
:-dynamic side/1. %side(Side), Side=right,left,random
:-dynamic colors/2. %colors(Color1,Color2)
%cube(Num,X,Y).
cube(1,120,325).
cube(2,230,325).
retractor:-
retractall(pieces(_,_,_,_)),
retractall(dice1(_,_)),
retractall(dice2(_,_)),
retractall(piecepic(_,_,_,_)),
retractall(marked(_,_)),
retractall(turn(_)),
retractall(check(_)),
retractall(check1(_)),
retractall(check2(_)),
retractall(check3(_)),
retractall(check4(_)),
retractall(double(_,_)),
retractall(grade(_,_,_)),
retractall(side(_)),
retractall(colors(_,_)).
%bmp(Pic, PicName).
bmp(b, 'C:\\Users\\User\\Desktop\\Backgammon\\bgs7_bitmaps\\board.bmp').
bmp(1, 'C:\\Users\\User\\Desktop\\Backgammon\\bgs7_bitmaps\\dice1.bmp').
bmp(2, 'C:\\Users\\User\\Desktop\\Backgammon\\bgs7_bitmaps\\dice2.bmp').
bmp(3, 'C:\\Users\\User\\Desktop\\Backgammon\\bgs7_bitmaps\\dice3.bmp').
bmp(4, 'C:\\Users\\User\\Desktop\\Backgammon\\bgs7_bitmaps\\dice4.bmp').
bmp(5, 'C:\\Users\\User\\Desktop\\Backgammon\\bgs7_bitmaps\\dice5.bmp').
bmp(6, 'C:\\Users\\User\\Desktop\\Backgammon\\bgs7_bitmaps\\dice6.bmp').
%lineX(Pos,X,Side).
lineX(1,710,right).
lineX(2,655,right).
lineX(3,600,right).
lineX(4,545,right).
lineX(5,490,right).
lineX(6,435,right).
lineX(7,312,right).
lineX(8,257,right).
lineX(9,202,right).
lineX(10,147,right).
lineX(11,92,right).
lineX(12,37,right).
lineX(13,37,right).
lineX(14,92,right).
lineX(15,147,right).
lineX(16,202,right).
lineX(17,257,right).
lineX(18,312,right).
lineX(19,435,right).
lineX(20,490,right).
lineX(21,545,right).
lineX(22,600,right).
lineX(23,655,right).
lineX(24,710,right).
lineX(25,380,right).
lineX(0,360,right).
lineX(1,37,left).
lineX(2,92,left).
lineX(3,147,left).
lineX(4,202,left).
lineX(5,257,left).
lineX(6,312,left).
lineX(7,435,left).
lineX(8,490,left).
lineX(9,545,left).
lineX(10,600,left).
lineX(11,655,left).
lineX(12,710,left).
lineX(13,710,left).
lineX(14,655,left).
lineX(15,600,left).
lineX(16,545,left).
lineX(17,490,left).
lineX(18,435,left).
lineX(19,312,left).
lineX(20,257,left).
lineX(21,202,left).
lineX(22,147,left).
lineX(23,92,left).
lineX(24,37,left).
lineX(25,360,left).
lineX(0,380,left).
%lineY(Pos,Y).
lineY(1,1,610).
lineY(1,2,555).
lineY(1,3,500).
lineY(1,4,445).
lineY(1,5,390).
lineY(1,6,380).
lineY(1,7,370).
lineY(1,8,360).
lineY(1,9,350).
lineY(1,10,340).
lineY(1,11,330).
lineY(1,12,320).
lineY(1,13,310).
lineY(1,14,300).
lineY(1,15,290).
lineY(2,1,37).
lineY(2,2,92).
lineY(2,3,147).
lineY(2,4,202).
lineY(2,5,257).
lineY(2,6,267).
lineY(2,7,277).
lineY(2,8,287).
lineY(2,9,297).
lineY(2,10,307).
lineY(2,11,317).
lineY(2,12,327).
lineY(2,13,337).
lineY(2,14,347).
lineY(2,15,357).
lineY(3,1,265).
lineY(3,2,275).
lineY(3,3,285).
lineY(3,4,295).
lineY(3,5,305).
lineY(3,6,315).
lineY(3,7,325).
lineY(3,8,335).
lineY(3,9,345).
lineY(3,10,355).
lineY(3,11,365).
lineY(3,12,375).
lineY(3,13,385).
lineY(3,14,395).
lineY(3,15,405).
game(Side,Colors):-
retractor,
%Close and destroy the existing window if it exists
( object(@window),
free(@window)
-> true
; true
),
new(@window,window('backgammon',size(800,730))),
send(@window,background,colour(white)),
bmp(b, BMP), %calling bitmap of board
new(Chessboard,bitmap(BMP)),
send(@window,display,Chessboard,point(0,0)),
side_decide(Side), %inserting side to dynamic
side(M), %calling it for circles positions
coloring(Colors), %inserting the colors to dynamic
colors(Color,EColor), %Color for player 1 and EColor for player 2
%starting positions of circles of the 2 players
new(B1,circle(55)),
send(B1,fill_pattern,colour(Color)),
((M=right, send(@window,display,B1,point(37,37));
(M=left, send(@window,display,B1,point(710,37))))),
new(B2,circle(55)),
send(B2,fill_pattern,colour(Color)),
((M=right, send(@window,display,B2,point(37,92));
(M=left, send(@window,display,B2,point(710,92))))),
new(B3,circle(55)),
send(B3,fill_pattern,colour(Color)),
((M=right, send(@window,display,B3,point(37,147));
(M=left, send(@window,display,B3,point(710,147))))),
new(B4,circle(55)),
send(B4,fill_pattern,colour(Color)),
((M=right, send(@window,display,B4,point(37,202));
(M=left, send(@window,display,B4,point(710,202))))),
new(B5,circle(55)),
send(B5,fill_pattern,colour(Color)),
((M=right, send(@window,display,B5,point(37,257));
(M=left, send(@window,display,B5,point(710,257))))),
new(W6,circle(55)),
send(W6,fill_pattern,colour(EColor)),
((M=right, send(@window,display,W6,point(257,37));
(M=left, send(@window,display,W6,point(490,37))))),
new(W7,circle(55)),
send(W7,fill_pattern,colour(EColor)),
((M=right, send(@window,display,W7,point(257,92));
(M=left, send(@window,display,W7,point(490,92))))),
new(W8,circle(55)),
send(W8,fill_pattern,colour(EColor)),
((M=right, send(@window,display,W8,point(257,147));
(M=left, send(@window,display,W8,point(490,147))))),
new(B6,circle(55)),
send(B6,fill_pattern,colour(Color)),
((M=right, send(@window,display,B6,point(257,610));
(M=left, send(@window,display,B6,point(490,610))))),
new(B7,circle(55)),
send(B7,fill_pattern,colour(Color)),
((M=right, send(@window,display,B7,point(257,555));
(M=left, send(@window,display,B7,point(490,555))))),
new(B8,circle(55)),
send(B8,fill_pattern,colour(Color)),
((M=right, send(@window,display,B8,point(257,500));
(M=left, send(@window,display,B8,point(490,500))))),
new(W1,circle(55)),
send(W1,fill_pattern,colour(EColor)),
((M=right, send(@window,display,W1,point(37,610));
(M=left, send(@window,display,W1,point(710,610))))),
new(W2,circle(55)),
send(W2,fill_pattern,colour(EColor)),
((M=right, send(@window,display,W2,point(37,555));
(M=left, send(@window,display,W2,point(710,555))))),
new(W3,circle(55)),
send(W3,fill_pattern,colour(EColor)),
((M=right, send(@window,display,W3,point(37,500));
(M=left, send(@window,display,W3,point(710,500))))),
new(W4,circle(55)),
send(W4,fill_pattern,colour(EColor)),
((M=right, send(@window,display,W4,point(37,445));
(M=left, send(@window,display,W4,point(710,445))))),
new(W5,circle(55)),
send(W5,fill_pattern,colour(EColor)),
((M=right, send(@window,display,W5,point(37,390));
(M=left, send(@window,display,W5,point(710,390))))),
new(B9,circle(55)),
send(B9,fill_pattern,colour(Color)),
((M=right, send(@window,display,B9,point(710,37));
(M=left, send(@window,display,B9,point(37,37))))),
new(B10,circle(55)),
send(B10,fill_pattern,colour(Color)),
((M=right, send(@window,display,B10,point(710,92));
(M=left, send(@window,display,B10,point(37,92))))),
new(W9,circle(55)),
send(W9,fill_pattern,colour(EColor)),
((M=right, send(@window,display,W9,point(710,610));
(M=left, send(@window,display,W9,point(37,610))))),
new(W10,circle(55)),
send(W10,fill_pattern,colour(EColor)),
((M=right, send(@window,display,W10,point(710,555));
(M=left, send(@window,display,W10,point(37,555))))),
new(B11,circle(55)),
send(B11,fill_pattern,colour(Color)),
((M=right, send(@window,display,B11,point(435,610));
(M=left, send(@window,display,B11,point(312,610))))),
new(B12,circle(55)),
send(B12,fill_pattern,colour(Color)),
((M=right, send(@window,display,B12,point(435,555));
(M=left, send(@window,display,B12,point(312,555))))),
new(B13,circle(55)),
send(B13,fill_pattern,colour(Color)),
((M=right, send(@window,display,B13,point(435,500));
(M=left, send(@window,display,B13,point(312,500))))),
new(B14,circle(55)),
send(B14,fill_pattern,colour(Color)),
((M=right, send(@window,display,B14,point(435,445));
(M=left, send(@window,display,B14,point(312,445))))),
new(B15,circle(55)),
send(B15,fill_pattern,colour(Color)),
((M=right, send(@window,display,B15,point(435,390));
(M=left, send(@window,display,B15,point(312,390))))),
new(W11,circle(55)),
send(W11,fill_pattern,colour(EColor)),
((M=right, send(@window,display,W11,point(435,37));
(M=left, send(@window,display,W11,point(312,37))))),
new(W12,circle(55)),
send(W12,fill_pattern,colour(EColor)),
((M=right, send(@window,display,W12,point(435,92));
(M=left, send(@window,display,W12,point(312,92))))),
new(W13,circle(55)),
send(W13,fill_pattern,colour(EColor)),
((M=right, send(@window,display,W13,point(435,147));
(M=left, send(@window,display,W13,point(312,147))))),
new(W14,circle(55)),
send(W14,fill_pattern,colour(EColor)),
((M=right, send(@window,display,W14,point(435,202));
(M=left, send(@window,display,W14,point(312,202))))),
new(W15,circle(55)),
send(W15,fill_pattern,colour(EColor)),
((M=right, send(@window,display,W15,point(435,257));
(M=left, send(@window,display,W15,point(312,257))))),
%asserting pieces to dynamic with their top circle of column
assert(pieces(1,2,EColor,W10)),
assert(pieces(6,5,Color,B15)),
assert(pieces(8,3,Color,B8)),
assert(pieces(12,5,EColor,W5)),
assert(pieces(13,5,Color,B5)),
assert(pieces(17,3,EColor,W8)),
assert(pieces(19,5,EColor,W15)),
assert(pieces(24,2,Color,B10)),
%asserting all piecepics and their positions in column
assert(piecepic(B1,1,13,Color)),
assert(piecepic(B2,2,13,Color)),
assert(piecepic(B3,3,13,Color)),
assert(piecepic(B4,4,13,Color)),
assert(piecepic(B5,5,13,Color)),
assert(piecepic(B6,1,8,Color)),
assert(piecepic(B7,2,8,Color)),
assert(piecepic(B8,3,8,Color)),
assert(piecepic(B9,1,24,Color)),
assert(piecepic(B10,2,24,Color)),
assert(piecepic(B11,1,6,Color)),
assert(piecepic(B12,2,6,Color)),
assert(piecepic(B13,3,6,Color)),
assert(piecepic(B14,4,6,Color)),
assert(piecepic(B15,5,6,Color)),
assert(piecepic(W1,1,12,EColor)),
assert(piecepic(W2,2,12,EColor)),
assert(piecepic(W3,3,12,EColor)),
assert(piecepic(W4,4,12,EColor)),
assert(piecepic(W5,5,12,EColor)),
assert(piecepic(W6,1,17,EColor)),
assert(piecepic(W7,2,17,EColor)),
assert(piecepic(W8,3,17,EColor)),
assert(piecepic(W9,1,1,EColor)),
assert(piecepic(W10,2,1,EColor)),
assert(piecepic(W11,1,19,EColor)),
assert(piecepic(W12,2,19,EColor)),
assert(piecepic(W13,3,19,EColor)),
assert(piecepic(W14,4,19,EColor)),
assert(piecepic(W15,5,19,EColor)),
%setting start values to the dynamics
assert(marked(_,0)),
assert(double(false,false)),
assert(check(false)),
assert(check1(false)),
assert(check2(true)),
assert(check3(false)),
assert(check4(false)),
assert(grade(-10000,-10000,-10000)),
new(Text,text('Start!')),
send(Text, font, font(times, bold, 36)),
send(Text, colour(white)),
send(@window,display,Text,point(140,320)),
send(Text,recogniser,click_gesture(left,'',single,message(@prolog,diceroll,Text))),
send(@window,open).
%rolling the dices
diceroll(Text):-
((dice1(L1,_), dice2(L2,_), free(L1), free(L2), retractall(dice1(_,_)), retractall(dice2(_,_)));true),
free(Text),
%rolling numbers of dices, E is Dice1 G is Dice2
random_between(1,6,E),
random_between(1,6,G),
write('Dices of who starts: ['), write(E:G), writeln(']'),
%keep rolling for the effect of rolling dices, rolling 5 times
keep_rolling,
keep_rolling,
keep_rolling,
keep_rolling,
keep_rolling,
roll(E,1),
roll(G,2),
starter.
%Player starts
starter:-
dice1(L1,E),
dice2(L2,G),E > G, new(Text1,text('Player starts!')),
send(Text1, font, font(times, bold, 18)),
send(@window,display,Text1,point(0,700)),
send(L1,recogniser,click_gesture(left,'',single,message(@prolog,makelist_of_circles,1))),
send(L2,recogniser,click_gesture(left,'',single,message(@prolog,makelist_of_circles,1))).
%Draw, reroll
starter:-
dice1(L1,E),
dice2(L2,G),
E is G,
new(Text2,text('Draw, reroll')),
send(Text2, font, font(times, bold, 18)),
send(@window,display,Text2,point(0,700)),
send(L1,recogniser,click_gesture(left,'',single,message(@prolog,diceroll,Text2))),
send(L2,recogniser,click_gesture(left,'',single,message(@prolog,diceroll,Text2))).
%Computer starts
starter:-
dice1(L1,E),
dice2(L2,G),
G > E, new(Text3,text('Computer starts!')),
send(Text3, font, font(times, bold, 18)),
send(@window,display,Text3,point(0,700)),
send(timer(0.5), delay),
free(L1), free(L2),
retractall(dice1(_,_)),
retractall(dice2(_,_)),
makelist_of_circles(2).
%roll(Num,Cube) R can be 1 or 2 depends on which dice
roll(N,R):-
bmp(N, BMP),
cube(R,X,Y),
(R is 1, new(Dice1,bitmap(BMP)),
send(@window,display,Dice1,point(X,Y)),
assert(dice1(Dice1,N));
(R is 2, new(Dice2,bitmap(BMP)),
send(@window,display,Dice2,point(X,Y)),
assert(dice2(Dice2,N)))).
%makelist_of_circles(R) R is the one who starts
makelist_of_circles(R):-
colors(Color,_),
findall(Circle1, piecepic(Circle1,_,_,Color), L1),
recogniser_pieces(L1),
((R is 1, moveU);(R is 2, moveComputer)).
%adding recogniser to player's pieces (circles)
recogniser_pieces([]).
recogniser_pieces([H|T]):-
send(H,recogniser,click_gesture(left,'',single,message(@prolog,mark,H))),
recogniser_pieces(T).
%player's turn
moveU:-
%reseting dynamics for player's turn
retractall(turn(_)),
assert(turn(player)),
double(false,false),
dice1(Dice1,_),
dice2(Dice2,_),
%removing dices pics and its former info
free(Dice1), free(Dice2),
retractall(dice1(_,_)),
retractall(dice2(_,_)),
retractall(marked(_,_)),
%asserting no circle has been marked for movement
assert(marked(_,0)),
%rolling numbers of dices, F is Dice1 D is Dice2
random_between(1,6,F),
random_between(1,6,D),
write('Player Dices: ['), write(F:D), writeln(']'),
keep_rolling,
keep_rolling,
keep_rolling,
keep_rolling,
keep_rolling,
roll(F,1),
roll(D,2),
dice1(_,Cube1),
dice2(_,Cube2),
%incase of double
((Cube1=Cube2, retractall(double(_,_)), assert(double(true,true)));
(Cube1\=Cube2)),
colors(Color,_),
%incase of no movement
findall(Pos, pieces(Pos,_,Color,_), L),
(((marked(_,0), not(isnt_at_home1(Color)),no_move_u(L,Cube1), no_move_u(L,Cube2), nomove, changing_turn_to_computer);
(isnt_at_home1(Color),is_there_move));true).
moveU.
%checking if all pieces in base of player
isnt_at_home1(Color):-
not(pieces(7,_,Color,_)),
not(pieces(8,_,Color,_)),
not(pieces(9,_,Color,_)),
not(pieces(10,_,Color,_)),
not(pieces(11,_,Color,_)),
not(pieces(12,_,Color,_)),
not(pieces(13,_,Color,_)),
not(pieces(14,_,Color,_)),
not(pieces(15,_,Color,_)),
not(pieces(16,_,Color,_)),
not(pieces(17,_,Color,_)),
not(pieces(18,_,Color,_)),
not(pieces(19,_,Color,_)),
not(pieces(20,_,Color,_)),
not(pieces(21,_,Color,_)),
not(pieces(22,_,Color,_)),
not(pieces(23,_,Color,_)),
not(pieces(24,_,Color,_)),
not(pieces(25,_,Color,_)).
%checking if all pieces in base of computer
isnt_at_home2(EColor):-
not(pieces(0,_,EColor,_)),
not(pieces(1,_,EColor,_)),
not(pieces(2,_,EColor,_)),
not(pieces(3,_,EColor,_)),
not(pieces(4,_,EColor,_)),
not(pieces(5,_,EColor,_)),
not(pieces(6,_,EColor,_)),
not(pieces(7,_,EColor,_)),
not(pieces(8,_,EColor,_)),
not(pieces(9,_,EColor,_)),
not(pieces(10,_,EColor,_)),
not(pieces(11,_,EColor,_)),
not(pieces(12,_,EColor,_)),
not(pieces(13,_,EColor,_)),
not(pieces(14,_,EColor,_)),
not(pieces(15,_,EColor,_)),
not(pieces(16,_,EColor,_)),
not(pieces(17,_,EColor,_)),
not(pieces(18,_,EColor,_)).
%no_move_u(List of pos,Cube), checks if player can move in his turn
no_move_u([],_).
no_move_u([H|L1],Cube):-
H1 is H-Cube,
colors(_,EColor),
((pieces(H1,X,EColor,_),
X>1,
H1>0);
(H1=<0)),
no_move_u(L1,Cube).
%mark(Top) Top circle in column so you can mark it for movement
mark(Top):-
check4(false), %inorder to block pressing circles while displaying no move msg
turn(player), %checking who's turn that is
pieces(25,_,_,Top), %25 is the captive piece of player so must choose that on
marked(_,0),
retractall(marked(_,_)),
assert(marked(Top,1)),
%asserting marked(Top Circle,Amount of marked circles)
send(Top,fill_pattern,colour(red)),
%checking player's movement with the 2 dices
((not(dice1(_,_)),
dice2(Dice2,Cube2),
Pos1 is 25-Cube2,
Pos2 is 25-Cube2);
(not(dice2(_,_)),
dice1(Dice1,Cube1),
Pos1 is 25-Cube1,
Pos2 is 25-Cube1);
(dice1(Dice1,Cube1),
dice2(Dice2,Cube2),
Pos1 is 25-Cube1,
Pos2 is 25-Cube2)),
colors(_,EColor),
%checks if there's column of computer's pieces
(are_there_any_pie(EColor,Pos1,2);
are_there_any_pie(EColor,Pos2,2)),
%reseting check2
retractall(check2(_)),
assert(check2(false)),
%clicking the dice you'd like for moving the marked circle piece
((dice1(_,_), send(Dice1,recogniser,click_gesture(left,'',single,message(@prolog,getNewPos,1))));true),
((dice2(_,_), send(Dice2,recogniser,click_gesture(left,'',single,message(@prolog,getNewPos,2))));true).
%for clicking circles that are not in top column
mark(Top):-
check4(false),
not(pieces(_,_,_,Top)).
mark(Top):-
check4(false),
not(pieces(25,_,_,_)), %no captive pieces
turn(player),
marked(_,0),
retractall(marked(_,_)),
colors(Color,_),
pieces(_,_,Color,Top), %checking if the selected circle is top circle in column from player
assert(marked(Top,1)), %asserting marked(Top Circle,Amount of marked circles)
send(Top,fill_pattern,colour(red)),
dice1(Dice1,_),
dice2(Dice2,_),
%clicking the dice you'd like for moving the marked circle piece
send(Dice1,recogniser,click_gesture(left,'',single,message(@prolog,getNewPos,1))),
send(Dice2,recogniser,click_gesture(left,'',single,message(@prolog,getNewPos,2))).
% click other top piece M instead of your previous Top, changing
% selected cirlce
mark(M):-
check4(false),
turn(player),
not(pieces(25,_,_,_)),
marked(Top,1),
M\=Top,
colors(Color,_),
send(Top,fill_pattern,colour(Color)),
pieces(_,_,Color,M),
retractall(marked(_,_)),
assert(marked(M,1)),
send(M,fill_pattern,colour(red)),
retractall(check2(_)),
assert(check2(false)).
%selected circle is captive and can not move
mark(Top):-
check4(false),
turn(player),
check2(true),
pieces(25,_,_,Top), %25 is captive line
nomove, %nomove msg
marked(Top,1),
colors(Color,_), %Color is player's color
send(Top,fill_pattern,colour(Color)),
%if you got any dice, remove them
((dice1(Dice1,_), free(Dice1), retractall(dice1(_,_)));true),
((dice2(Dice2,_), free(Dice2), retractall(dice2(_,_)));true),
retractall(double(_,_)),
assert(double(false,false)),
moveComputer.
%cant click on other circles that are captive
mark(Top):-
check4(false),
turn(player),
pieces(25,_,_,_),
marked(_,0),
pieces(25,_,_,M),
M\=Top.
mark(_).
%getNewPos(Dice)
%getting new pos for dice 1
getNewPos(1):-
dice1(Dice,Main),
(dice2(_,Second);true),
marked(Top,1),
colors(Color,_),
pieces(Pos,A,Color,Top),
Pos1 is Pos-Main,
(
%when not all pieces in base moves normally
((pieces(7,_,Color,_);
pieces(8,_,Color,_);
pieces(9,_,Color,_);
pieces(10,_,Color,_);
pieces(11,_,Color,_);
pieces(12,_,Color,_);
pieces(13,_,Color,_);
pieces(14,_,Color,_);
pieces(15,_,Color,_);
pieces(16,_,Color,_);
pieces(17,_,Color,_);
pieces(18,_,Color,_);
pieces(19,_,Color,_);
pieces(20,_,Color,_);
pieces(21,_,Color,_);
pieces(22,_,Color,_);
pieces(23,_,Color,_);
pieces(24,_,Color,_);
pieces(25,_,Color,_)),
((Pos1>=1, Pos1=<24,
possible(Top,A,Pos1,Pos,Main,Second,Dice,1));
(Pos1<1, send(Top,fill_pattern,colour(Color)),
retractall(marked(_,_)),
assert(marked(_,0)))));
(%when all pieces in base moving them out or closer to 1
not(pieces(7,_,Color,_)),
not(pieces(8,_,Color,_)),
not(pieces(9,_,Color,_)),
not(pieces(10,_,Color,_)),
not(pieces(11,_,Color,_)),
not(pieces(12,_,Color,_)),
not(pieces(13,_,Color,_)),
not(pieces(14,_,Color,_)),
not(pieces(15,_,Color,_)),
not(pieces(16,_,Color,_)),
not(pieces(17,_,Color,_)),
not(pieces(18,_,Color,_)),
not(pieces(19,_,Color,_)),
not(pieces(20,_,Color,_)),
not(pieces(21,_,Color,_)),
not(pieces(22,_,Color,_)),
not(pieces(23,_,Color,_)),
not(pieces(24,_,Color,_)),
not(pieces(25,_,Color,_)),
(%when new pos isn't getting them out, move normally
Pos1>0, possible(Top,A,Pos1,Pos,Main,Second,Dice,1));
(%when new pos is 0, that means getting them out from the column is correct
Pos1=0,
pieces(Main,A,_,Top),
NewA is A-1,
dice1(Dice1,_),
((double(true,T),
retractall(double(_,_)),
assert(double(false,T)));
(double(false,_),
free(Dice1),
retractall(dice1(_,_)))),
isEmpty(NewA,Main,Color),
free(Top),
retractall(piecepic(_,A,Main,_)),
retractall(marked(_,_)),
assert(marked(_,0)),
(win;is_there_move;moveComputer) );
(%when new pos is below 0, that means getting them out when you don't have higher columns of pieces
Pos1<0,
((pieces(Poss,A,_,Top),
findall(F, (pieces(F,_,Color,_),(F>Poss,F<7)), L1),
length(L1,Length),
Length is 0,
NewA is A-1,
dice1(Dice1,_),
((double(true,T),
retractall(double(_,_)),
assert(double(false,T)));
(double(false,_),
free(Dice1),
retractall(dice1(_,_)))),
isEmpty(NewA,Poss,Color),
free(Top),
retractall(piecepic(_,A,Poss,_)),
retractall(marked(_,_)),
assert(marked(_,0)),
(win;is_there_move;moveComputer));
(send(Top,fill_pattern,colour(Color)),
retractall(marked(_,_)),
assert(marked(_,0))))))).
%getting new pos for dice 2
getNewPos(2):-
dice2(Dice,Main),
(dice1(_,Second);true),
marked(Top,1),
colors(Color,_),
pieces(Pos,A,Color,Top),
Pos1 is Pos-Main,
(((pieces(7,_,Color,_);
pieces(8,_,Color,_);
pieces(9,_,Color,_);
pieces(10,_,Color,_);
pieces(11,_,Color,_);
pieces(12,_,Color,_);
pieces(13,_,Color,_);
pieces(14,_,Color,_);
pieces(15,_,Color,_);
pieces(16,_,Color,_);
pieces(17,_,Color,_);
pieces(18,_,Color,_);
pieces(19,_,Color,_);
pieces(20,_,Color,_);
pieces(21,_,Color,_);
pieces(22,_,Color,_);
pieces(23,_,Color,_);
pieces(24,_,Color,_);
pieces(25,_,Color,_)),
((Pos1>=1, Pos1=<24,
possible(Top,A,Pos1,Pos,Main,Second,Dice,2));
(Pos1<1, send(Top,fill_pattern,colour(Color)),
retractall(marked(_,_)),
assert(marked(_,0)))));
(not(pieces(7,_,Color,_)),
not(pieces(8,_,Color,_)),
not(pieces(9,_,Color,_)),
not(pieces(10,_,Color,_)),
not(pieces(11,_,Color,_)),
not(pieces(12,_,Color,_)),
not(pieces(13,_,Color,_)),
not(pieces(14,_,Color,_)),
not(pieces(15,_,Color,_)),
not(pieces(16,_,Color,_)),
not(pieces(17,_,Color,_)),
not(pieces(18,_,Color,_)),
not(pieces(19,_,Color,_)),
not(pieces(20,_,Color,_)),
not(pieces(21,_,Color,_)),
not(pieces(22,_,Color,_)),
not(pieces(23,_,Color,_)),
not(pieces(24,_,Color,_)),
not(pieces(25,_,Color,_)),
(Pos1>0,
possible(Top,A,Pos1,Pos,Main,Second,Dice,2));
(Pos1=0,
pieces(Main,A,_,Top),
dice2(Dice2,_),
((double(T,true),
retractall(double(_,_)),
assert(double(T,false)));
(double(_,false),
free(Dice2),
retractall(dice2(_,_)))),
NewA is A-1,
isEmpty(NewA,Main,Color),
free(Top),
retractall(piecepic(_,A,Main,_)),
retractall(marked(_,_)),
assert(marked(_,0)),
(win;is_there_move;moveComputer));
(Pos1<0,
((pieces(Poss,A,_,Top),
findall(F, (pieces(F,_,Color,_),F>Poss,F<7), L1),
length(L1,Length),
Length is 0,
dice2(Dice2,_),
((double(T,true),
retractall(double(_,_)),
assert(double(T,false)));
(double(_,false),
free(Dice2),
retractall(dice2(_,_)))),
free(Top),
NewA is A-1,
isEmpty(NewA,Poss,Color),
retractall(piecepic(_,A,Pos1,_)),
retractall(marked(_,_)),
assert(marked(_,0)),
(win;moveComputer));
(send(Top,fill_pattern,colour(Color)),
retractall(marked(_,_)),
assert(marked(_,0))))))).
getNewPos(_).
%win msg for player
win:-
colors(Color,EColor),
not(pieces(_,_,Color,_)),
new(Win,text('Player wins!')),
send(Win,colour(Color)),
send(Win, font, font(times, bold, 36)),
send(@window,display,Win,point(100,330)),
((dice1(Dice1,_),
free(Dice1), retractall(dice1(_,_)));true),
((dice2(Dice2,_),
free(Dice2), retractall(dice2(_,_)));true),
writeln('win'),
((pieces(P,_,EColor,_), (P=0;P=1;P=2;P=3;P=4;P=5;P=6), write('turkish '));true),
(((not(isnt_at_home2(EColor))), writeln('march'));true),
play_again(Color).
%lose msg for player
lose:-
colors(Color,EColor),
not(pieces(_,_,EColor,_)),
new(Lose,text('Computer wins!')),
send(Lose,colour(EColor)),
send(Lose, font, font(times, bold, 36)),
send(@window,display,Lose,point(100,330)),
dice1(Dice1,_),
dice2(Dice2,_),
free(Dice1), free(Dice2),
retractall(dice1(_,_)), retractall(dice2(_,_)),
writeln('lose'),
((pieces(P,_,Color,_), (P=25;P=24;P=23;P=22;P=21;P=20;P=19), write('turkish '));true),
(((not(isnt_at_home1(Color))), writeln('march'));true),
play_again(EColor).
play_again(C):-
new(Again,text('Again?')),
send(Again,colour(C)),
send(Again, font, font(times, bold, 26)),
send(@window,display,Again,point(100,400)),
send(Again,recogniser,click_gesture(left,'',single,message(@prolog,again))),
new(Exit,text('Exit?')),
send(Exit,colour(C)),
send(Exit, font, font(times, bold, 26)),
send(@window,display,Exit,point(220,400)),
send(Exit,recogniser,click_gesture(left,'',single,message(@prolog,exit))).
again:-
send(@window,destroy),
settings.
exit:-
retractor,
send(@window,destroy).
% updown(Pos,Amount,Pixel)
%checks if your new pos in the upper or lower part of the board
updown(Pos,A,Pixel):-
Pos>=1, Pos=<12, lineY(1,A,Pixel).
updown(Pos,A,Pixel):-
Pos>=13, Pos=<24, lineY(2,A,Pixel).
%captive piece in middle
updown(Pos,A,Pixel):-
(Pos=25;Pos=0), lineY(3,A,Pixel).
%possible(Top Circle,Amount,Pos,Cube,Other_Cube,Dice Pic,Num)
%possible movement for dice 1
possible(Top,A,Pos1,Pos,Cube,Cube2,Dice,1):-
dice1(Dice,Cube),
colors(Color,EColor),
((Cube=Cube2,
((not(pieces(Pos1,X,EColor,_)));
(pieces(Pos1,1,EColor,_))),
double(T1,T2),
((T1=false,
free(Dice),
retractall(dice1(_,_)));
(T1=true,retractall(double(_,_)),
assert(double(false,T2)))));true),
(%when player moves to empty column
(check1(false),not(pieces(Pos1,_,EColor,_)), not(pieces(Pos1,_,Color,_)),
assert(pieces(Pos1,1,Color,Top)),
NewA is A-1,
isEmpty(NewA,Pos,Color),
retractall(piecepic(_,A,Pos,_)),
assert(piecepic(Top,1,Pos1,Color)),
side(M), lineX(Pos1,X,M), updown(Pos1,1,Y),
send(Top,x(X)),
send(Top,y(Y)),
write('Pl moving: '), writeln(Pos+Cube=Pos1),
((Cube\=Cube2,
free(Dice),
retractall(dice1(_,_)));true) ,
retractall(check1(_)),
assert(check1(true)),
retractall(marked(_,_)),
assert(marked(_,0))
);
%when player enters his column
(check1(false),not(pieces(Pos1,_,EColor,_)), pieces(Pos1,B,Color,_),
NewB is B+1,
NewA is A-1,
retractall(pieces(Pos1,_,_,_)),
assert(pieces(Pos1,NewB,Color,Top)),
isEmpty(NewA,Pos,Color),
retractall(piecepic(_,A,Pos,_)),
((not(piecepic(Top,_,Pos1,Color)),assert(piecepic(Top,NewB,Pos1,Color)));true),
side(M), lineX(Pos1,X,M), updown(Pos1,NewB,Y),
send(Top,x(X)),
send(Top,y(Y)),
write('Pl moving: '), writeln(Pos+Cube=Pos1),
((Cube\=Cube2,
free(Dice),
retractall(dice1(_,_)));true) ,
retractall(marked(_,_)),
assert(marked(_,0))
);
(%when player eats computer's piece
pieces(Pos1,1,EColor,_),
piecepic(WPart,1,Pos1,EColor),
retractall(pieces(Pos1,_,_,_)),
assert(pieces(Pos1,1,Color,Top)),
((not(pieces(0,_,_,_)),