164 lines
5.8 KiB
Python
164 lines
5.8 KiB
Python
from annotationlib import type_repr
|
|
import sys
|
|
import random
|
|
from types import NoneType
|
|
import pygame as pg
|
|
from modules.agente import Agente
|
|
from modules.mapa import Mapa
|
|
import modules.estrategia as ModEstrategia
|
|
import modules.interaccoes as ModInteraccoes
|
|
|
|
flags = 0
|
|
|
|
|
|
def devolveCor(agente: Agente) -> tuple[int, int, int, int]:
|
|
if type(agente) is None:
|
|
return (0, 0, 0, 0)
|
|
else:
|
|
match agente.estrategia:
|
|
case "Neutro":
|
|
return (255, 255, 255, 0)
|
|
case "Tit4Tat":
|
|
return (255, 255, 0, 0)
|
|
case "Vingativo":
|
|
return (255, 0, 0, 0)
|
|
case "Lunatico":
|
|
return (0, 0, 255, 0)
|
|
case _:
|
|
return (0, 0, 0, 0)
|
|
|
|
|
|
def criarTabuleiro(
|
|
surface: pg.Surface,
|
|
dimensao_tabuleiro: tuple[int, int],
|
|
mapa: Mapa,
|
|
) -> None:
|
|
dimensoes_surface_principal: tuple[int, int] = surface.get_size()
|
|
max_altura: int = int((dimensoes_surface_principal[0] - 20) / dimensao_tabuleiro[0])
|
|
max_largura: int = int(
|
|
(dimensoes_surface_principal[1] - 20) / dimensao_tabuleiro[1]
|
|
)
|
|
tamanho_quadrado: int = max_altura if max_altura < max_largura else max_largura
|
|
cor_quadrado: tuple[int, int, int, int]
|
|
|
|
for pos_y in range(0, dimensao_tabuleiro[0]):
|
|
for pos_x in range(0, dimensao_tabuleiro[1]):
|
|
agente: Agente | None = mapa.posicao((pos_y, pos_x))
|
|
cor_quadrado = devolveCor(agente)
|
|
pg.draw.rect(
|
|
surface,
|
|
cor_quadrado,
|
|
[
|
|
10 + (tamanho_quadrado * pos_y),
|
|
10 + (tamanho_quadrado * pos_x),
|
|
tamanho_quadrado,
|
|
tamanho_quadrado,
|
|
],
|
|
0,
|
|
)
|
|
|
|
|
|
def popularMapa(mapa: Mapa) -> None:
|
|
for pos_y in range(0, mapa.dimensao[0]):
|
|
for pos_x in range(0, mapa.dimensao[1]):
|
|
# escolher uma estrategia aleatoria
|
|
tmp_estrategia = random.choice(ModEstrategia.listaNomesEstrategias())
|
|
# criar Agente com estrategia aleatoria e colocar na posicao y e x
|
|
mapa.mundo[pos_y][pos_x] = Agente(tmp_estrategia)
|
|
|
|
|
|
def mostrarEstatisticas(mapa: Mapa | None) -> None:
|
|
if mapa is None:
|
|
print("SEM MAPA!!")
|
|
else:
|
|
stats_estrategias: dict = {}
|
|
n_total_agentes: int = 0
|
|
print("estatisticas mapa:")
|
|
for tipo_estrategia in ModEstrategia.lista_estrategias:
|
|
stats_estrategias[tipo_estrategia] = 0
|
|
for pos_y in range(0, mapa.dimensao[0]):
|
|
for pos_x in range(0, mapa.dimensao[1]):
|
|
tmp_agente = mapa.posicao((pos_y, pos_x))
|
|
stats_estrategias[tmp_agente.estrategia] += 1
|
|
n_total_agentes += 1
|
|
for tipo_estrategia in stats_estrategias:
|
|
print(
|
|
f"{tipo_estrategia}: {(stats_estrategias[tipo_estrategia] / n_total_agentes) * 100:3.2f}"
|
|
)
|
|
|
|
|
|
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
|
|
for pos_y in range(0, mapa.dimensao[0]):
|
|
for pos_x in range(0, mapa.dimensao[1]):
|
|
tmp_agente: Agente | None = mapa.posicao((pos_x, pos_y))
|
|
if not isinstance(tmp_agente, NoneType):
|
|
print(f"{tmp_agente.estrategia[0]:2}", end="")
|
|
print("")
|
|
|
|
|
|
def main(mapa: Mapa | None, tamanho_mapa: tuple[int, int]):
|
|
if isinstance(mapa, NoneType):
|
|
raise TypeError("não foi passado mapa (tipo None)")
|
|
pg.init()
|
|
janela = pg.display.set_mode((1280, 800), flags)
|
|
pg.display.set_caption("Game Theory of Life")
|
|
clock = pg.time.Clock()
|
|
|
|
running: bool = True
|
|
# cor1: tuple[int, int, int, int] = (255, 0, 0, 0)
|
|
# cor2: tuple[int, int, int, int] = (0, 255, 0, 0)
|
|
mostrarMapa(mapa)
|
|
|
|
while running:
|
|
for event in pg.event.get():
|
|
if event.type == pg.QUIT:
|
|
running = False
|
|
if event.type == pg.KEYDOWN:
|
|
# sair do mapa com 'q' (quit)
|
|
if event.key == pg.K_q:
|
|
running = False
|
|
# mudar mapa com 'c' (change)
|
|
if event.key == pg.K_c:
|
|
novo_tamanho = int(input("tamanho do mapa: "))
|
|
tamanho_mapa = (novo_tamanho, novo_tamanho)
|
|
janela.fill(0)
|
|
mapa = Mapa(tamanho_mapa)
|
|
popularMapa(mapa)
|
|
# mostrar estatisticas do mapa com 's' (stats)
|
|
if event.key == pg.K_s:
|
|
mostrarEstatisticas(mapa)
|
|
# correr proxima iteração com 'n' (next)
|
|
if event.key == pg.K_n:
|
|
ModInteraccoes.correrInteraccoesEntreAgentes(mapa)
|
|
# mostrar mapa no terminal com 'm'
|
|
if event.key == pg.K_m:
|
|
print("Mapa:")
|
|
mostrarMapa(mapa)
|
|
if event.key == pg.K_r:
|
|
mapa = Mapa(tamanho_mapa)
|
|
popularMapa(mapa)
|
|
|
|
# interaccoes entre agentes
|
|
# primeiro criar um dicionario de interacções, atravessando todas as
|
|
# posicoes de mapa[i][j] e fazendo um hash com agente_actual.id e
|
|
# agente_vizinho.id (e seu inverso, para evitar interaccoes ja
|
|
# existentes no dicionario). adicionar essa interação se ainda não
|
|
# existir no dicionario. passar para proximo vizinho. passar para
|
|
# proxima posicao no mapa.
|
|
|
|
criarTabuleiro(janela, mapa.dimensao, mapa)
|
|
pg.display.flip()
|
|
clock.tick(60)
|
|
|
|
pg.quit()
|
|
sys.exit()
|
|
|
|
|
|
if __name__ == "__main__":
|
|
mapa_mundo: Mapa = Mapa((128, 128))
|
|
app = main(mapa_mundo, tamanho_mapa=(128, 128))
|