169 lines
5.6 KiB
Python
169 lines
5.6 KiB
Python
import random
|
|
|
|
import pygame
|
|
from Constants import SQUARE_SIZE, GREEN, RIGHT, LEFT, UP, DOWN, COLS, ROWS, DECOY, ATOMIC_BOMB, CHEMICAL_BOMB, MOVE, CLAYMORE, LAND_MINE
|
|
from Engine.Board import Board
|
|
from Engine.Agent import Agent
|
|
from Engine.BombFactory import BombFactory
|
|
from Engine.Point import Point
|
|
from Engine.Stone import Stone
|
|
from Engine.BfsPathFinder import BfsPathFinder
|
|
from Engine.State import State
|
|
from Engine.PathFinder import PathFinder
|
|
from Engine.Mud import Mud
|
|
from Engine.DecisionTree import DecisionTree
|
|
|
|
class Game:
|
|
def __init__(self, win):
|
|
self._init(win)
|
|
self.win = win
|
|
self.randomizeObject()
|
|
pygame.display.update()
|
|
|
|
def _init(self, win):
|
|
self.board = Board(win)
|
|
self.agent = Agent(Point(0, 0))
|
|
self.turn = GREEN
|
|
self.goingDown = True
|
|
self.path = []
|
|
self.dTree = DecisionTree(False)
|
|
|
|
def update(self):
|
|
self.defuseBomb()
|
|
self.board.drawSquares(self.win)
|
|
self.board.drawBombs()
|
|
self.board.drawStones()
|
|
self.board.drawMud()
|
|
self.board.drawAgent(self.win, self.agent)
|
|
pygame.display.update()
|
|
|
|
# def move(self):
|
|
# point = self.agent.getPoint()
|
|
# tmpPoint = Point(point.getX(), point.getY())
|
|
# if self.goingDown:
|
|
# if point.getY() + 1 < ROWS:
|
|
# tmpPoint.y += 1
|
|
# elif point.getX() + 1 < COLS:
|
|
# tmpPoint.x += 1
|
|
# self.goingDown = not self.goingDown
|
|
# else:
|
|
# if point.getY() - 1 >= 0:
|
|
# tmpPoint.y -= 1
|
|
# elif point.getX() + 1 < COLS:
|
|
# tmpPoint.x += 1
|
|
# self.goingDown = not self.goingDown
|
|
# self.agent.rotateImage(tmpPoint)
|
|
# self.agent.point = tmpPoint
|
|
# #self.moveSequence()
|
|
#
|
|
# def moveSequence(self):
|
|
# pathfinder = PathFinder(self.board)
|
|
# for point in pathfinder.findPath(Point(0,0), Point(5,5)):
|
|
# self.agent.point = point
|
|
# self.update()
|
|
|
|
|
|
def randomizeObject(self):
|
|
i = 0
|
|
while i < 10:
|
|
point = Point(random.randint(0, 9), random.randint(0, 9))
|
|
if(point.getX() == 0 and point.getY() == 0):
|
|
continue;
|
|
if point not in self.board.bombMap:
|
|
object = self.pickObject(random.randint(0, 4))
|
|
self.board.bombMap[point] = object
|
|
i += 1
|
|
r = 5
|
|
j = 0
|
|
while j < r:
|
|
point = Point(random.randint(0, 9), random.randint(0, 9))
|
|
if (point.getX() == 0 and point.getY() == 0):
|
|
continue
|
|
if point not in self.board.bombMap and point not in self.board.stoneMap:
|
|
object = Stone()
|
|
self.board.stoneMap[point] = object
|
|
j += 1
|
|
k = 0
|
|
r = 10
|
|
while k < r:
|
|
point = Point(random.randint(0, 9), random.randint(0, 9))
|
|
if (point.getX() == 0 and point.getY() == 0):
|
|
continue
|
|
if point not in self.board.bombMap and point not in self.board.stoneMap and point not in self.board.mudMap:
|
|
object = Mud()
|
|
self.board.mudMap[point] = object
|
|
k += 1
|
|
|
|
|
|
def pickObject(self, rand):
|
|
if rand == 0:
|
|
return BombFactory.create(DECOY)
|
|
elif rand == 1:
|
|
return BombFactory.create(CHEMICAL_BOMB)
|
|
elif rand == 2:
|
|
return BombFactory.create(ATOMIC_BOMB)
|
|
elif rand == 3:
|
|
return BombFactory.create(CLAYMORE)
|
|
elif rand == 4:
|
|
return BombFactory.create(LAND_MINE)
|
|
|
|
def findBomb(self):
|
|
# return BfsPathFinder(self.board).findBomb(State(self.agent.getOrientation(),Point(self.agent.getPoint().getX(),self.agent.getPoint().getY())))
|
|
return PathFinder(self.board,self.dTree).findBomb(State(self.agent.getOrientation(),Point(self.agent.getPoint().getX(),self.agent.getPoint().getY())))
|
|
|
|
|
|
def finalState(self):
|
|
if len(self.board.bombMap) == 0:
|
|
return True
|
|
return False
|
|
|
|
def moveToNext(self):
|
|
action = self.path.pop(0)
|
|
self.doAction(action)
|
|
|
|
|
|
def savePath(self,path):
|
|
self.path = path
|
|
|
|
def getPath(self):
|
|
return self.path
|
|
|
|
def defuseBomb(self):
|
|
point = self.agent.getPoint()
|
|
bomb = self.board.getBomb(point)
|
|
self.agent.defuse(bomb)
|
|
if point in self.board.bombMap:
|
|
self.board.bombMap.pop(point)
|
|
|
|
def doAction(self,action):
|
|
if action == RIGHT:
|
|
if self.agent.orientation == RIGHT:
|
|
self.agent.orientation = DOWN
|
|
elif self.agent.orientation == LEFT:
|
|
self.agent.orientation = UP
|
|
elif self.agent.orientation == DOWN:
|
|
self.agent.orientation = LEFT
|
|
else:
|
|
self.agent.orientation = RIGHT
|
|
elif action == LEFT:
|
|
if self.agent.orientation == RIGHT:
|
|
self.agent.orientation = UP
|
|
elif self.agent.orientation == LEFT:
|
|
self.agent.orientation = DOWN
|
|
elif self.agent.orientation == DOWN:
|
|
self.agent.orientation = RIGHT
|
|
else:
|
|
self.agent.orientation = LEFT
|
|
elif action == MOVE:
|
|
if self.agent.orientation == RIGHT:
|
|
self.agent.point.x += 1
|
|
elif self.agent.orientation == LEFT:
|
|
self.agent.point.x -= 1
|
|
elif self.agent.orientation == DOWN:
|
|
self.agent.point.y += 1
|
|
else:
|
|
self.agent.point.y -= 1
|
|
|
|
|
|
|