0% found this document useful (0 votes)
13 views7 pages

Ai Project

The document describes three Python projects that illustrate fundamental AI concepts. It includes a rule-based chatbot, a Tic-Tac-Toe game utilizing the minimax algorithm for decision-making, and a pathfinding visualization using the BFS algorithm. These projects demonstrate how AI can interact, strategize, and solve problems without relying on external libraries.

Uploaded by

ishitasahu2050
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)
13 views7 pages

Ai Project

The document describes three Python projects that illustrate fundamental AI concepts. It includes a rule-based chatbot, a Tic-Tac-Toe game utilizing the minimax algorithm for decision-making, and a pathfinding visualization using the BFS algorithm. These projects demonstrate how AI can interact, strategize, and solve problems without relying on external libraries.

Uploaded by

ishitasahu2050
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

1.

Chatbot using Rule-Based Logic Knowledge-Based Agent /NLP

def chatbot():
print("Hello! I'm a simple chatbot. Type 'bye' to exit.")
while True:
user_input = input("You: ").lower()
if user_input in ['hi', 'hello']:
print("Bot: Hello there!")
elif user_input == "what is ai?":
print("Bot: AI stands for Artificial Intelligence.")
elif user_input == "who created you?":
print("Bot: I was created by a Python developer.")
elif user_input == "bye":
print("Bot: Goodbye!")
break
else:
print("Bot: Sorry, I don't understand that.")

chatbot()

Output:
Hello! I'm a simple chatbot. Type 'bye' to exit.
You: what is ai?
Bot: AI stands for Artificial Intelligence.
You: when was Ai created
Bot: Sorry, I don't understand that.
You: bye
Bot: Goodbye!
[Link]- Tac-Toe Game using Minimax Algorithm

import math
def print_board(board):
for row in board:
print(row)

def is_winner(board, player):


win_states = [
[board[0][0], board[0][1], board[0][2]],
[board[1][0], board[1][1], board[1][2]],
[board[2][0], board[2][1], board[2][2]],
[board[0][0], board[1][0], board[2][0]],
[board[0][1], board[1][1], board[2][1]],
[board[0][2], board[1][2], board[2][2]],
[board[0][0], board[1][1], board[2][2]],
[board[0][2], board[1][1], board[2][0]]
]
return [player, player, player] in win_states

def get_empty_cells(board):
return [(r, c) for r in range(3) for c in range(3) if board[r][c] == ' ']

def minimax(board, depth, is_maximizing):


if is_winner(board, 'O'):
return 1
if is_winner(board, 'X'):
return -1
if not get_empty_cells(board):
return 0

if is_maximizing:
best_score = -[Link]
for r, c in get_empty_cells(board):
board[r][c] = 'O'
score = minimax(board, depth + 1, False)
board[r][c] = ' '
best_score = max(score, best_score)
return best_score
else:
best_score = [Link]
for r, c in get_empty_cells(board):
board[r][c] = 'X'
score = minimax(board, depth + 1, True)
board[r][c] = ' '
best_score = min(score, best_score)
return best_score

def best_move(board):
best_score = -[Link]
move = None
for r, c in get_empty_cells(board):
board[r][c] = 'O'
score = minimax(board, 0, False)
board[r][c] = ' '
if score > best_score:
best_score = score
move = (r, c)
return move

def play_game():
board = [[' ' for _ in range(3)] for _ in range(3)]
while True:
print_board(board)
r, c = map(int, input("Enter row and col (0-2): ").split())
if board[r][c] != ' ':
print("Invalid move!")
continue
board[r][c] = 'X'
if is_winner(board, 'X'):
print_board(board)
print("You win!")
break
if not get_empty_cells(board):
print("It's a tie!")
break

ai_r, ai_c = best_move(board)


board[ai_r][ai_c] = 'O'
if is_winner(board, 'O'):
print_board(board)
print("AI wins!")
break

play_game()
Output :
[' ', ' ', ' ']
[' ', ' ', ' ']
[' ', ' ', ' ']
Enter row and col (0-2): 0 0
['X', ' ', ' ']
[' ', 'O', ' ']
[' ', ' ', ' ']
Enter row and col (0-2): 2 2
['X', 'O', ' ']
[' ', 'O', ' ']
[' ', ' ', 'X']
Enter row and col (0-2): 1 2
['X', 'O', 'O']
[' ', 'O', 'X']
[' ', ' ', 'X']
Enter row and col (0-2): 2 1
['X', 'O', 'O']
[' ', 'O', 'X']
['O', 'X', 'X']
AI wins!

3. Path finding Visualization using BFS or A*


from collections import deque
def bfs(grid, start, goal):
rows, cols = len(grid), len(grid[0])
visited = set()
queue = deque([(start, [start])])
while queue:
(r, c), path = [Link]()
if (r, c) == goal:
return path
for dr, dc in [(0,1),(1,0),(-1,0),(0,-1)]:
nr, nc = r + dr, c + dc
if 0 <= nr < rows and 0 <= nc < cols and grid[nr][nc] == 0 and (nr, nc) not in visited:
[Link]((nr, nc))
[Link](((nr, nc), path + [(nr, nc)]))
return None
# 5x5 Grid: 0 = free, 1 = wall
grid = [
[0, 0, 1, 0, 0],
[1, 0, 1, 0, 1],
[0, 0, 0, 0, 0],
[0, 1, 1, 1, 0],
[0, 0, 0, 1, 0],
]
start = (0, 0)
goal = (4, 4)

path = bfs(grid, start, goal)


if path:
print("Path found:", path)
else:
print("No path found.")

Output:
Path found: [(0, 0), (0, 1), (1, 1), (2, 1), (2, 2), (2, 3), (2, 4), (3, 4), (4, 4)
SUMMARY:
These three projects help us understand the basics of how AI works using simple Python
code. The chatbot shows how we can make a computer respond to questions using rules and
basic language understanding. The Tic-Tac-Toe game teaches how AI can make smart moves
by thinking ahead using the minimax algorithm. The pathfinding project shows how AI can
find the best way to reach a goal using search techniques like BFS or A*. By building these,
we learn how AI can make decisions, solve problems, and interact with people—all without
using any extra libraries.

You might also like