From ee00c85b8015114ce7358f461afb934c88ddcdbd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joanna=20Rado=C5=82a?= Date: Tue, 16 Mar 2021 15:36:24 +0100 Subject: [PATCH] added agent to the map, error handling of boxes placed at the same square --- model.py | 15 +++++++++++---- server.py | 7 +++++-- 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/model.py b/model.py index c2043f0..677f00e 100644 --- a/model.py +++ b/model.py @@ -1,7 +1,7 @@ from mesa import Model, Agent from agent import Player from mesa.time import RandomActivation -from mesa.space import SingleGrid +from mesa.space import MultiGrid #from mesa.datacollection import DataCollector import random @@ -14,17 +14,23 @@ boxes_number = 5 class GameMap(Model): def __init__(self, x, y, boxes_number): - self.grid = SingleGrid(x, y, False) + self.grid = MultiGrid(x, y, False) self.schedule = RandomActivation(self) # agenci losowo po kolei wykonują swoje akcje # to jest potrzebne przy założeniu, że potwory chodzą? self.boxes_number = boxes_number + player = Player(1, self) + x = self.random.randrange(self.grid.width) + y = self.random.randrange(self.grid.height) + self.grid.place_agent(player, (x, y)) for i in range(self.boxes_number): box = Box(i, self) self.schedule.add(box) x = self.random.randrange(self.grid.width) y = self.random.randrange(self.grid.height) - self.grid.place_agent(box, (x, y)) - player = Player(i, self) + try: + self.grid.place_agent(box, (x, y)) + except: + pass # self.datacollector=DataCollector #informacje o stanie planszy, pozycja agenta @@ -41,6 +47,7 @@ class Box(Agent): # self.y_coor = random.randrange(1, y + 1) self.closed = True self.content = 'unknown' + self.isBox=True def open(self): self.closed = False diff --git a/server.py b/server.py index 072b68c..b3673e3 100644 --- a/server.py +++ b/server.py @@ -3,15 +3,18 @@ from mesa.visualization.modules import CanvasGrid from mesa.visualization.ModularVisualization import ModularServer -def box_representation(box): +def player_representation(agent): portrayal = {"Shape": "circle", "Filled": "true", "Layer": 0, "Color": "red", "r": 0.5} + if agent.isBox: + portrayal["Color"] = "grey" + portrayal["Layer"] = 1 return portrayal -grid = CanvasGrid(box_representation, 10, 10, 500, 500) +grid = CanvasGrid(player_representation, 10, 10, 500, 500) server = ModularServer(GameMap, [grid], "Map",