This repository has been archived by the owner on May 2, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
play_gomoku.py
52 lines (43 loc) · 1.58 KB
/
play_gomoku.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
import os
from alphazero.agents.alphabeta import AlphaBetaAgent
from alphazero.games.gomoku import GomokuPlayer, GomokuMove, GomokuGame
from alphazero.games.gomoku.eval_functions import simple_eval_func
def clear():
if os.name == 'nt':
os.system('cls')
else:
os.system('clear')
def read_move(player: GomokuPlayer) -> GomokuMove:
x, y = input(f'{player.name} move: ').split()
x, y = int(x), int(y)
return GomokuMove(x, y)
game = GomokuGame(7, 5)
agent = AlphaBetaAgent(GomokuPlayer.WHITE, depth=2, eval_fn=simple_eval_func)
while not game.is_over:
clear()
game.show_board()
# print(f"current state score by eval func: {agent.eval_fn(game.state, agent.player)}")
move = read_move(game.current_player)
while not game.state.is_legal_move(move):
print('Illegal move, try again')
move = read_move(game.current_player)
clear()
# if game.current_player == GomokuPlayer.BLACK:
# move = read_move(game.current_player)
# while not game.state.is_legal_move(move):
# print('Illegal move, try again')
# move = read_move(game.current_player)
# clear()
# else:
# print(f"legal moves: {', '.join([str(m) for m in game.state.get_legal_moves()])}")
# move = agent.select_move(game.state)
# clear()
# print(f'Agent WHITE move: {move}')
game.play(move)
print('--- GAME OVER ---')
game.show_board()
print(f'current state score by eval func: {agent.eval_fn(game.state, agent.player)}')
if game.winner:
print(f'{game.winner!s} wins!')
else:
print('tie :(')