-
Notifications
You must be signed in to change notification settings - Fork 0
/
bots.pl
162 lines (115 loc) · 4.69 KB
/
bots.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
:- use_module(library(lists)).
:- use_module(library(random)).
dynamic(nextPlayer/2).
type_of_player(1, human).
type_of_player(2, noobBot).
type_of_player(3, proBot).
is_bot(noobBot).
is_bot(proBot).
% Define the predicate define_players/2, which defines the player as a human or a bot
define_players(Player1, Player2):-
assert(nextPlayer(Player1, Player2)),
assert(nextPlayer(Player2, Player3)).
% HELPER
% Define the predicate longer_list/2, which takes a list of lists and a result list as arguments
longest_list(List, Longest) :-
longest_list(List, [], Longest).
longest_list([], Longest, Longest).
longest_list([H|T], Current, Longest) :-
( length(H, L),
length(Current, CL),
L > CL
-> longest_list(T, H, Longest)
; longest_list(T, Current, Longest)
).
head_of_list([X|_], X).
% HELPER
% Define the predicate choose_move/5, which takes a board, a position and a color as arguments
% - Board: the current board
% - Pos: the position of the piece to move
% - Color: the color of the piece to move
% - Bot: the bot to play
% - Move: the move to be made
chooseMove(Board, Pos, Color, noobBot, Move):-
valid_moves(Board, Pos, Moves, Color),
write('Moves available for Noob:'), write(Moves), nl,
\+length(Moves, 0),
random_member(Move, Moves),
write('he chooses:'), write(Move), nl.
chooseMove(Board, Pos, Color, proBot, Move):-
once((valid_chains(Board, Pos, Captures, Color),
write('Chains available for Pro:'), write(Captures), nl,
\+length(Captures, 0),
head_of_list(Captures, Head),
\+length(Head, 0),
longest_list(Captures, Move),
write('he chooses:'), write(Move), nl)).
chooseMove(Board, Pos, Color, proBot, Move):-
write('Entred here'),
valid_moves(Board, Pos, [Move| Tail], Color),
write('Moves available for Pro:'), write(Captures), nl,
\+length([Move| Tail], 0),
write('he chooses:'), write(Move), nl.
% Define the predicate find_pieces/3, which takes a board and a color and returns a list of positions of pieces of that color
% - Board: the current board
% - Color: the color of the pieces to find
% - Pieces: the list of positions of pieces of that color
find_pieces(Board, Color, Pieces):-
findall(Result, pos(Board, Result, Color), Pieces),
write('Pieces: '), write(Pieces), nl.
% Define the predicate all_best_moves/4, which takes a board, a color and a bot and returns the list of the best moves of each piece of that color
% - Board: the current board
% - Color: the color of the pieces to move
% - Bot: the bot to play
% - Moves: the list of the best moves of each piece of that color
allBestMoves(Board, Color, Bot, Moves):-
find_pieces(Board, Color, Pieces),
allbestMovesAux(Board, Pieces, Color, Bot, Moves).
allbestMovesAux(_, [], _, _, []):- !.
allbestMovesAux(Board, [X|T], Color, Bot, Moves):-
chooseMove(Board, X, Color, Bot, Move),
allbestMovesAux(Board, T, Color, Bot, NewMoves),
append([(X,Move)], NewMoves, Moves).
%choose_place/2
%chooses a random empty position to place a piece
choose_place(Board, Pos):-
findall(Pos, empty(Board, Pos), Empties),
random_member(Pos, Empties).
% Define the typeofMove/3, which takes a board, a move and returns the type of movement
% - Board: the current board
% - Move: the move to be made
% - Type: the type of movement
typeofMove(Board, (X,Y), jump):-
\+empty(Board, (X,Y)).
typeofMove(Board, (X,Y), move):-
empty(Board, (X,Y)).
typeofMove(_, Move, jump):-
length(Move, 1).
higher_value([(Pos, Move) | Tail], Result):-
write('Started: '), write((Pos, Move)), nl,
higher_value(Tail, (Pos, Move), Result).
type(0, place).
type(1, move).
type(2, jump).
type(Number, chain):- Number >= 2.
execute_move(Color, ((StartX, StartY), (NextX, NextY)), Board, NewBoard):-
change_board_element(Board, StartX, StartY, 0, MidBoard),
place(MidBoard, (NextX, NextY), Color, NewBoard).
execute_move(Color, ((StartX, StartY), [(CapX, CapY)]), Board, NewBoard):-
DifX is CapX - StartX,
FinalX is CapX + DifX,
DifY is CapY - StartY,
FinalY is CapY + DifY,
change_board_element(Board, CapX, CapY, 0, MidBoard),
change_board_element(MidBoard, StartX, StartY, 0, FinalBoard),
place(FinalBoard, (FinalX,FinalY), Color, NewBoard).
execute_move(((StartX, StartY), [(CapX, CapY) | Tail]), Board, NewBoard):-
DifX is CapX - StartX,
FinalX is CapX + DifX,
DifY is CapY - StartY,
FinalY is CapY + DifY,
change_board_element(Board, CapX, CapY, 0, MidBoard),
change_board_element(MidBoard, StartX, StartY, 0, FinalBoard),
place(FinalBoard, (FinalX,FinalY), Color, PrintBoard),
display_board(PrintBoard), nl,
execute_move((FinalX, FinalY), Tail, PrintBoard, NewBoard).