Menu
- 1.Write a Program to Implement Breadth First Search using Python.
- 2.Write a Program to Implement Depth First Search using Python
- 3.Write a Program to Implement Tic-Tac-Toe game using Python.
- 4.Write a Program to Implement 8-Puzzle problem using Python.
- 5.Write a Program to Implement Water-Jug problem using Python.
- 6.Write a Program to Implement Travelling Salesman Problem using Python.
- 7.Write a Program to Implement Tower of Hanoi using Python
Aim: Write a Program to Implement Monkey Banana Problem using Python
Program:
class State:
def __init__(self, monkey, box, banana):
self.monkey = monkey # Position of the monkey
self.box = box # Position of the box
self.banana = banana # Position of the banana
def __str__(self):
return f"Monkey: {self.monkey}, Box: {self.box}, Banana: {self.banana}"
def push_box(state):
if not state.box and not state.monkey:
return State(state.monkey, True, state.banana)
return state
def climb_box(state):
if state.box and not state.monkey:
return State(True, state.box, state.banana)
return state
def grab_banana(state):
if state.monkey and state.banana:
print("Banana grabbed!")
return State(state.monkey, state.box, True)
return state
def monkey_banana_problem():
initial_state = State(False, False, False)
print("Initial State:", initial_state)
state = push_box(initial_state)
print("After pushing the box:", state)
state = climb_box(state)
print("After climbing the box:", state)
state = grab_banana(state)
if __name__ == "__main__":
monkey_banana_problem()
Output:
Initial State: Monkey: False, Box: False, Banana: False
After pushing the box: Monkey: False, Box: True, Banana: False
After climbing the box: Monkey: True, Box: True, Banana: False
Banana grabbed!