I am trying to solve tic-tac-toe with a simple minimax algorithm. Simple, but should cover a lot of language. What I still have:
The board is presented as an array of 9 (unrelated) variables that are either set to x or o .
The winning condition in this case is mainly: win(Player, [X1,X2,X3|_]) :- X1==Player,X2==Player,X3==Player. etc. for all eight options. draw simply checks if all variables are bound.
The move offers are also simple: move(Player, [X|_], 0, 0) :- var(X), X=Player. again for all possible positions (I will leave code reuse for a later program :)).
Now I can generate all possible moves with a simple backtrace: move(Player, Board, X, Y). , which should basically be all that I need for minimax (obviously, a simple utility function that returns 1 if the computer wins, 0 in the event of a tie and -1 if the person wins, this is easy). I just donโt know how to implement it, and all the examples that I find on the network are quite complex and not entirely clear.
Note. I'm fine with n ^ 2 or worse runtime behavior - it's really not about efficiency. And yes, I know how to write minimax in lisp, python, java - I just donโt know how to "port" this code to the prolog.
Voo
source share