Question: I have a program I've written in prolog that deals with the classic rat in a maze example, but I'm trying to add to it

I have a program I've written in prolog that deals with the classic rat in a maze example, but I'm trying to add to it a bit of functionality. Basically, I want to keep a count of how many rooms have been traversed so far, and ensure that a certain condition has been met at least once in the past three traversals (rooms in the maze).

The code I have so far is as follows:

connected(X,Y):- d(X,Y);d(Y,X).

path(A,B,Path):- go(A,B,[A],Q), reverse(Q,Path).

go(A,B,P,[B|P]):- connected(A,B).

go(A,B,Visited,Path):- connected(A,C), C\==B, not(member(C,Visited)), go(C,B,[C|Visited],Path).

cheese(X):- cheese(X).

I realize I probably don't need the last predicate since I could just directly say cheese(c), cheese(b), etc, but it's mostly there to remind myself of where I'm going with this and to actually write the check for cheese into the traversal. The main issue I'm having here is that I'm unsure how to have the counter increment/decrement appropriately on traversal or backtracking. Any help would be appreciated. Thanks.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!