-
Notifications
You must be signed in to change notification settings - Fork 0
/
gameUtils.pl
372 lines (321 loc) · 16.6 KB
/
gameUtils.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
:- include('casinhas.pl').
:- include('teste.pl').
:- include('directions.pl').
:- use_module(library(lists)).
:- use_module(library(sets)).
printRowSeparator:-
write('# --------------------------------------------------- #'), nl.
printSectorSeparator:-
write('# ################################################### #'), nl.
printDownBorder:-
write('#######################################################'), nl.
printBorder:-
write('#').
printColSeparator:-
write('|').
/*pair[playermarkercolor, direction]
startingBoard([ [[2, 9],[0, 0],[1, 8],[0, 0],[0, 0],[0, 0],[2, 8],[0, 0],[0, 0]],
[[0, 0],[0, 0],[0, 0],[0, 0],[0, 0],[0, 0],[0, 0],[0, 0],[0, 0]],
[[0, 0],[0, 0],[0, 0],[0, 0],[0, 0],[0, 0],[0, 0],[0, 0],[0, 0]],
[[0, 0],[0, 0],[0, 0],[0, 0],[0, 0],[0, 0],[0, 0],[0, 0],[0, 0]],
[[1, 6],[0, 0],[0, 0],[0, 0],[0, 0],[0, 0],[0, 0],[0, 0],[2, 4]],
[[0, 0],[0, 0],[0, 0],[0, 0],[0, 0],[0, 0],[0, 0],[0, 0],[0, 0]],
[[0, 0],[0, 0],[0, 0],[0, 0],[0, 0],[0, 0],[0, 0],[0, 0],[0, 0]],
[[0, 0],[0, 0],[0, 0],[0, 0],[0, 0],[0, 0],[0, 0],[0, 0],[0, 0]],
[[0, 0],[0, 0],[1, 2],[0, 0],[0, 0],[0, 0],[2, 2],[0, 0],[1, 1]]
]).*/
startingBoard:-[ [[2, 9],[0, 0],[1, 8],[0, 0],[0, 0],[0, 0],[2, 8],[0, 0],[0, 0]],
[[0, 0],[0, 0],[0, 0],[0, 0],[0, 0],[0, 0],[0, 0],[0, 0],[0, 0]],
[[0, 0],[0, 0],[0, 0],[0, 0],[0, 0],[0, 0],[0, 0],[0, 0],[0, 0]],
[[0, 0],[0, 0],[0, 0],[0, 0],[0, 0],[0, 0],[0, 0],[0, 0],[0, 0]],
[[1, 6],[0, 0],[0, 0],[0, 0],[0, 0],[0, 0],[0, 0],[0, 0],[2, 4]],
[[0, 0],[0, 0],[0, 0],[0, 0],[0, 0],[0, 0],[0, 0],[0, 0],[0, 0]],
[[0, 0],[0, 0],[0, 0],[0, 0],[0, 0],[0, 0],[0, 0],[0, 0],[0, 0]],
[[0, 0],[0, 0],[0, 0],[0, 0],[0, 0],[0, 0],[0, 0],[0, 0],[0, 0]],
[[0, 0],[0, 0],[1, 2],[0, 0],[0, 0],[0, 0],[2, 2],[0, 0],[1, 1]]
].
printLine(_, [], _).
printLine(LineNumber, [Pair], _Iterator):-
marking(LineNumber, Pair).
printLine(LineNumber, [Pair|Rest], 3):-
marking(LineNumber, Pair),
write(' # '),
printLine(LineNumber, Rest, 1).
printLine(LineNumber, [Pair|Rest], Iterator):-
IteratorPlus is Iterator + 1,
marking(LineNumber, Pair),
write(' | '),
printLine(LineNumber, Rest, IteratorPlus).
printRow(Row):-
write('# '), printLine(1, Row, 1), write(' #'), nl,
write('# '), printLine(2, Row, 1), write(' #'), nl,
write('# '), printLine(3, Row, 1), write(' #'), nl.
displayBoard([], _).
displayBoard([Row], _Iterator):-
printRow(Row).
displayBoard([Row|Rest], 3):- % Quando se faz o separador de secções
printRow(Row),
printSectorSeparator,
displayBoard(Rest, 1).
displayBoard([Row|Rest], Iterator):-
IteratorPlus is Iterator + 1,
printRow(Row),
printRowSeparator,
displayBoard(Rest, IteratorPlus).
printBoard:-
printDownBorder, %experiencia
T= [[[2, 0],[0, 0],[1, 0],[1, 2],[1, 2],[0, 0],[2, 0],[2, 0],[1, 3]],
[[0, 0],[2, 0],[1, 0],[1, 0],[1, 0],[2, 0],[2, 0],[1, 0],[2, 0]],
[[0, 0],[2, 0],[1, 0],[1, 0],[1, 0],[2, 0],[2, 0],[1, 0],[2, 0]],
[[0, 0],[2, 0],[1, 0],[0, 0],[1, 0],[2, 0],[2, 0],[1, 0],[2, 0]],
[[1, 0],[1, 0],[2, 0],[1, 9],[2, 0],[1, 0],[2, 0],[1, 0],[2, 0]],
[[2, 4],[2, 0],[1, 0],[2, 0],[2, 0],[1, 0],[1, 0],[2, 0],[2, 6]],
[[0, 0],[0, 0],[2, 0],[1, 0],[1, 0],[2, 0],[1, 0],[2, 0],[2, 6]],
[[0, 0],[0, 0],[2, 0],[1, 0],[1, 0],[1, 0],[2, 0],[1, 0],[2, 9]],
[[0, 0],[0, 0],[1, 0],[2, 0],[2, 0],[2, 0],[2, 0],[0, 0],[1, 0]]],
displayBoard(T, 1),
printDownBorder.
printBoard(T):-
printDownBorder, %experiencia
displayBoard(T, 1),
printDownBorder.
% Predicados
/*
movePeca(Jogador, Peca, Direcao).
colocaMarcador(Jogador, Casa).
giraPeca(Jogador, Peca, Direcao). % 0 - Nao gira, 1 - 45º contra-relogio, 2 - 45º sentido relogio
podeMoverPeca(Jogador, Peca, Direcao). %- Verifica se uma peca pode ser movida na direcao escolhida
mudouDeSeccao(Jogador, Peca, Direcao). %- Verifica se uma peca mudou de seccao
alcancavel(Jogador, Peca, Casa). %- Verifica se, nesse momento, é possivel determinada peça, com um numero qualquer de jogadaschegar a uma determinada casa
casaTemPeca(Casa). %
casaTemMarcadorOposto(Jogador, Casa). %
contaMarcadores(Jogador, Tabuleiro, Contador). %
fimDeJogo. %
*/
%----------------------------------------------------------
%----------------------------------------------------------
getLine(Board, Line, LineNumber):- getLine(Board, Line, LineNumber, 1). % Iterator = 1
getLine([], _, _, _):- %If lineNumber is greater that the board size
write('Line not existent'), nl, fail.
getLine([Line|_Rest], ResultLine, LineNumber, LineNumber):- % When Iterator = LineNumber
ResultLine = Line.
getLine([_Line|Rest], ResultLine, LineNumber, Iterator):- % Iterates across the lines of the board
IteratorPlus is Iterator + 1,
getLine(Rest, ResultLine, LineNumber, IteratorPlus).
getLineElement(Line, Tile, ColumnNumber):- getLineElement(Line, Tile, ColumnNumber, 1).
getLineElement([], _, _, _):-
write('Column not existent'), nl, fail.
getLineElement([Element|_Rest], Tile, ColumnNumber, ColumnNumber):-
Tile = Element.
getLineElement([_Element|Rest], Tile, ColumnNumber, Iterator):-
IteratorPlus is Iterator + 1,
getLineElement(Rest, Tile, ColumnNumber, IteratorPlus).
getTile(Board, Tile, [X, Y]):-
!,
getLine(Board, Line, Y), !, % - Avoids redoing getLine
getLineElement(Line, Tile, X).
%WARNING write(Tile), nl.
getPlayer(Board, [X, Y], Player):-
getTile(Board, [TilePlayer, _TileDirection], [X, Y]),
Player = TilePlayer.
setPlayer(Board, [X, Y], Player, NewBoard):-
getTile(Board, [_TilePlayer, TileDirection], [X, Y]),
changeTile(Board, [X, Y], [Player, TileDirection], NewBoard).
getDirection(Board, [X, Y], Direction):-
getTile(Board, [_TilePlayer, TileDirection], [X, Y]),
Direction = TileDirection.
setDirection(Board, [X, Y], Direction, NewBoard):-
Direction > -1, Direction < 10,
getTile(Board, [TilePlayer, _TileDirection], [X, Y]),
changeTile(Board, [X, Y], [TilePlayer, Direction], NewBoard).
% -----------------------------------------------
% -----------------------------------------------
validMove(Board, Player, [_X, _Y], Direction):- %Checks if moving a player's piece in the Direction is a valid move.
%TODO getTile to get the Player color automatically???
directionToCoordinates(Direction, [_X, _Y], [NextX, NextY]),
NextX > 0, NextY > 0,
NextX < 10, NextY < 10,
getTile(Board, [NextTilePlayer, NextTileDirection], [NextX, NextY]), % Gets the next tile acording to the direction given
!, (NextTilePlayer =:= Player ; NextTilePlayer =:= 0), % The next tile must be the from the players color, or no color
NextTileDirection =:= 0. % The next tile must not have any piece there
% jogadasValidas(Tabuleiro, Jogador, Peca, []).
getValidMoves(Board, Player, [X, Y], Result):-
Y > 0, X > 0,
Y < 10, X < 10,
getValidMoves(Board, Player, [X, Y], [], 1, Result).
getValidMoves(_Board, _Player, [_X, _Y], DirectionsList, 10, Result):- %Ending condition (Iterator == 10)
Result = DirectionsList.
getValidMoves(Board, Player, [X, Y], DirectionsList, Iterator, Result):- % Iterator is the directions
IteratorPlus is Iterator + 1,
\+validMove(Board, Player, [X, Y], Iterator),
getValidMoves(Board, Player, [X, Y], DirectionsList, IteratorPlus, Result).
getValidMoves(Board, Player, [X, Y], DirectionsList, Iterator, Result):- % Iterator is the directions
IteratorPlus is Iterator + 1,
validMove(Board, Player, [X, Y], Iterator),
append(DirectionsList, [Iterator], NewDirectionsList),
getValidMoves(Board, Player, [X, Y], NewDirectionsList, IteratorPlus, Result).
% Obtains the list of Directions that the piece can move in
getMovingDirections(Board, [X, Y], Directions):-
getTile(Board, [TilePlayer, TileDirection], [X, Y]),
TileDirection \= 0, %It must have a piece in the tile
getValidMoves(Board, TilePlayer, [X, Y], ValidMovesList), %Get all the valid tiles around the [X, Y] tile
getNearDirections(TileDirection, NearDirectionsList), %Get the near possible directions to move the piece
list_to_set(NearDirectionsList, NearDirectionsSet),
list_to_set(ValidMovesList, ValidMovesSet),
intersection(NearDirectionsSet, ValidMovesSet, Directions). %Get the near possible directions to move the piece that are valid
% -----------------------------------------------
% -----------------------------------------------
changeLine(Board, NewLine, LineNumber, Result):-
length(NewLine, 9),
LineNumber < 10,
LineNumber > 0,
changeLine(Board, [], LineNumber, NewLine, 1, Result).
changeLine(RemainderOfBoard, NewBoard, LineNumber, _, Iterator, Result):-
Iterator > LineNumber,
LineNumber < 10,
append(NewBoard, RemainderOfBoard, Result).
changeLine([Line | Rest], NewBoard, LineNumber, NewLine, Iterator, Result):-
Iterator < LineNumber,
LineNumber > 0,
IteratorPlus is Iterator + 1,
append(NewBoard, [Line], NewBoardPlus),
changeLine(Rest, NewBoardPlus, LineNumber, NewLine, IteratorPlus, Result).
changeLine([_Line | Rest], NewBoard, LineNumber, NewLine, Iterator, Result):-
LineNumber =:= Iterator,
IteratorPlus is Iterator + 1,
append(NewBoard, [NewLine], NewBoardPlus), %Adds line to the NewBoard
changeLine(Rest, NewBoardPlus, LineNumber, NewLine, IteratorPlus, Result).
changeElement(Line, NewElement, ColumnNumber, Result):-
length(NewElement, 2),
ColumnNumber > 0,
ColumnNumber < 10,
changeElement(Line, [], NewElement, ColumnNumber, 1, Result).
changeElement([_Element | Rest], TempLine, NewElement, ColumnNumber, Iterator, Result):-
Iterator =:= ColumnNumber,
append(TempLine, [NewElement], TempLinePlus),
append(TempLinePlus, Rest, Result).
changeElement([Element | Rest], TempLine, NewElement, ColumnNumber, Iterator, Result):-
Iterator < ColumnNumber,
IteratorPlus is Iterator + 1,
append(TempLine, [Element], TempLinePlus),
changeElement(Rest, TempLinePlus, NewElement, ColumnNumber, IteratorPlus, Result).
changeTile(Board, [X, Y], [TilePlayer, TileDirection], ResultBoard):-
TileDirection > -1, TileDirection < 10,
getLine(Board, TargetLine, Y), % Gets line to change
changeElement(TargetLine, [TilePlayer, TileDirection], X, ResultLine), %Changes the line
changeLine(Board, ResultLine, Y, ResultBoard). %Changes the board
% -----------------------------------------------
% -----------------------------------------------
% This DOESN'T ask for the initial direction change of the piece!!!
% That must be done before, after input from the player/AI
movePiece(Board, [X, Y], Direction, NewBoard):-
getTile(Board, [TilePlayer, TileDirection], [X, Y]),!, %get old Tile
TilePlayer \= 0, !, %if it is 0, then there is no piece
TileDirection \= 0, TileDirection \= 5, !,%if it is 0 or 5, there is no piece
getMovingDirections(Board, [X, Y], ValidDirections),!, %get valid directions to move
memberchk(Direction, ValidDirections), %check if direction is a valid direction
directionToCoordinates(Direction, [X, Y], [NewX, NewY]), %Get the new Tile
changeTile(Board, [NewX, NewY], [TilePlayer, Direction], TempBoard), %Place the marker and the piece in the new Tile
setDirection(TempBoard, [X, Y], 0, NewBoard). %Erase the old piece from the previous tile
% -----------------------------------------------
% -----------------------------------------------
rotatePiece(Board, [X, Y], 1, NewBoard):- % CounterClockWise
getTile(Board, [TilePlayer, TileDirection], [X, Y]),!, %get Tile
write('Got tile'), nl,
TilePlayer \= 0, !, %if it is 0, then there is no piece
TileDirection \= 0, TileDirection \= 5, !,%if it is 0 or 5, there is no piece
getNearDirections(TileDirection, [CounterClockWise, _Same, _ClockWise]),
setDirection(Board, [X, Y], CounterClockWise, NewBoard),
write('Rotate Piece done'), nl.
rotatePiece(Board, [X, Y], 2, NewBoard):- % Same direction
getTile(Board, [TilePlayer, TileDirection], [X, Y]),!, %get old Tile
TilePlayer \= 0, !, %if it is 0, then there is no piece
TileDirection \= 0, TileDirection \= 5, !,%if it is 0 or 5, there is no piece
getNearDirections(TileDirection, [_CounterClockWise, Same, _ClockWise]),
setDirection(Board, [X, Y], Same, NewBoard).
rotatePiece(Board, [X, Y], 3, NewBoard):- % ClockWise
getTile(Board, [TilePlayer, TileDirection], [X, Y]),!, %get old Tile
TilePlayer \= 0, !, %if it is 0, then there is no piece
TileDirection \= 0, TileDirection \= 5, !,%if it is 0 or 5, there is no piece
getNearDirections(TileDirection, [_CounterClockWise, _Same, ClockWise]),
setDirection(Board, [X, Y], ClockWise, NewBoard).
rotatePiece(_, _, _, _):- fail .
% -----------------------------------------------
% -----------------------------------------------
% Detects if between the two tiles there is an area change
changedArea([PrevX, PrevY], [NewX, NewY]):-
(PrevX =:= 3, NewX =:= 4);
(PrevX =:= 4, NewX =:= 3);
(PrevX =:= 6, NewX =:= 7);
(PrevX =:= 7, NewX =:= 6);
(PrevY =:= 3, NewY =:= 4);
(PrevY =:= 4, NewY =:= 3);
(PrevY =:= 6, NewY =:= 7);
(PrevY =:= 7, NewY =:= 6).
changedArea([PrevX, PrevY], Direction, HasChanged):-
directionToCoordinates(Direction, [PrevX, PrevY], [ResX, ResY]),
changedArea([PrevX, PrevY], [ResX, ResY]),
HasChanged = 1.
changedArea([PrevX, PrevY], Direction, HasChanged):-
directionToCoordinates(Direction, [PrevX, PrevY], [ResX, ResY]),
\+changedArea([PrevX, PrevY], [ResX, ResY]),
HasChanged = 0.
% -----------------------------------------------
% -----------------------------------------------
nextTileHasMarker(Board, [X,Y], Direction):-
directionToCoordinates(Direction, [X, Y], [NewX, NewY]),!,
getTile(Board, [TilePlayer, _TileDirection], [NewX, NewY]),!,
TilePlayer \= 0.
% -----------------------------------------------
% -----------------------------------------------
getPlayerPieces(Board, Player, Pieces, PiecesLength):- %TODO Verification of NumPlayers
Player > 0, Player < 5,
getPlayerPieces(Board, Player, 1, [], Pieces, 0, PiecesLength).
getPlayerPieces([], _, _, TempPieces, Pieces, TempPiecesLength, PiecesLength):-
Pieces = TempPieces,
PiecesLength = TempPiecesLength.
getPlayerPieces([Line | Rest], Player, ActualY, TempPieces, Pieces, TempPiecesLength, PiecesLength):-
getPlayerPiecesLine(Line, Player, [1, ActualY], [], Pieces1, 0, PiecesLength1),
ActualYPlus is ActualY + 1,
TempPiecesLengthPlus is PiecesLength1 + TempPiecesLength,
append(TempPieces, Pieces1, TempPiecesPlus),
getPlayerPieces(Rest, Player, ActualYPlus, TempPiecesPlus, Pieces, TempPiecesLengthPlus, PiecesLength).
getPlayerPiecesLine([], _, _, TempPieces, Pieces, TempPiecesLength, PiecesLength):-
Pieces = TempPieces,
PiecesLength = TempPiecesLength.
getPlayerPiecesLine([[TilePlayer, _TileDirection] | Rest], Player, [ActualX, ActualY], TempPieces, Pieces, TempPiecesLength, PiecesLength):-
TilePlayer \= Player,
ActualXPlus is ActualX + 1,
getPlayerPiecesLine(Rest, Player, [ActualXPlus, ActualY], TempPieces, Pieces, TempPiecesLength, PiecesLength).
getPlayerPiecesLine([[TilePlayer, _TileDirection] | Rest], Player, [ActualX, ActualY], TempPieces, Pieces, TempPiecesLength, PiecesLength):-
TilePlayer =:= Player,
append(TempPieces, [[ActualX, ActualY]], TempPiecesPlus),
ActualXPlus is ActualX + 1,
PiecesLengthPlus is TempPiecesLength + 1,
getPlayerPiecesLine(Rest, Player, [ActualXPlus, ActualY], TempPiecesPlus, Pieces, PiecesLengthPlus, PiecesLength).
% TODO Funcao que filtra o getPlayerPieces para ter so aquelas que se possam movimentar
pieceCanMove(Board, [X, Y]):-
getMovingDirections(Board, [X, Y], Directions),
!,
Directions \= []. %If it is empty, the piece cant move to any valid tile
% -----------------------------------------------
% -----------------------------------------------
getPlayerMovingPieces(Board, Player, Pieces, PiecesLength):- %Only return the pieces of the player that can move
getPlayerPieces(Board, Player, AllPieces, _AllPiecesLength),
getPlayerMovingPiecesAux(Board, AllPieces, Pieces, PiecesLength).
getPlayerMovingPiecesAux(Board, AllPieces, MovingPieces, MovingPiecesLength):-
getPlayerMovingPiecesAux(Board, AllPieces, [], MovingPieces, 0, MovingPiecesLength).
getPlayerMovingPiecesAux(_Board, [], TempMovingPieces, MovingPieces, TempMovingPiecesLength, MovingPiecesLength):-
MovingPieces = TempMovingPieces,
MovingPiecesLength = TempMovingPiecesLength.
getPlayerMovingPiecesAux(Board, [Piece | Rest], TempMovingPieces, MovingPieces, TempMovingPiecesLength, MovingPiecesLength):-
pieceCanMove(Board, Piece),
append(TempMovingPieces, [Piece], TempMovingPiecesPlus),
TempMovingPiecesLengthPlus is TempMovingPiecesLength + 1,
getPlayerMovingPiecesAux(Board, Rest, TempMovingPiecesPlus, MovingPieces, TempMovingPiecesLengthPlus, MovingPiecesLength).
getPlayerMovingPiecesAux(Board, [Piece | Rest], TempMovingPieces, MovingPieces, TempMovingPiecesLength, MovingPiecesLength):-
\+pieceCanMove(Board, Piece),
getPlayerMovingPiecesAux(Board, Rest, TempMovingPieces, MovingPieces, TempMovingPiecesLength, MovingPiecesLength).
% -----------------------------------------------
% -----------------------------------------------
%getMostValuableMove(Board, Tile). %TODO So verificar a linha reta em frente