Testing some Prolog code to see if I can show gameover when a white(w) has a connected path from one side of the board to the other.
My biggest problem so far is while gameover/2
does go through all the nodes on each side and checks if their connected it doesn't stop or show when they are. Thus when testing this query:
gameover([[e,e,b,e,e],[e,w,w,b,e],[b,w,b,w,w],[w,w,b,b,b],[e,e,b,w,e]], w).
Prolog returns no when it should be true. I've gone through everything I can but I am new to Prolog and I'm sure I must be missing something obvious.
gameover(State, w) :-
width(State, W), write(W), nl,
series(1, W, NewList), write(NewList), nl,
member(X1, NewList), write(X1), nl,
write(loc(X1,1)), nl, write('X1'), nl,
member(X2, NewList), write(X2), nl,
write(loc(X2,W)), nl, write('X2'), nl,
connected(State, loc(X1,1), loc(X2,W), w).
I've tested connected, member, series and width each very thoroughly and am sure they all work so I think it must be something I have done in the code above.
There is also a similar predicate for gameover(State,b)
that uses the Y
coordinate rather than the X
. It's also having the same troubles.
Connected:
connected(State, Start, End, Colour) :- get_location(State, End, Colour),
get_location(State, Start, Colour),
connects(State, End, Colour, Start, [Start]).
connected(_State, _Next, _Colour, End, Close):- member(End, Close).
%checking to make sure every node is connected to the other using linked
%list of children from linked- check child is not member of closed list(meaning it has been checked before)
%get location of child to check for colour
%recursive call, include children in closed list to mark they have been checked and won't be checked again
connects(State, End, Colour, Next, Close) :- linked(Next, Child),
\+member(Child, Close),
get_location(State, Child, Colour),
connects(State, End, Colour, Child, [Child|Close]).
connects(_State, End, _Colour, End, _Close).