/
TicTacToePlayers.py
47 lines (37 loc) · 1.17 KB
/
TicTacToePlayers.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
import numpy as np
"""
Random and Human-ineracting players for the game of TicTacToe.
Author: Evgeny Tyurin, github.com/evg-tyurin
Date: Jan 5, 2018.
Based on the OthelloPlayers by Surag Nair.
"""
class RandomPlayer():
def __init__(self, game):
self.game = game
def play(self, board):
a = np.random.randint(self.game.getActionSize())
valids = self.game.getValidMoves(board, 1)
while valids[a]!=1:
a = np.random.randint(self.game.getActionSize())
return a
class HumanTicTacToePlayer():
def __init__(self, game):
self.game = game
def play(self, board):
# display(board)
valid = self.game.getValidMoves(board, 1)
for i in range(len(valid)):
if valid[i]:
print(int(i/self.game.n), int(i%self.game.n))
while True:
# Python 3.x
a = input()
# Python 2.x
# a = raw_input()
x,y = [int(x) for x in a.split(' ')]
a = self.game.n * x + y if x!= -1 else self.game.n ** 2
if valid[a]:
break
else:
print('Invalid')
return a