-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinteraction.pl
102 lines (87 loc) · 3.38 KB
/
interaction.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
%% Author : Reynaud Nicolas (Kaldoran)
% Ask a move on the '+Board', with the '+PlayPawn'
% Output the '-From', '-To', to move pawn From to To
% -------------------------------------------------- %
askMove(From, To, Board, PlayPawn) :-
write('From - To [Row, Column] - [Row, Column] ? '),
read(RowColumnFrom - RowColumnTo),
convert(RowColumnFrom, From),
convert(RowColumnTo, To),
writeDebug(' Square N° [+1 Cause nth need +1]: '), writeDebug(From), write(' - '), writeDebug(To), nl,
check(RowColumnFrom, From, Board, PlayPawn),
check(RowColumnTo, To, Board, ' ').
% Alternative predicate
% If move is not valide write non valide and ask for another
% ---------------------------------------------------------- %
askMove(From, To, Board, PlayPawn) :-
write('[WARNING] : Illegal Move. Play Again !'), nl,
askMove(From, To, Board, PlayPawn).
% Ask player if he got an other move
% ---------------------------------- %
askEndMove :-
write('Do you have any extra move ? [y]es or [N]o or [e]xit.'), nl,
read(Answer),
( Answer = 'y', true; Answer = 'e', halt; fail).
% Ask player which AI he wants to fight
% ------------------------------------- %
askAI(AI):-
write('Which AI do you want to fight ?'),nl,
write('1- Nicolas\' one.'),nl,
write('2- Kevin B\'s one.'),nl,
write('3- Kevin L\'s one.'),nl,
read(TmpAI),
testAI(TmpAI,AI).
% Tests if AI exists
% ------------------ %
testAI(AI,AI):-
AI > 0, AI < 4,!.
testAI(_,AI):-write('Error number AI'),nl,askAI(AI).
% Do a multi move
% --------------- %
multiMove(Board, [], Board).
% Alternative for a MultiMove with a jump
% Same input / Output as previous
% --------------------------------------- %
multiMove(Board, [[From, Between, To]|MultiMove], NewBoard) :-
nth(From, Board, Pawn),
move(Board, From, To, Pawn, TmpBoard),
removePawn(TmpBoard, Between, TmpBoardBis),
multiMove(TmpBoardBis, MultiMove, NewBoard).
% Apply '+MultiMove' on the '+Board' with the '+Pawn'
% Output the '-NewBoard'
% --------------------------------------------------- %
multiMove(Board, [From, To], NewBoard) :-
nth(From, Board, Pawn),
move(Board, From, To, Pawn, NewBoard).
% Apply the Move but first, change the pawn to a Queen if needed
% -------------------------------------------------------------- %
move(Board, From, To, Pawn, NewBoard) :-
( To >= 90; To =< 10),
queen(Pawn, Queen),
doMove(Board, From, To, Queen, NewBoard).
% Apply the move if there is no change to Pawn
% -------------------------------------------- %
move(Board, From, To, Pawn, NewBoard) :-
doMove(Board, From, To, Pawn, NewBoard).
% Copy the board
% -------------- %
doMove([], _, _, _, []).
% Copy the '+Board' remplacing the '+From' Pawn, By ' '
% And the To Pawn by '+Pawn'
% OutputAll to '-NewBoard'
% ------------------------------------------------------- %
doMove([_|Board], 1, To, Pawn, [' '|NewBoard]) :-
NewTo is To - 1,
doMove(Board, 0, NewTo, Pawn, NewBoard), !.
doMove([_|Board], From, 1, Pawn, [Pawn|NewBoard]) :-
NewFrom is From - 1,
doMove(Board, NewFrom, 0, Pawn, NewBoard), !.
doMove([X|Board], From, To, Pawn, [X|NewBoard]) :-
NewFrom is From - 1,
NewTo is To - 1,
doMove(Board, NewFrom, NewTo, Pawn, NewBoard).
% Remove the pawn on the '+Board' at '+Square'
% Output the '-NewBoard'
% -------------------------------------------- %
removePawn(Board, Square, NewBoard) :-
move(Board, Square, 0, ' ',NewBoard).