Prolog Programming Write a prolog program for the following
Solution
database
stateVisited(integer,integer)
predicates
newState(integer,integer)
clauses
newState(2,0).
newState(X,Y):- X < 4,
not(stateVisited(4,Y)),
assert(stateVisited(X,Y)),
write(\"Fill 4-Gallon Jug: (\",X,\",\",Y,\") --> (\", 4,\",\",Y,\")\ \"),
newState(4,Y).
newState(X,Y):- Y < 3,
not(stateVisited(X,3)),
assert(stateVisited(X,Y)),
write(\"Fill 3-Gallon Jug: (\", X,\",\",Y,\") --> (\", X,\",\",3,\")\ \"),
newState(X,3).
newState(X,Y):- X > 0,
not(stateVisited(0,Y)),
assert(stateVisited(X,Y)),
write(\"Empty 4-Gallon jug on ground: (\", X,\",\",Y,\") --> (\", 0,\",\",Y,\")\ \"),
newState(0,Y).
newState(X,Y):- Y > 0,
not(stateVisited(X,0)),
assert(stateVisited(X,0)),
write(\"Empty 3-Gallon jug on ground: (\", X,\",\",Y,\") --> (\", X,\",\",0,\")\ \"),
newState(X,0).
newState(X,Y):- X + Y >= 4,
Y > 0,
NEW_Y = Y - (4 - X),
not(stateVisited(4,NEW_Y)),
assert(stateVisited(X,Y)),
write(\"Fill water from 3-Gallon jug to 4-gallon until it full: (\", X,\",\",Y,\") --> (\", 4,\",\",NEW_Y,\")\ \"),
newState(4,NEW_Y).
newState(X,Y):- X + Y >=3,
X > 0,
NEW_X = X - (3 - Y),
not(stateVisited(X,3)),
assert(stateVisited(X,Y)),
write(\"Fill water from 4-Gallon jug to 3-gallon until it is full: (\", X,\",\",Y,\") --> (\", NEW_X,\",\",3,\")\ \"),
newState(NEW_X,3).
newState(X,Y):- X + Y <=4,
Y > 0,
NEW_X = X + Y,
not(stateVisited(NEW_X,0)),
assert(stateVisited(X,Y)),
write(\"Fill all the water from 3-Gallon jug to 4-gallon: (\", X,\",\",Y,\") --> (\", NEW_X,\",\",0,\")\ \"),
newState(NEW_X,0).
newState(X,Y):- X+Y<=3,
X > 0,
NEW_Y = X + Y,
not(stateVisited(0,NEW_Y)),
assert(stateVisited(X,Y)),
write(\"Fill all the water from 4-Gallon jug to 3-gallon: (\", X,\",\",Y,\") --> (\", 0,\",\",NEW_Y,\")\ \"),
newState(0,NEW_Y).
newState(0,2):- not(stateVisited(2,0)),
assert(stateVisited(0,2)),
write(\"Fill 2 gallons from 3-Gallon jug to 4-gallon: (\", 0,\",\",2,\") --> (\", 2,\",\",0,\")\ \"),
newState(2,0).
newState(2,Y):- not(stateVisited(0,Y)),
assert(stateVisited(2,Y)),
write(\"Empty 2 gallons from 4-Gallon jug on the ground: (\", 2,\",\",Y,\") --> (\", 0,\",\",Y,\")\ \"),
newState(0,Y).
goal
makewindow(1,2,3,\"4-3 Water Jug Problem\",0,0,25,80),
newState(0,0).

