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