Prolog 5x5 tic-tac-toe
One beautiful sunny day, I’ve been told that I have to learn Prolog. In fact it was something like 5 laboratories + a project to do in home. I’ve decided to do a tic-tac-toe playing program. To make it a bit more challenging, the game plays on 5x5 board by default (in fact it was designed, and did work for 6x6, but I lost that in tweaking; you may also try 4x4 and bigger depth). The algorithm employed for the AI is minimax search with heuristic move generation.
GPLv2, as always.
The program displays a board with empty fields marked as ‘.’ and respective players signs ‘o’ and ‘x’. It asks about coordinates of your next move. The answer should be a single number terminated by ‘.'. Enter invalid move (outside of board or taken field) to end the game and display scores.
There are two versions - one for SWI Prolog and second for Borland Prolog. To run the first version (under Linux) all you need is SWI-prolog installed, make the file executable and run it under CLI. Borland version needs Turbo Prolog that can be downloaded from many places on the net as abandonware. Note, that due to limited resources the second version does not run well on boards bigger than 3x3.
You can play tweaking the algorithm. Constants you may want to change are in (line numbers for the first version):
- move evaluation - lines 137 and 140
- moves generator - line 149
- minimax depth - line 218
Furthermore board size can be changed with the very top boardsize predicate.