I would like to write a predicate for tic-tac-toe game, that generate possible successor board configuration for given player and board. After typing this input:
next_board([[x,o,x],[x,x,o],[o,e,e]],x,N).
the output should be displayed as following:
N=[[x,o,x],[x,x,o],[o,e,e]];
N=[[x,o,x],[x,e,o],[o,x,e]];
N=[[x,o,x],[x,e,o],[o,e,x]];
To generate possible successor boards you basically have to “replace” single empty cells (marked with
e) with your player chip.At first sight you might be tempted to use select/4 but it would replace every empty cell in a row with your player chip. So, you might be better off using
append/3predicate with something like this:You might as well make recursive procedures to achieve the same logic without using
append/3.