0% found this document useful (0 votes)
5 views10 pages

Vaidhei

The document contains three practical programming assignments related to artificial intelligence. The first assignment implements a Tic-Tac-Toe game, the second solves the water jug problem using breadth-first search (BFS), and the third implements depth-first search (DFS) for the 8-puzzle problem. Each section includes code snippets and aims to demonstrate the respective algorithms and problem-solving techniques.

Uploaded by

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

Vaidhei

The document contains three practical programming assignments related to artificial intelligence. The first assignment implements a Tic-Tac-Toe game, the second solves the water jug problem using breadth-first search (BFS), and the third implements depth-first search (DFS) for the 8-puzzle problem. Each section includes code snippets and aims to demonstrate the respective algorithms and problem-solving techniques.

Uploaded by

darkwiki786
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Artificial Intelligence 200020107517

Practical-1
AIM:-Write a program to implement Tic-toe game problem.
• Code
import os
import time
board = [' ',' ',' ',' ',' ',' ',' ',' ',' ',' ']
player = 1
Win = 1
Draw = -1
Running = 0
Stop = 1
Game = Running
Mark = 'X'
#This Function Draws Game Board
def DrawBoard():
print(" %c | %c | %c " % (board[1],board[2],board[3]))
print("___|___|___")
print(" %c | %c | %c " % (board[4],board[5],board[6]))
print("___|___|___")
print(" %c | %c | %c " % (board[7],board[8],board[9]))
print(" | | ")
#This Function Checks position is empty or not
def CheckPosition(x):
if(board[x] == ' '):
return True
else:
return False
#This Function Checks player has won or not
def CheckWin():
global Game

Page | 1
Artificial Intelligence 200020107517

#Horizontal winning condition


if(board[1] == board[2] and board[2] == board[3] and board[1] != ' '):
Game = Win
elif(board[4] == board[5] and board[5] == board[6] and board[4] != ' '):
Game = Win
elif(board[7] == board[8] and board[8] == board[9] and board[7] != ' '):
Game = Win
#Vertical Winning Condition
elif(board[1] == board[4] and board[4] == board[7] and board[1] != ' '):
Game = Win
elif(board[2] == board[5] and board[5] == board[8] and board[2] != ' '):
Game = Win
elif(board[3] == board[6] and board[6] == board[9] and board[3] != ' '):
Game=Win
#Diagonal Winning Condition
elif(board[1] == board[5] and board[5] == board[9] and board[5] != ' '):
Game = Win
elif(board[3] == board[5] and board[5] == board[7] and board[5] != ' '):
Game=Win
#Match Tie or Draw Condition
elif(board[1]!=' ' and board[2]!=' ' and board[3]!=' ' and board[4]!=' ' and board[5]!=' ' and
board[6]!=' ' and board[7]!=' ' and board[8]!=' ' and board[9]!=' '):
Game=Draw
else:
Game=Running

print("Tic-Tac-Toe Game ")


print("Player 1 [X] --- Player 2 [O]\n")
print()
print()
print("Please Wait...")
Page | 2
Artificial Intelligence 200020107517

time.sleep(3)
while(Game == Running):
os.system('cls')
DrawBoard()
if(player % 2 != 0):
print("Player 1's chance")
Mark = 'X'
else:
print("Player 2's chance")
Mark = 'O'
choice = int(input("Enter the position between [1-9] where you want to mark : "))
if(CheckPosition(choice)):
board[choice] = Mark
player+=1
CheckWin()

os.system('cls')
DrawBoard()
if(Game==Draw):
print("Game Draw")
elif(Game==Win):
player-=1
if(player%2!=0):
print("Player 1 Won")
else:
print("Player 2 Won")

Page | 3
Artificial Intelligence 200020107517

• Output

Page | 4
Artificial Intelligence 200020107517

