Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

List all possible actions on a 4x4 board in Prolog

Tags:

prolog

How can I list all possible boards after (for example) one player performs some action in a 4x4 board (with 8 different pions)?

The pions should be, for example:

([1,2,3,4,5,6,7,8])

and each time you play one automatically it cannot re-used.

The board should be like this board

([1,1,-,-],(1,2,-,-)...(1,3,-,-)(1,4,-,-)
 [2,1,-,-],(2,2,-,-)...(2,3,-,-)(2,4,-,-)
 [3,1,-,-],(3,2,-,-)...(3,3,-,-)(3,4,-,-)
 [4,1,-,-],(4,2,-,-)...(4,3,-,-)(4,4,-,-)

And one player per round puts one pion on the board until it's full.

like image 616
user1319472 Avatar asked Nov 05 '22 02:11

user1319472


1 Answers

I assume you have a predicate like board0_move_board/3, which is a ternary relation between a board B, a move M, and the resulting board after the move M is made on B. Further, I assume you have a predicate board_move/2 that relates a board to a move that is possible on that board, and on backtracking successively gives all moves that are possible. With these predicates, you can get all possible boards with:

findall(Board, (board_move(Board0, Move), board0_move_board(Board0, Move, Board)), Boards)
like image 107
mat Avatar answered Nov 27 '22 00:11

mat