-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplayers.py
64 lines (51 loc) · 1.63 KB
/
players.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
59
60
61
62
63
import random
class Player(object):
def __init__(self, symbol, number):
self._symbol = symbol
self._number = number
self._string = 'Player %d (%s)' % (number, symbol)
def __str__(self):
return self._string
@property
def symbol(self):
return self._symbol
@property
def number(self):
return self._number
def chooseMove(self, board):
raise NotImplementedError
class Human(Player):
def chooseMove(self, board):
while True:
move = raw_input('Enter your move: ')
try:
square = board.getByHuman(move)
except ValueError as e:
print e
continue
if square.empty():
break
print 'invalid move: %s is already taken' % move
return square
class Random(Player):
def chooseMove(self, board):
empties = list(board.emptySquares())
square = random.choice(empties)
return square
def distanceFromCentre(square, centre):
return sum(abs(centre - index) for index in square.indexes)
class Centre(Player):
def chooseMove(self, board):
centre = board.size / 2
empties = board.emptySquares()
closest = []
minDistance = None
for square in empties:
distance = distanceFromCentre(square, centre)
if minDistance is None or distance < minDistance:
minDistance = distance
closest = [square]
elif minDistance == distance:
closest.append(square)
square = random.choice(closest)
return square