Projekt_AI-Automatyczny_saper/Engine/Game.py

203 lines
6.8 KiB
Python
Raw Permalink Normal View History

2021-03-27 22:07:55 +01:00
import random
2021-03-13 21:16:35 +01:00
import pygame
from Constants import SQUARE_SIZE, GREEN, RIGHT, LEFT, UP, DOWN, COLS, ROWS, DECOY, ATOMIC_BOMB, CHEMICAL_BOMB, MOVE, CLAYMORE, LAND_MINE, DEFUSE, DETONATE, POLIGON, AGENT
2021-03-16 18:53:26 +01:00
from Engine.Board import Board
from Engine.Agent import Agent
2021-03-27 22:07:55 +01:00
from Engine.BombFactory import BombFactory
2021-06-14 22:20:20 +02:00
from Engine.GeneticFinder import GeneticFinder
2021-03-27 22:07:55 +01:00
from Engine.Point import Point
2021-03-27 22:16:28 +01:00
from Engine.Stone import Stone
2021-04-13 21:27:51 +02:00
from Engine.BfsPathFinder import BfsPathFinder
2021-04-26 20:24:52 +02:00
from Engine.State import State
2021-04-27 21:10:01 +02:00
from Engine.PathFinder import PathFinder
2021-04-27 22:29:50 +02:00
from Engine.Mud import Mud
2021-05-18 00:21:14 +02:00
from Engine.DecisionTree import DecisionTree
from Engine.PoligonPath import PoligonPath
2021-03-13 21:16:35 +01:00
2021-05-18 19:34:52 +02:00
2021-03-13 21:16:35 +01:00
class Game:
def __init__(self, win):
2021-03-27 22:07:55 +01:00
self._init(win)
2021-03-13 21:16:35 +01:00
self.win = win
2021-03-27 22:07:55 +01:00
self.randomizeObject()
pygame.display.update()
2021-03-13 21:16:35 +01:00
2021-03-30 21:01:54 +02:00
def _init(self, win):
2021-03-27 22:07:55 +01:00
self.board = Board(win)
2021-03-30 21:01:54 +02:00
self.agent = Agent(Point(0, 0))
2021-03-13 21:16:35 +01:00
self.turn = GREEN
2021-03-16 18:53:26 +01:00
self.goingDown = True
2021-04-13 21:27:51 +02:00
self.path = []
2021-05-18 19:01:17 +02:00
self.dTree = DecisionTree(False)
2021-03-16 18:53:26 +01:00
2021-03-13 21:16:35 +01:00
def update(self):
2021-03-15 19:58:20 +01:00
self.board.drawSquares(self.win)
2021-04-12 22:54:33 +02:00
self.board.drawStones()
2021-04-27 22:29:50 +02:00
self.board.drawMud()
self.board.drawBombs()
2021-03-30 21:01:54 +02:00
self.board.drawAgent(self.win, self.agent)
2021-03-13 21:16:35 +01:00
pygame.display.update()
2021-04-13 21:27:51 +02:00
# 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()
2021-04-12 22:54:33 +02:00
2021-03-27 22:07:55 +01:00
def randomizeObject(self):
2021-03-30 23:10:20 +02:00
i = 0
2021-04-13 21:27:51 +02:00
while i < 10:
2021-04-12 22:54:33 +02:00
point = Point(random.randint(0, 9), random.randint(0, 9))
2021-03-30 23:10:20 +02:00
if(point.getX() == 0 and point.getY() == 0):
2021-06-14 22:20:20 +02:00
continue
2021-03-27 22:07:55 +01:00
if point not in self.board.bombMap:
2021-03-27 22:16:28 +01:00
object = self.pickObject(random.randint(0, 4))
self.board.bombMap[point] = object
2021-03-30 23:10:20 +02:00
i += 1
2021-04-13 19:32:08 +02:00
r = 5
2021-04-12 22:54:33 +02:00
j = 0
while j < r:
point = Point(random.randint(0, 9), random.randint(0, 9))
if (point.getX() == 0 and point.getY() == 0):
2021-04-27 21:10:01 +02:00
continue
2021-04-12 22:54:33 +02:00
if point not in self.board.bombMap and point not in self.board.stoneMap:
object = Stone()
self.board.stoneMap[point] = object
j += 1
2021-04-27 22:29:50 +02:00
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
2021-04-12 22:54:33 +02:00
2021-03-27 22:07:55 +01:00
2021-03-27 22:16:28 +01:00
def pickObject(self, rand):
2021-03-30 21:01:54 +02:00
if rand == 0:
2021-03-27 22:07:55 +01:00
return BombFactory.create(DECOY)
2021-03-30 21:01:54 +02:00
elif rand == 1:
2021-03-27 22:07:55 +01:00
return BombFactory.create(CHEMICAL_BOMB)
2021-03-30 21:01:54 +02:00
elif rand == 2:
2021-03-27 22:07:55 +01:00
return BombFactory.create(ATOMIC_BOMB)
2021-03-30 21:01:54 +02:00
elif rand == 3:
2021-03-27 22:07:55 +01:00
return BombFactory.create(CLAYMORE)
2021-03-30 21:01:54 +02:00
elif rand == 4:
2021-03-27 22:07:55 +01:00
return BombFactory.create(LAND_MINE)
2021-04-12 22:54:33 +02:00
2021-04-13 19:32:08 +02:00
def findBomb(self):
self.agent.changeImage(AGENT)
2021-04-27 21:10:01 +02:00
# return BfsPathFinder(self.board).findBomb(State(self.agent.getOrientation(),Point(self.agent.getPoint().getX(),self.agent.getPoint().getY())))
2021-05-18 00:21:14 +02:00
return PathFinder(self.board,self.dTree).findBomb(State(self.agent.getOrientation(),Point(self.agent.getPoint().getX(),self.agent.getPoint().getY())))
2021-04-27 21:10:01 +02:00
2021-06-14 22:20:20 +02:00
def getBombPath(self, point):
self.agent.changeImage(AGENT)
return GeneticFinder(self.board,self.dTree).bombDistance(State(self.agent.getOrientation(),Point(self.agent.getPoint().getX(),self.agent.getPoint().getY())), State(None,point))
2021-04-13 19:32:08 +02:00
2021-04-12 22:54:33 +02:00
def finalState(self):
if len(self.board.bombMap) == 0 and len(self.path) == 0:
2021-04-13 21:27:51 +02:00
return True
return False
def moveToNext(self):
2021-04-26 21:51:08 +02:00
action = self.path.pop(0)
self.doAction(action)
2021-04-26 23:24:59 +02:00
2021-04-13 21:27:51 +02:00
def savePath(self,path):
self.path = path
def getPath(self):
return self.path
def defuseBomb(self):
point = self.agent.getPoint()
2021-05-18 00:21:14 +02:00
bomb = self.board.getBomb(point)
self.agent.defuse(bomb)
2021-04-13 21:27:51 +02:00
if point in self.board.bombMap:
self.board.bombMap.pop(point)
2021-04-26 21:51:08 +02:00
def doAction(self,action):
2021-04-27 21:10:01 +02:00
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
2021-05-18 19:34:52 +02:00
elif action == DEFUSE:
self.defuseAction()
elif action == DETONATE:
self.detonateAction()
elif action == POLIGON:
self.poligionAction()
def defuseAction(self):
self.agent.changeImage(DEFUSE)
self.update()
2021-05-18 19:34:52 +02:00
self.defuseBomb()
def detonateAction(self):
self.agent.changeImage(DETONATE)
self.update()
self.defuseBomb()
2021-05-18 19:34:52 +02:00
def poligionAction(self):
self.defuseBomb()
self.update()
2021-06-14 22:20:20 +02:00
self.defuseBomb()
# self.agent.changeImage(POLIGON)
# self.update()
# path = PoligonPath(self.board, self.dTree).findBomb(
# State(self.agent.getOrientation(), Point(self.agent.getPoint().getX(), self.agent.getPoint().getY())))
# self.savePath(path)
2021-04-27 21:10:01 +02:00
2021-04-26 21:51:08 +02:00