I have to program simple pacman game in figure below that consisting of 4*4 grid (not GUI based).
Explaination
The starting point of pacman is cell 0 and its goal is to consume/eat maximum food pallets, while considering following given limitations.
- Pacman can move up, down, left right (keeping in view walls).
- Pacman can eat power pallets, i.e., cherries to keep ghost scared, i.e., if pacman enters the ghost cell its is not destroyed.
- Pacman keeps moving until all the power pallets are consumed.
I'm new to python and this is what I have done so far:
import numpy as np
import secrets
grid = np.array = [
['pacman', 'specialFruit', 'foodPallet', 'specialFruit'],
['foodPallet', 'enemy', 'foodPallet', 'specialFruit'],
['specialFruit', 'foodPallet', 'foodPallet', 'enemy'],
['specialFruit', 'foodPallet', 'enemy', 'foodPallet']
]
actions = ['left', 'right', 'up', 'down']
scores = {'foodPallet': 1, 'specialFruit': 2, 'enemy': 5}
I have declare a 4x4 grid. Actions(i.e, pacman can move left, right, up, down). And respective scores (i.e, if pacman has cherry it can kill ghost).
class setting:
def __init__(self):
self.score = 0
self.cherry = 0
def pac_enemy(self):
if self.cherry >= 1:
print("Pacman has killed the Enemy")
self.score = self.score + scores['enemy']
self.cherry = self.cherry - 1
else:
print("Not enough Cherries: Game Over")
score = 0
quit()
def pac_cherry(self):
print("pacman has eaten the cherry")
self.score = self.score + scores['specialFruit']
self.cherry = self.cherry + 1
def pac_food(self):
print("pacman has eaten the foodPallet")
self.score = self.score + scores['foodPallet']
def pac_scores(self):
print("Score = ", self.score)
print("Cherry = ", self.cherry)
I made a class from which i will use the function according to the situation example: if pacman move at grid location 1, I will call pac_cherry()
function and so on.
class PacMan(setting):
def game(self):
while 1:
choice = secrets.choice(actions)
if choice == 'right' or choice == 'down':
print("Pacman moving " + choice)
break
if choice == 'right':
print("Pacman is Moving ", choice, " at location 1")
if grid[0][1] == 'enemy':
self.pac_enemy()
elif grid[0][1] == 'specialFruit':
self.pac_cherry()
elif grid[0][1] == 'foodPallet':
self.pac_food()
grid[0][1] = 'pacman'
grid[0][0] = ''
self.pac_scores()
print(grid)
I am moving pacman randomly as If oacman is at location 0 it can only move location 1 or location 4 and so on.
Basically, by doing this method there will be so many states for pacman(agent). I'm confused and not sure how will It be work. Kindly suggest me any other method or some tips, algorithm, or suggest me a way to complete it efficiently.