tipc21/synchrod/ia.py

55 lines
1.2 KiB
Python

from synchrod import *
from astar import astar
def convert(board):
t = []
for i in range(4):
for c in board:
if c == WALL or (is_a(c, EXIT) and c != EXIT + i): v = -1
elif c == EXIT + i or not affects(c, i): v = 0
elif is_a(c, MONSTER): v = 1
elif is_a(c, SPIKES): v = 10
elif is_a(c, TRAP): v = 10 / 3
t.append(v)
return t
def hash(p):
return p[3] << 24 | p[2] << 16 | p[1] << 8 | p[0]
moves = [GO_UP, GO_DOWN, GO_LEFT, GO_RIGHT]
deltas = [-16, +16, -1, +1]
path = []
def turn(board, players, events):
global path
sleep(50)
if {NEW_GAME, TRAP_APPEARED, SPIKES_APPEARED}.intersection([e[2] for e in events]):
t = convert(board)
p = players.copy()
e = [0, 0, 0, 0]
l = len(board)
for i in range(4):
e[i] = board.index(EXIT + i)
if p[i] < 0: p[i] = e[i]
path = astar(t, hash(p), hash(e), l)
m = moves[path[0]]
dn = deltas[path[0]]
for i in range(4):
n = players[i]
if n < 0: continue
dest = board[n + dn]
if est_un(dest, MONSTER) and affects(dest, i):
return ATTACK
path = path[1:]
return m
play_game(turn, blind = False)