0% found this document useful (0 votes)
9 views

AI file

The document is a lab file for an Artificial Intelligence course at Amity University, detailing experiments submitted by a student named Nandini Tiwari. It includes Python programs for a number guessing game and the water jug problem using BFS. Each experiment outlines the aim and provides the source code for the respective programs.

Uploaded by

rawatishita2020
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)
9 views

AI file

The document is a lab file for an Artificial Intelligence course at Amity University, detailing experiments submitted by a student named Nandini Tiwari. It includes Python programs for a number guessing game and the water jug problem using BFS. Each experiment outlines the aim and provides the source code for the respective programs.

Uploaded by

rawatishita2020
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
You are on page 1/ 6

AMITY SCHOOL OF ENGINEERING TECHNOLOGY AMITY

UNIVERSITY

NOIDA, UTTAR PRADESH

LAB FILE

Artificial Intelligence

CSE401

Submitted by : Nandini Tiwari

Enrollment No.: A2305222468

Class: 6CSE-7 (Y)

Submitted to: Dr. Garima Aggarwal


TABLE OF CONTENTS
S. NAME OF PAGE DATE REMARKS
NO. EXPERIMENT NO.
1. Write a program in python
to build a simple single
player game.
2.(a) Write a program in python
to implement water jug
problem using BFS.
2.(b)
3.
EXPERIMENT 1

AIM: Write a program in python to build a simple single player game.

SOURCE CODE:
import random
def guess_the_number():
# Generate a random number between 1 and 100
target_number = random.randint(1, 100)
attempts = 6
print("Welcome to the Number Guessing Game!")
print("I have selected a number between 1 and 100.")
print(f"You have {attempts} attempts to guess it.")

for attempt in range(1, attempts + 1):


try:
guess = int(input(f"Attempt {attempt}: Enter your guess: "))

if guess < target_number:


print("Too low! Try a higher number.")
elif guess > target_number:
print("Too high! Try a lower number.")
else:
print(f"Congratulations! You guessed the number {target_number} correctly in
{attempt} attempts.")
break
except ValueError:
print("Invalid input. Please enter a valid integer.")
else:
if attempt == attempts:
print(f"Sorry! You've used all {attempts} attempts. The number was
{target_number}. Better luck next time!")
break

guess_the_number()
OUTPUT:
EXPERIMENT 2

AIM: Write a program in python to implement water jug problem using BFS.

SOURCE CODE:
def display_state(state, operation):
print(f"State: {state} | Operation: {operation}")

def water_jug_problem():
initial_state = (0, 0)
goal_state = (2, 0)
knowledge_base = set()
states = [(initial_state, [])]

while states:
current_state, path = states.pop(0)
knowledge_base.add(current_state)

if current_state == goal_state:
print("\nGoal state reached!")
print("Path to solution:")
for step in path:
print(step)
return

a, b = current_state

possible_moves = [
((4, b), "Fill 4L jug"),
((a, 3), "Fill 3L jug"),
((0, b), "Empty 4L jug"),
((a, 0), "Empty 3L jug"),
((min(a + b, 4), max(0, b - (4 - a))), "Pour 3L to 4L"),
((max(0, a - (3 - b)), min(a + b, 3)), "Pour 4L to 3L"),
]

for new_state, operation in possible_moves:


if new_state not in knowledge_base:
display_state(new_state, operation)
states.append((new_state, path + [operation]))

if __name__ == "__main__":
print("Initial State: (0, 0)")
water_jug_problem()
OUTPUT:

You might also like