alterada interação com tecla c (change)
agora a tecla c (change) permite alterar o tamanho do mapa. para fazer reset, usa-se agora a tecla 'r' (reset)
This commit is contained in:
@ -6,7 +6,7 @@ import modules.estrategia as ModEstrategia
|
|||||||
|
|
||||||
tamanho_mundo: tuple[int, int] = (30, 30)
|
tamanho_mundo: tuple[int, int] = (30, 30)
|
||||||
|
|
||||||
ModEstrategia.carregarFicheirosEstrategias('strategies/')
|
ModEstrategia.carregarFicheirosEstrategias("strategies/")
|
||||||
|
|
||||||
# criar mapa com tamanho_mundo
|
# criar mapa com tamanho_mundo
|
||||||
mapa = Mapa(tamanho_mundo)
|
mapa = Mapa(tamanho_mundo)
|
||||||
@ -20,4 +20,4 @@ for pos_y in range(0, mapa.dimensao[0]):
|
|||||||
|
|
||||||
|
|
||||||
# Gui.App(mapa)
|
# Gui.App(mapa)
|
||||||
Gui.main(mapa)
|
Gui.main(mapa, tamanho_mundo)
|
||||||
|
|||||||
@ -1,19 +1,20 @@
|
|||||||
|
from types import NoneType
|
||||||
import modules.estrategia as ModEstrategia
|
import modules.estrategia as ModEstrategia
|
||||||
|
|
||||||
|
|
||||||
class Agente():
|
class Agente:
|
||||||
# traços de personalidade
|
# traços de personalidade
|
||||||
personalidade: dict[str, float] | None = {
|
personalidade: dict[str, float] = {
|
||||||
'amabilidade': 0.0,
|
"amabilidade": 0.0,
|
||||||
'retaliacao': 0.0,
|
"retaliacao": 0.0,
|
||||||
'perdao': 0.0,
|
"perdao": 0.0,
|
||||||
'coerencia': 0.0,
|
"coerencia": 0.0,
|
||||||
'memoria': 0.0,
|
"memoria": 0.0,
|
||||||
}
|
}
|
||||||
pontuacao: int = 0
|
pontuacao: int = 0
|
||||||
estrategia: str | None = None
|
estrategia: str | None = None
|
||||||
id_agente: int = 0
|
id_agente: int = 0
|
||||||
memoria: dict = {}
|
banco_memoria: dict[int, list] = {}
|
||||||
|
|
||||||
def __init__(self, estrategia: str):
|
def __init__(self, estrategia: str):
|
||||||
# definir estrategia aleatoriamente
|
# definir estrategia aleatoriamente
|
||||||
@ -26,7 +27,22 @@ class Agente():
|
|||||||
self.pontuacao = 0
|
self.pontuacao = 0
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return f'Dados Agente ({self.id_agente})\n \
|
return f"Dados Agente ({self.id_agente})\n \
|
||||||
\testrategia: {self.estrategia}\n \
|
\testrategia: {self.estrategia}\n \
|
||||||
\t\tpersonalidade: {self.personalidade}\n \
|
\t\tpersonalidade: {self.personalidade}\n \
|
||||||
\tpontução: {self.pontuacao}'
|
\tpontução: {self.pontuacao}"
|
||||||
|
|
||||||
|
def amabilidade(self) -> float:
|
||||||
|
return self.personalidade["amabilidade"]
|
||||||
|
|
||||||
|
def retaliacao(self) -> float:
|
||||||
|
return self.personalidade["retaliacao"]
|
||||||
|
|
||||||
|
def perdao(self) -> float:
|
||||||
|
return self.personalidade["perdao"]
|
||||||
|
|
||||||
|
def coerencia(self) -> float:
|
||||||
|
return self.personalidade["coerencia"]
|
||||||
|
|
||||||
|
def memoria(self) -> float:
|
||||||
|
return self.personalidade["memoria"]
|
||||||
|
|||||||
@ -2,6 +2,7 @@
|
|||||||
from enum import Enum
|
from enum import Enum
|
||||||
import json
|
import json
|
||||||
import os
|
import os
|
||||||
|
from types import NoneType
|
||||||
|
|
||||||
|
|
||||||
class TiposEstrategia(Enum):
|
class TiposEstrategia(Enum):
|
||||||
@ -14,13 +15,13 @@ class TiposEstrategia(Enum):
|
|||||||
|
|
||||||
# lista de estratégias. cada entrada é um dicionário com nome de estratégia
|
# lista de estratégias. cada entrada é um dicionário com nome de estratégia
|
||||||
# e respectivos tracos de personalidade
|
# e respectivos tracos de personalidade
|
||||||
lista_estrategias: dict = {}
|
lista_estrategias: dict[str, float] = {}
|
||||||
|
|
||||||
|
|
||||||
def carregarFicheirosEstrategias(pasta_estrategias: str) -> None:
|
def carregarFicheirosEstrategias(pasta_estrategias: str) -> None:
|
||||||
lista_ficheiros: list = os.listdir(pasta_estrategias)
|
lista_ficheiros: list = os.listdir(pasta_estrategias)
|
||||||
for ficheiro_json in lista_ficheiros:
|
for ficheiro_json in lista_ficheiros:
|
||||||
with open(f'{pasta_estrategias}/{ficheiro_json}', 'r') as ficheiro:
|
with open(f"{pasta_estrategias}/{ficheiro_json}", "r") as ficheiro:
|
||||||
dados = json.load(ficheiro)
|
dados = json.load(ficheiro)
|
||||||
lista_estrategias.update(dados)
|
lista_estrategias.update(dados)
|
||||||
|
|
||||||
@ -29,7 +30,8 @@ def listaNomesEstrategias() -> list:
|
|||||||
return list(lista_estrategias.keys())
|
return list(lista_estrategias.keys())
|
||||||
|
|
||||||
|
|
||||||
def devolvePersonalidade(nome_estrategia: str) -> dict[str, float] | None:
|
def devolvePersonalidade(nome_estrategia: str | None) -> dict[str, float]:
|
||||||
personalidade: dict[str, float] | None = lista_estrategias.get(
|
if isinstance(nome_estrategia, NoneType):
|
||||||
nome_estrategia)
|
raise ValueError("estratégia {nome_estrategia=} não é válida")
|
||||||
|
personalidade: dict[str, float] = lista_estrategias.get(nome_estrategia)
|
||||||
return personalidade
|
return personalidade
|
||||||
|
|||||||
@ -1,5 +1,6 @@
|
|||||||
import sys
|
import sys
|
||||||
import random
|
import random
|
||||||
|
from types import NoneType
|
||||||
import pygame as pg
|
import pygame as pg
|
||||||
from modules.agente import Agente
|
from modules.agente import Agente
|
||||||
from modules.mapa import Mapa
|
from modules.mapa import Mapa
|
||||||
@ -35,7 +36,8 @@ def criarTabuleiro(
|
|||||||
max_altura: int = int(
|
max_altura: int = int(
|
||||||
(dimensoes_surface_principal[0] - 20) / dimensao_tabuleiro[0])
|
(dimensoes_surface_principal[0] - 20) / dimensao_tabuleiro[0])
|
||||||
max_largura: int = int(
|
max_largura: int = int(
|
||||||
(dimensoes_surface_principal[1]-20)/dimensao_tabuleiro[1])
|
(dimensoes_surface_principal[1] - 20) / dimensao_tabuleiro[1]
|
||||||
|
)
|
||||||
tamanho_quadrado: int = max_altura if max_altura < max_largura else max_largura
|
tamanho_quadrado: int = max_altura if max_altura < max_largura else max_largura
|
||||||
cor_quadrado: tuple[int, int, int, int]
|
cor_quadrado: tuple[int, int, int, int]
|
||||||
|
|
||||||
@ -43,12 +45,17 @@ def criarTabuleiro(
|
|||||||
for pos_x in range(0, dimensao_tabuleiro[1]):
|
for pos_x in range(0, dimensao_tabuleiro[1]):
|
||||||
agente: Agente | None = mapa.posicao((pos_y, pos_x))
|
agente: Agente | None = mapa.posicao((pos_y, pos_x))
|
||||||
cor_quadrado = devolveCor(agente)
|
cor_quadrado = devolveCor(agente)
|
||||||
pg.draw.rect(surface,
|
pg.draw.rect(
|
||||||
|
surface,
|
||||||
cor_quadrado,
|
cor_quadrado,
|
||||||
[10+(tamanho_quadrado*pos_y),
|
[
|
||||||
|
10 + (tamanho_quadrado * pos_y),
|
||||||
10 + (tamanho_quadrado * pos_x),
|
10 + (tamanho_quadrado * pos_x),
|
||||||
tamanho_quadrado, tamanho_quadrado],
|
tamanho_quadrado,
|
||||||
0)
|
tamanho_quadrado,
|
||||||
|
],
|
||||||
|
0,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
def popularMapa(mapa: Mapa) -> None:
|
def popularMapa(mapa: Mapa) -> None:
|
||||||
@ -76,21 +83,27 @@ def mostrarEstatisticas(mapa: Mapa | None) -> None:
|
|||||||
stats_estrategias[tmp_agente.estrategia] += 1
|
stats_estrategias[tmp_agente.estrategia] += 1
|
||||||
n_total_agentes += 1
|
n_total_agentes += 1
|
||||||
for tipo_estrategia in stats_estrategias:
|
for tipo_estrategia in stats_estrategias:
|
||||||
print(f"{tipo_estrategia}: {
|
print(
|
||||||
(stats_estrategias[tipo_estrategia]/n_total_agentes)*100:3.2f}")
|
f"{tipo_estrategia}: {
|
||||||
|
(stats_estrategias[tipo_estrategia] / n_total_agentes) * 100:3.2f
|
||||||
|
}"
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
def mostrarMapa(mapa: Mapa) -> None:
|
def mostrarMapa(mapa: Mapa | None) -> None:
|
||||||
|
# verificacao
|
||||||
|
if isinstance(mapa, NoneType):
|
||||||
|
raise TypeError("objecto passado do tipo None")
|
||||||
# mostrar mapa com pontuação em cada posição
|
# mostrar mapa com pontuação em cada posição
|
||||||
for pos_y in range(0, mapa.dimensao[0]):
|
for pos_y in range(0, mapa.dimensao[0]):
|
||||||
for pos_x in range(0, mapa.dimensao[1]):
|
for pos_x in range(0, mapa.dimensao[1]):
|
||||||
tmp_agente: Agente | None = mapa.posicao((pos_x, pos_y))
|
tmp_agente: Agente | None = mapa.posicao((pos_x, pos_y))
|
||||||
if type(tmp_agente) is not None:
|
if not isinstance(tmp_agente, NoneType):
|
||||||
print(f"{tmp_agente.estrategia[0]:2}", end='')
|
print(f"{tmp_agente.estrategia[0]:2}", end="")
|
||||||
print("")
|
print("")
|
||||||
|
|
||||||
|
|
||||||
def main(mapa: Mapa | None):
|
def main(mapa: Mapa | None, tamanho_mapa: tuple[int, int]):
|
||||||
if mapa is None:
|
if mapa is None:
|
||||||
print("SEM MAPA!!")
|
print("SEM MAPA!!")
|
||||||
pg.init()
|
pg.init()
|
||||||
@ -99,8 +112,8 @@ def main(mapa: Mapa | None):
|
|||||||
clock = pg.time.Clock()
|
clock = pg.time.Clock()
|
||||||
|
|
||||||
running: bool = True
|
running: bool = True
|
||||||
cor1: tuple[int, int, int, int] = (255, 0, 0, 0)
|
# cor1: tuple[int, int, int, int] = (255, 0, 0, 0)
|
||||||
cor2: tuple[int, int, int, int] = (0, 255, 0, 0)
|
# cor2: tuple[int, int, int, int] = (0, 255, 0, 0)
|
||||||
mostrarMapa(mapa)
|
mostrarMapa(mapa)
|
||||||
|
|
||||||
while running:
|
while running:
|
||||||
@ -108,18 +121,29 @@ def main(mapa: Mapa | None):
|
|||||||
if event.type == pg.QUIT:
|
if event.type == pg.QUIT:
|
||||||
running = False
|
running = False
|
||||||
if event.type == pg.KEYDOWN:
|
if event.type == pg.KEYDOWN:
|
||||||
|
# sair do mapa com 'q' (quit)
|
||||||
if event.key == pg.K_q:
|
if event.key == pg.K_q:
|
||||||
running = False
|
running = False
|
||||||
|
# mudar mapa com 'c' (change)
|
||||||
if event.key == pg.K_c:
|
if event.key == pg.K_c:
|
||||||
mapa = Mapa((30, 30))
|
novo_tamanho = int(input("tamanho do mapa: "))
|
||||||
|
tamanho_mapa = (novo_tamanho, novo_tamanho)
|
||||||
|
janela.fill(0)
|
||||||
|
mapa = Mapa(tamanho_mapa)
|
||||||
popularMapa(mapa)
|
popularMapa(mapa)
|
||||||
|
# mostrar estatisticas do mapa com 's' (stats)
|
||||||
if event.key == pg.K_s:
|
if event.key == pg.K_s:
|
||||||
mostrarEstatisticas(mapa)
|
mostrarEstatisticas(mapa)
|
||||||
|
# correr proxima iteração com 'n' (next)
|
||||||
if event.key == pg.K_n:
|
if event.key == pg.K_n:
|
||||||
ModInteraccoes.correrInteraccoesEntreAgentes(mapa)
|
ModInteraccoes.correrInteraccoesEntreAgentes(mapa)
|
||||||
|
# mostrar mapa no terminal com 'm'
|
||||||
if event.key == pg.K_m:
|
if event.key == pg.K_m:
|
||||||
print("Mapa:")
|
print("Mapa:")
|
||||||
mostrarMapa(mapa)
|
mostrarMapa(mapa)
|
||||||
|
if event.key == pg.K_r:
|
||||||
|
mapa = Mapa(tamanho_mapa)
|
||||||
|
popularMapa(mapa)
|
||||||
|
|
||||||
# (367, 250)
|
# (367, 250)
|
||||||
criarTabuleiro(janela, mapa.dimensao, mapa)
|
criarTabuleiro(janela, mapa.dimensao, mapa)
|
||||||
@ -130,6 +154,6 @@ def main(mapa: Mapa | None):
|
|||||||
sys.exit()
|
sys.exit()
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == "__main__":
|
||||||
mapa_mundo: Mapa = Mapa((10, 10))
|
mapa_mundo: Mapa = Mapa((10, 10))
|
||||||
app = main(mapa_mundo)
|
app = main(mapa_mundo)
|
||||||
|
|||||||
Reference in New Issue
Block a user