Program 2
Implement and Demonstrate Best First Search Algorithm
on Missionaries-Cannibals Problems using Python.
print("Game Start")
lM, lC = 3, 3
rM, rC = 0, 0
boat = "L"
while True:
print("\nLeft:", lM, "M", lC, "C | Right:", rM, "M", rC, "C")
# Win condition
if rM == 3 and rC == 3:
print("You Won!")
break
# Lose condition
if (lM > 0 and lC > lM) or (rM > 0 and rC > rM):
print("You Lost!")
break
m = int(input("Missionaries: "))
c = int(input("Cannibals: "))
# Only 1 or 2 people allowed
if m + c == 0 or m + c > 2:
print("Invalid move")
continue
if boat == "L":
if m <= lM and c <= lC:
lM -= m; lC -= c
rM += m; rC += c
boat = "R"
else:
print("Not enough people")
else:
if m <= rM and c <= rC:
rM -= m; rC -= c
lM += m; lC += c
boat = "L"
else:
print("Not enough people")
Result:-