AI Based Packman Game
AI Based Packman Game
This is completely scratch code for packman using very simple manner using python. We can also say that this is a Mini Project based on AI. Even I did not use the Numpy in this Code.
actions=["Move Right","Move Left","Move Up","Move Down","Eat Food","Eat power pallet","Avoid Gost","Eat Ghost"]
percepts=[['p','pp','f','pp'],
['f','g','f','pp'],
['pp','f','pp','g'],
['pp','f','g','f']]
wlr1=[0,1]
wlr2=[2,1]
wlr3=[3,0]
wll1=[0,2]
wll2=[2,2]
wll3=[3,1]
wlr=[wlr1,wlr2,wlr3]
wll=[wll1,wll2,wll3]
m=len(percepts)
n=len(percepts[0])
wlrm=len(wlr)
wllm=len(wll)
wlm=len(percepts[0])
class Agent():
def __init__(self):
self.currentAction=0
self.r=0
self.c=0
self.power=False
self.chwlr=False
self.chwll=False
self.run_agent()
def run_agent(self):
for i in range(m):
for j in range(n):
if(percepts[i][j]=='p'):
print("Packman is present at ",i,j,"\n")
if((j+1)!=n):
if((self.r==i) and (self.c==j)):
# print("I am inside j+1 and value is ",i,j)
for x in range(wlrm):
# print("I am inside wlrm loop ",x)
if(wlr[x][0]==i):
# print("Value of wlr[x][0] is ",wlr[x][0])
if(wlr[x][1]==j):
# print("Value of wlr[x][1] is ",wlr[x][1])
print("wall is here for right side:",wlr[x])
self.chwlr=True
if(self.chwlr==False):
# print("Value of j+1 is false for wall")
if(percepts[i][j+1]=='pp'):
print("So I found a Power pallet j+1")
self.power=True
self.update_packman(i,j,actions[0])
elif(percepts[i][j+1]=='g'):
print("I found a gost at j+1 with ",i,j)
if(self.power==True):
self.update_packman(i, j, actions[0])
self.power=False
elif(percepts[i][j+1]=='f'):
print("I found a fruit at j+1 with ",i,j)
self.update_packman(i, j, actions[0])
self.chwlr=False
pass
if((j-1)!=-1):
if((self.r==i) and (self.c==j)):
# print("I am inside j-1 with ",i,j);
for y in range(wllm):
# print("I am inside wllm loop ",y)
if(wll[y][0]==i):
# print("Value of wll[y][0] is ",wll[y][0])
if(wll[y][1]==j):
# print("Value of wll[y][1] is ",wll[y][1])
print("wall is here:",wll[y])
self.chwll=True
if(self.chwll==False):
if(percepts[i][j-1]=='pp'):
print("I found a power pallet at j-1")
self.power=True
self.update_packman(i,j,actions[1])
j=j-1
elif(percepts[i][j-1]=='f'):
print("I found a fruit at j-1 with ",i,j)
self.update_packman(i,j,actions[1])
j=j-1
elif(percepts[i][j-1]=='g'):
print("I found a gost at j-1 with ",i,j)
if(self.power==True):
self.update_packman(i, j, actions[1])
self.power=False
j=j-1
self.chwll=False
pass
if((i+1)!=n):
if((self.r==i) and (self.c==j)):
# print("I am inside i+1 with ",i,j)
if(percepts[i+1][j]=='g'):
print("I found a gost at i+1")
if(self.power==True):
print("I also has power")
self.update_packman(i,j,actions[3])
self.power=False
pass
elif(percepts[i+1][j]=='pp'):
print("I found a power pallet at i+1")
self.update_packman(i,j,actions[3])
self.power=True
elif(percepts[i+1][j]=='f'):
print("I found fruit at i+1 with ",i,j)
self.update_packman(i,j,actions[3])
pass
if((i-1)!=-1):
if((self.r==i) and (self.c==j)):
# print("I am inside i-1 ",i,j)
if(percepts[i-1][j]=='pp'):
print("I found a power pallet at i-1 with ",i,j)
self.power=True
self.update_packman(i, j, actions[2])
elif(percepts[i-1][j]=='g'):
print("I found a gost at i-1 with ",i,j)
if(self.power==True):
print("I have power at ",i,j)
self.update_packman(i,j,actions[2])
self.power=False
i=i-1
elif(percepts[i-1][j]=='f'):
print("I found a fruit at i-1 with ",i,j)
self.update_packman(i, j, actions[2])
i=i-1
pass
def update_grid(self,a,b):
percepts[a][b]='-'
for i in range(wlm):
print(percepts[i],'\n')
pass
def update_packman(self,a,b,act):
if(act==actions[0]):
percepts[a][b+1]='p'
self.r=a
self.c=b+1
self.update_grid(a,b)
elif(act==actions[3]):
percepts[a+1][b]='p'
self.r=a+1
self.c=b
self.update_grid(a,b)
elif(act==actions[1]):
percepts[a][b-1]='p'
self.r=a
self.c=b-1
self.update_grid(a,b)
elif(act==actions[2]):
percepts[a-1][b]='p'
self.r=a-1
self.c=b
self.update_grid(a,b)
pass
Run=Agent()
for i in range(wlm):
print(percepts[i],'\n')
Comments
Post a Comment