0% found this document useful (0 votes)
2 views2 pages

Program 2

The document outlines a Python program that implements the Best First Search algorithm to solve the Missionaries-Cannibals problem. It includes a game loop that tracks the number of missionaries and cannibals on both sides of a river, checks for win or lose conditions, and allows user input for moving characters. The program ensures valid moves and updates the game state accordingly.

Uploaded by

vijetha.cbit
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views2 pages

Program 2

The document outlines a Python program that implements the Best First Search algorithm to solve the Missionaries-Cannibals problem. It includes a game loop that tracks the number of missionaries and cannibals on both sides of a river, checks for win or lose conditions, and allows user input for moving characters. The program ensures valid moves and updates the game state accordingly.

Uploaded by

vijetha.cbit
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

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:-

You might also like