From 72a975f8e51a87da429634b7099f39ddca572cae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cle=CC=81ment=20Grennerat?= Date: Fri, 28 Nov 2025 13:15:37 +0100 Subject: [PATCH] Working basic tic tac toe --- run.sh | 2 ++ src/script.pl | 1 + src/test.pl | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 64 insertions(+) create mode 100755 run.sh create mode 100644 src/script.pl create mode 100644 src/test.pl diff --git a/run.sh b/run.sh new file mode 100755 index 0000000..6e63f15 --- /dev/null +++ b/run.sh @@ -0,0 +1,2 @@ +#!/bin/zsh +{ cat ./src/script.pl ; echo -ne '\n\n' } | swipl ./src/test.pl diff --git a/src/script.pl b/src/script.pl new file mode 100644 index 0000000..8cf5f95 --- /dev/null +++ b/src/script.pl @@ -0,0 +1 @@ +init. diff --git a/src/test.pl b/src/test.pl new file mode 100644 index 0000000..2a4659d --- /dev/null +++ b/src/test.pl @@ -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).