-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathamazons.py
58 lines (48 loc) · 1.96 KB
/
amazons.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import itertools
import utils
def mover_iter():
g.start_game()
yield None
for player in itertools.islice(itertools.cycle(g.players), 2000):
best_move = player.find_best_move()
if best_move:
g.plot.title_artist.set_text(f'{player.name} to move')
piece, to, arrow = best_move
print(f'found best move?! '
f'{utils.print_pos(player.pieces[piece].i, player.pieces[piece].j)} - '
f'{utils.print_pos(*to)} - '
f'{utils.print_pos(*arrow)}')
g.make_move(player.i_player, piece, *to, *arrow)
else:
print(f'Player {player.name} loses!')
g.plot.title_artist.set_text(f'{player.name} loses!')
break
yield None
while True:
yield None
if __name__ == "__main__":
with_plot = True
g = utils.Game('B2E5', (6, 6), with_plot=with_plot)
mover = mover_iter()
def init_plot():
pieces_artists = (item.artist for p in g.players for item in p.pieces)
arrow_artists = (arrow.artist for arrow in g.arrows)
text_artists = (g.plot.title_artist, )
return tuple(itertools.chain(pieces_artists, arrow_artists, text_artists))
def update(_):
next(mover)
pieces_artists = (item.artist for p in g.players for item in p.pieces)
arrow_artists = (arrow.artist for arrow in g.arrows)
text_artists = (g.plot.title_artist, )
return tuple(itertools.chain(pieces_artists, arrow_artists, text_artists))
if with_plot:
from matplotlib import pyplot as plt
from matplotlib import animation
frames = g.board.m * g.board.n
ani = animation.FuncAnimation(g.plot.fig, update, frames=frames, interval=100,
init_func=init_plot, blit=True)
ani.save('animation.mp4', fps=10, extra_args=['-vcodec', 'libx264'])
plt.show()
else:
for _ in mover:
g.board.print()