-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlayers.py
74 lines (58 loc) · 2.02 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
64
65
66
67
68
69
70
71
72
73
74
from Boards import Board
class Player:
def Play(self):
raise NotImplementedError("Please Implement this method")
class MinimaxPlayer(Player):
myID = ""
enemyID = ""
def __init__(self, ID):
self.ID = ID
MinimaxPlayer.myID = ID
MinimaxPlayer.enemyID = "X" if ID == "O" else "O"
def GenerateChild(self, board, symbol):
r = []
plays = board.AvailablePlays()
for p in plays:
b = Board(board)
b.PlayAt(p[0],p[1], symbol)
r.append(b)
return r
def Minimax(self, board, maxPlayer):
value = board.WinCondition()
if value != False:
if value == True:
return 0
else:
return -1 if maxPlayer else 1
if maxPlayer:
_best = -float("inf")
for b in self.GenerateChild(board, self.ID):
v = self.Minimax(b, False)
_best = max([_best,v])
return _best
else:
_best = float("inf")
for b in self.GenerateChild(board, MinimaxPlayer.enemyID):
v = self.Minimax(b, True)
_best = min([_best,v])
return _best
def Play(self, board):
plays = self.GenerateChild(board, self.ID)
weights = {}
for p in plays:
weights[p] = self.Minimax(p, False)
_bestPlay = None
_bestValue = -float("inf")
for p in weights:
if weights[p] > _bestValue or _bestPlay == None:
_bestPlay = p
_bestValue = weights[p]
return board.PlayAt(_bestPlay.lastX, _bestPlay.lastY, self.ID)
class HumanPlayer(Player):
def __init__(self, ID):
self.ID = ID
def Play(self, board):
print "Player " + self.ID + " its your time"
x = int(raw_input("Line to be played"))
y = int(raw_input("Row to be played"))
return board.PlayAt(x,y,self.ID)