Select/4 on a list of lists

Viewed 35

On prolog i'm trying to solve the following exercise for connect 4

?- generate_move(2,[[1, 0, 1, 0],[0, 1, 1, 0],[0, 0, 1, 1],[1, 0, 0, 1]],BP).
BP = [[1, 2, 1, 0], [0, 1, 1, 0], [0, 0, 1, 1], [1, 0, 0, 1]] ;
BP = [[1, 0, 1, 2], [0, 1, 1, 0], [0, 0, 1, 1], [1, 0, 0, 1]] ;
BP = [[1, 0, 1, 0], [2, 1, 1, 0], [0, 0, 1, 1], [1, 0, 0, 1]] ;
BP = [[1, 0, 1, 0], [0, 1, 1, 2], [0, 0, 1, 1], [1, 0, 0, 1]] ;
BP = [[1, 0, 1, 0], [0, 1, 1, 0], [2, 0, 1, 1], [1, 0, 0, 1]] ;
BP = [[1, 0, 1, 0], [0, 1, 1, 0], [0, 2, 1, 1], [1, 0, 0, 1]] ;

Where the rule is that is returns every possible move left for a player provided (in this case 2)

I was thinking of using select/4 to find every result possible for each list in the list of lists, then join it. But i'm failing to "condense" results and my second iteration of findall fails:

generate_move(_,[],[]).
generate_move(Player,[H|T],[FinalLine|FinalBoard]) :-
    findall(X0,(select(0, H, Player, X0)),FinalLine),
    join_results(FinalLine,T,FinalBoard),
    generate_move(Player,T,FinalBoard).

join_results([],[],[]).
join_results([H|T],LeftOvers,FinalBoard) :-
    append([H],LeftOvers,FinalBoard),
    join_results(T,LeftOvers,FinalBoard).

Any tips? My join_results returns false at the second call and so does my findall.

0 Answers
Related