Practical-2
AIM:-Write a program to implement BFS for water jug problem.
• Code
from collections import defaultdict
visited = defaultdict(lambda: False)
J1, J2, L = 0, 0, 0
def Water_Jug_problem(X, Y):
global J1, J2, L
if (X == L and Y == 0) or (Y == L and X == 0):
print("(",X, ", ",Y,")", sep ="")
return True
if visited[(X, Y)] == False:
print("(",X, ", ",Y,")", sep ="")
visited[(X, Y)] = True
return (Water_Jug_problem(0, Y) or
Water_Jug_problem(X, 0) or
Water_Jug_problem(J1, Y) or
Water_Jug_problem(X, J2) or
Water_Jug_problem(X + min(Y, (J1-X)),
Y - min(Y, (J1-X))) or
Water_Jug_problem(X - min(X, (J2-Y)),
Y + min(X, (J2-Y))))
else:
return False
J1 = int(input("Capacity of first jug"));
J2 = int(input("Capacity of second jug"))
L = int(input("Total Capacity "))
print("Path is as Follow:")
Water_Jug_problem(0, 0)

Page | 5
Artificial Intelligence 200020107517

• Output

Page | 6
Artificial Intelligence 200020107517

Practical-3
AIM:-Write a program to implement DFS for 8 puzzle problem.
• Code
import copy
from heapq import heappush, heappop
n=3
row = [ 1, 0, -1, 0 ]
col = [ 0, -1, 0, 1 ]
class priorityQueue:
def __init__(self):
self.heap = []# Inserts a new key 'k'
def push(self, k):
heappush(self.heap, k)
def pop(self):
return heappop(self.heap)
def empty(self):
if not self.heap:
return True
else:
return False
class node:
def __init__(self, parent, mat, empty_tile_pos,
cost, level):
self.parent = parent
self.mat = mat
self.empty_tile_pos = empty_tile_pos
self.cost = cost
self.level = level
def __lt__(self, nxt):
return self.cost < nxt.cost

Page | 7
Artificial Intelligence 200020107517

def calculateCost(mat, final) -> int:


count = 0
for i in range(n):
for j in range(n):
if ((mat[i][j]) and
(mat[i][j] != final[i][j])):
count += 1
return count
def newNode(mat, empty_tile_pos, new_empty_tile_pos,
level, parent, final) -> node:
new_mat = copy.deepcopy(mat)
# Move tile by 1 position
x1 = empty_tile_pos[0]
y1 = empty_tile_pos[1]
x2 = new_empty_tile_pos[0]
y2 = new_empty_tile_pos[1]
new_mat[x1][y1], new_mat[x2][y2] = new_mat[x2][y2], new_mat[x1][y1]
cost = calculateCost(new_mat, final)
new_node = node(parent, new_mat, new_empty_tile_pos,
cost, level)
return new_node
def printMatrix(mat):
for i in range(n):
for j in range(n):
print("%d " % (mat[i][j]), end = " ")
print()
def isSafe(x, y):
return x >= 0 and x < n and y >= 0 and y < n
def printPath(root):
if root == None:

Page | 8
Artificial Intelligence 200020107517

return
printPath(root.parent)
printMatrix(root.mat)
print()
def solve(initial, empty_tile_pos, final):
pq = priorityQueue()
cost = calculateCost(initial, final)
root = node(None, initial,
empty_tile_pos, cost, 0)
pq.push(root)
while not pq.empty():
minimum = pq.pop()
if minimum.cost == 0:
printPath(minimum)
return
for i in range(4):
new_tile_pos = [
minimum.empty_tile_pos[0] + row[i],
minimum.empty_tile_pos[1] + col[i], ]
if isSafe(new_tile_pos[0], new_tile_pos[1]):
child = newNode(minimum.mat,
minimum.empty_tile_pos,
new_tile_pos,
minimum.level + 1,
minimum, final,)
pq.push(child)
initial = [ [ 1, 2, 3 ],
[ 5, 6, 0 ],
[ 7, 8, 4 ] ]

Page | 9
Artificial Intelligence 200020107517

final = [ [ 1, 2, 3 ],
[ 5, 8, 6 ],
[ 0, 7, 4 ] ]
empty_tile_pos = [ 1, 2 ]
solve(initial, empty_tile_pos, final)

• Output

Page | 10

You might also like