SIP/core/board.py

162 lines
5.9 KiB
Python

import random
from core.constans import *
from core.decision_tree import DecisionTree
from core.neural_network import Neural_network
from core.tree import Tree
from core.mushroom import Mushroom
from core.agent import Agent
from core.a_star import a_star, bfs, successor, get_cost
class Board:
def __init__(self, x=0, y=0, width=WIDTH, height=HEIGHT, row=ROWS, col=COLUMNS):
self.x = x
self.y = y
self.width = width
self.height = height
self.row = row
self.col = col
self.square_size = (self.width / self.col, self.height / self.row)
self.board = [[False for j in range(self.row)] for i in range(self.col)]
self.free_spaces = []
self.update_free_spaces()
self.dt = DecisionTree(0.05)
self.dt.learn()
self.nn = Neural_network()
self.nn.build_model()
self.agent = Agent(self.free_spaces.pop(), self.square_size)
print(f"Board {col}x{row} with agent on ({self.agent.col},{self.agent.row}) {self.agent.angle}")
self.surface = pygame.Surface((self.width, self.height))
for i in range(TREES):
x_y = self.free_spaces.pop()
self.board[x_y[0]][x_y[1]] = Tree(x_y, random.randint(0, 8))
for i in range(MUSHROOMS_START):
x_y = self.free_spaces.pop()
self.board[x_y[0]][x_y[1]] = Mushroom(x_y, random.randint(0, 2))
for i in range(POISON_MUSHROOMS_START):
x_y = self.free_spaces.pop()
self.board[x_y[0]][x_y[1]] = Mushroom(x_y, random.randint(0, 2))
self.grow_next = 0
self.path = []
self.actions = []
def update_free_spaces(self):
self.free_spaces = []
for x in range(self.col):
for y in range(self.row):
if self.board[x][y] == False: self.free_spaces.append((x, y))
random.shuffle(self.free_spaces)
def draw_squares(self, win):
self.surface.fill(GREEN_2)
for x in range(self.col):
for y in range(self.row):
if (x + y) % 2 == 0:
pygame.draw.rect(self.surface, GREEN, (
x * self.square_size[0], y * self.square_size[1], self.square_size[0], self.square_size[1]))
win.blit(self.surface, (self.x, self.y))
def draw_agent(self, win):
self.agent.draw(self.surface)
win.blit(self.surface, (self.x, self.y))
def draw_pieces(self, win):
for x in range(self.col):
for y in range(self.row):
if self.board[x][y] != False:
self.board[x][y].draw(self.surface, self.square_size)
win.blit(self.surface, (self.x, self.y))
def draw_info(self, win):
myfont = pygame.font.SysFont('Comic Sans MS', 30)
textsurface = myfont.render(str(self.agent.points), False, (0, 0, 0))
win.blit(textsurface, (self.x + self.width - textsurface.get_width() - 5, self.y - 40))
# kolumna agneta
textsurface = myfont.render("c: " + str(self.agent.col), False, (0, 0, 0))
win.blit(textsurface, (self.x + 1, self.y - 40))
# wiersz agneta
textsurface = myfont.render("r: " + str(self.agent.row), False, (0, 0, 0))
win.blit(textsurface, (self.x + 100, self.y - 40))
# kierunek agenta
textsurface = myfont.render("a: " + str(self.agent.angle), False, (0, 0, 0))
win.blit(textsurface, (self.x + 200, self.y - 40))
def update_agent(self):
self.agent.update_animation()
if self.actions and self.agent.moving == self.agent.rotating == 0:
action = self.actions.pop(0)
if action == "move":
self.agent.move()
elif action == "rotate_left":
self.agent.rotate(1)
else:
self.agent.rotate(-1)
# zebranie grzyba
if self.board[self.agent.col][self.agent.row] != False and self.board[self.agent.col][
self.agent.row].name == "Mushroom" and self.board[self.agent.col][self.agent.row].grow == 100:
self.agent.points += self.board[self.agent.col][self.agent.row].points
self.board[self.agent.col][self.agent.row] = False
self.free_spaces.append((self.agent.col, self.agent.row))
def grow_mushrooms(self):
numer = 0
# numer_poison = 0
for x in range(self.col):
for y in range(self.row):
if self.board[x][y] != False and self.board[x][y].name == "Mushroom":
if self.board[x][y].grow < 100: self.board[x][y].grow += 1
# if self.board[x][y].poison:
# numer_poison += 1
# else:
numer += 1
if self.grow_next < 100:
self.grow_next += 1
elif random.random() > 0.5 and numer < MUSHROOMS_MAX:
self.grow_next = 0
self.update_free_spaces()
x_y = self.free_spaces.pop()
self.board[x_y[0]][x_y[1]] = Mushroom(x_y, random.randint(0, 2), 0)
# elif numer_poison < POISON_MUSHROOMS_MAX:
# self.grow_next = 0
# self.update_free_spaces()
# x_y = self.free_spaces.pop()
# self.board[x_y[0]][x_y[1]] = Mushroom(x_y, random.randint(0, 2), 0)
def a_starxd(self):
print(self.agent.angle)
fringe = []
explored = []
self.actions = a_star(self,
fringe,
explored,
(self.agent.col, self.agent.row, self.agent.angle),
successor,
get_cost,
(bfs((self.agent.col, self.agent.row, self.agent.angle), successor, self))
)
print(self.actions)
return self.actions