Working basic tic tac toe
This commit is contained in:
commit
72a975f8e5
2
run.sh
Executable file
2
run.sh
Executable file
@ -0,0 +1,2 @@
|
|||||||
|
#!/bin/zsh
|
||||||
|
{ cat ./src/script.pl ; echo -ne '\n\n' } | swipl ./src/test.pl
|
||||||
1
src/script.pl
Normal file
1
src/script.pl
Normal file
@ -0,0 +1 @@
|
|||||||
|
init.
|
||||||
61
src/test.pl
Normal file
61
src/test.pl
Normal file
@ -0,0 +1,61 @@
|
|||||||
|
:- dynamic board/1. % permet l'assertion et le retrait de faits board/1
|
||||||
|
|
||||||
|
% Print board
|
||||||
|
displayBoardCell(Board, I):- nth0(I, Board, El), displayBoardCellEl(El), displayBoardReturn(I), fail.
|
||||||
|
displayBoardReturn(I):- (I = 2 ; I = 5), writeln('|'), writeln('├───┼───┼───┤').
|
||||||
|
|
||||||
|
displayBoardCellEl(El):- var(El), write('| · ').
|
||||||
|
displayBoardCellEl(El):- nonvar(El), El = 'x', write('| x ').
|
||||||
|
displayBoardCellEl(El):- nonvar(El), El = 'o', write('| o ').
|
||||||
|
|
||||||
|
displayBoard :- board(Board), writeln('┌───┬───┬───┐'),
|
||||||
|
((between(0, 8, I), displayBoardCell(Board, I)) ; writeln('|'), writeln('└───┴───┴───┘')).
|
||||||
|
|
||||||
|
% User play
|
||||||
|
ia(Board, Move):- repeat, random_between(0, 8, Move), nth0(Move, Board, X), (var(X) -> !).
|
||||||
|
|
||||||
|
playMove(Board, Move, NewBoard, Player):- copy_term(Board, NewBoard), nth0(Move, NewBoard, Player).
|
||||||
|
|
||||||
|
applyIt(Board, NewBoard):- retract(board(Board)), assert(board(NewBoard)).
|
||||||
|
|
||||||
|
changePlayer(Player, NextPlayer):- (Player = 'x', NextPlayer = 'o') ; (Player = 'o', NextPlayer = 'x').
|
||||||
|
|
||||||
|
% End game detection
|
||||||
|
boardEndGame(A, B, C, _, _, _, _, _, _, Winner):- nonvar(A), nonvar(B), nonvar(C), A = B, B = C, Winner = A.
|
||||||
|
boardEndGame(_, _, _, A, B, C, _, _, _, Winner):- nonvar(A), nonvar(B), nonvar(C), A = B, B = C, Winner = A.
|
||||||
|
boardEndGame(_, _, _, _, _, _, A, B, C, Winner):- nonvar(A), nonvar(B), nonvar(C), A = B, B = C, Winner = A.
|
||||||
|
boardEndGame(A, _, _, B, _, _, C, _, _, Winner):- nonvar(A), nonvar(B), nonvar(C), A = B, B = C, Winner = A.
|
||||||
|
boardEndGame(_, A, _, _, B, _, _, C, _, Winner):- nonvar(A), nonvar(B), nonvar(C), A = B, B = C, Winner = A.
|
||||||
|
boardEndGame(_, _, A, _, _, B, _, _, C, Winner):- nonvar(A), nonvar(B), nonvar(C), A = B, B = C, Winner = A.
|
||||||
|
boardEndGame(A, _, _, _, B, _, _, _, C, Winner):- nonvar(A), nonvar(B), nonvar(C), A = B, B = C, Winner = A.
|
||||||
|
boardEndGame(_, _, A, _, B, _, C, _, _, Winner):- nonvar(A), nonvar(B), nonvar(C), A = B, B = C, Winner = A.
|
||||||
|
|
||||||
|
notFullGame(Board):- between(0, 8, I), nth0(I, Board, X), var(X).
|
||||||
|
endGame(Board, Winner):-
|
||||||
|
nth0(0, Board, A), nth0(1, Board, B), nth0(2, Board, C),
|
||||||
|
nth0(3, Board, D), nth0(4, Board, E), nth0(5, Board, F),
|
||||||
|
nth0(6, Board, G), nth0(7, Board, H), nth0(8, Board, I),
|
||||||
|
boardEndGame(A, B, C, D, E, F, G, H, I, Winner).
|
||||||
|
endGame(Board, _):- not(notFullGame(Board)).
|
||||||
|
|
||||||
|
% Game loop
|
||||||
|
play(Player):-
|
||||||
|
board(Board), % instanciate the board from the knowledge base
|
||||||
|
displayBoard, % print it
|
||||||
|
(endGame(Board, Winner) -> (
|
||||||
|
(var(Winner) -> writeln('Game is a draw!') ; (write(Winner), writeln(' won!'))),
|
||||||
|
!, fail
|
||||||
|
) ; true
|
||||||
|
), % check for end game
|
||||||
|
write('New turn for: '), writeln(Player),
|
||||||
|
ia(Board, Move), % ask the AI for a move, that is, an index for the Player
|
||||||
|
playMove(Board, Move, NewBoard, Player), % Play the move and get the result in a new Board
|
||||||
|
applyIt(Board, NewBoard), % Remove the old board from the KB and store the new one
|
||||||
|
changePlayer(Player, NextPlayer), % Change the player before next turn
|
||||||
|
play(NextPlayer). % next turn!
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
%%%%% Start the game!
|
||||||
|
init :- length(Board, 9), assert(board(Board)), play('x').
|
||||||
|
%init :- length(Board, 9), assert(board(Board)), playMove(Board, 3, NewBoard, 'x'), writeln(NewBoard).
|
||||||
Loading…
x
Reference in New Issue
Block a user