-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathultimateplayer.py
99 lines (80 loc) · 3.71 KB
/
ultimateplayer.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
from ultimateboard import UTTTBoardDecision, UTTTBoard
from learning import TableLearning
import random
class UTTTPlayer(object):
def __init__(self):
self.board = None
self.player = None
def setBoard(self, board, player):
self.board = board
self.player = player # X or O
def isBoardActive(self):
return (self.board is not None and self.board.getBoardDecision() == UTTTBoardDecision.ACTIVE)
def makeNextMove(self):
raise NotImplementedError
def learnFromMove(self, prevBoardState):
raise NotImplementedError
def startNewGame(self):
pass
def finishGame(self):
pass
class RandomUTTTPlayer(UTTTPlayer):
def makeNextMove(self):
previousState = self.board.getBoardState()
if self.isBoardActive():
nextBoardLocation = self.board.getNextBoardLocation()
if None in nextBoardLocation:
activeBoardLocations = self.board.getActiveBoardLocations()
nextBoardLocation = random.choice(activeBoardLocations)
emptyPlaces = self.board.getEmptyBoardPlaces(nextBoardLocation)
pickOne = random.choice(emptyPlaces)
self.board.makeMove(self.player, nextBoardLocation, pickOne)
return previousState
def learnFromMove(self, prevBoardState):
pass # Random player does not learn from move
class RLUTTTPlayer(UTTTPlayer):
def __init__(self, learningModel):
self.learningAlgo = learningModel
super(RLUTTTPlayer, self).__init__()
def printValues(self):
self.learningAlgo.printValues()
def testNextMove(self, state, boardLocation, placeOnBoard):
loc = 27*boardLocation[0] + 9*boardLocation[1] + 3*placeOnBoard[0] + placeOnBoard[1]
boardCopy = list(state)
boardCopy[loc] = self.player
return ''.join(boardCopy)
def startNewGame(self):
self.learningAlgo.resetForNewGame()
def finishGame(self):
self.learningAlgo.gameOver()
def makeNextMove(self):
previousState = self.board.getBoardState()
if self.isBoardActive():
nextBoardLocation = self.board.getNextBoardLocation()
activeBoardLocations = [nextBoardLocation]
if None in nextBoardLocation:
activeBoardLocations = self.board.getActiveBoardLocations()
if random.uniform(0, 1) < 0.8: # Make a random move with probability 0.2
moveChoices = {}
for boardLocation in activeBoardLocations:
emptyPlaces = self.board.getEmptyBoardPlaces(boardLocation)
for placeOnBoard in emptyPlaces:
possibleNextState = self.testNextMove(previousState, boardLocation, placeOnBoard)
moveChoices[(tuple(boardLocation), placeOnBoard)] = self.learningAlgo.getBoardStateValue(self.player, self.board, possibleNextState)
(chosenBoard, pickOne) = max(moveChoices, key=moveChoices.get)
else:
chosenBoard = random.choice(activeBoardLocations)
emptyPlaces = self.board.getEmptyBoardPlaces(chosenBoard)
pickOne = random.choice(emptyPlaces)
self.board.makeMove(self.player, chosenBoard, pickOne)
return previousState
def learnFromMove(self, prevBoardState):
self.learningAlgo.learnFromMove(self.player, self.board, prevBoardState)
def saveLearning(self, filename):
self.learningAlgo.saveLearning(filename)
def loadLearning(self, filename):
self.learningAlgo.loadLearning(filename)
if __name__ == '__main__':
board = UTTTBoard()
player1 = RandomUTTTPlayer()
player2 = RLUTTTPlayer(TableLearning(UTTTBoardDecision))