-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmnk.py
executable file
·55 lines (46 loc) · 1.8 KB
/
mnk.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
import time
from typing import Optional, Any
from agents import Agent
from board import Board, DRAW
from persistence import store_game
def printv(_str: Any, verbose: bool):
if verbose:
print(_str)
def play_mnk(board: Board, player0: Agent, player1: Agent, verbose: bool = True) -> Optional[str]:
while True:
for player in (player0, player1):
printv(f"{player.name} Making Move", verbose)
t1 = time.perf_counter()
move = player.move()
t2 = time.perf_counter()
printv(f'Time to make move: {t2 - t1}', verbose)
board.make_move(move)
outcome = board.get_outcome()
if outcome in board.tokens:
player0.end_game()
player1.end_game()
printv(f"{player.name} with token {player.token} wins!", verbose)
printv(board, verbose)
return player.name
elif outcome == DRAW:
player0.end_game()
player1.end_game()
printv(f"Draw!", verbose)
printv(board, verbose)
return DRAW
def main():
from agents import Minimax, TabularQLearning, Human, InOrder, DeepQLearning, Random
board = Board(num_rows=3, num_cols=3, num_to_win=3)
#player_one = FullGameTree(board.tokens, player_idx=0)
player_one = TabularQLearning(board, player_idx=0, use_double_q_table=True)
player_two = Random(board, player_idx=1)
#player_two = Minimax(board, player_idx=1)
#player_two = Human(board.tokens, player_idx=1)
#print(f"Playing the game")
t1 = time.perf_counter()
winner = play_mnk(board, player_one, player_two)
store_game(board, player_one, player_two, winner)
t2 = time.perf_counter()
print(t2-t1)
if __name__ == '__main__':
main()