Skip to content

Commit

Permalink
A way to play a sequence of ultimate TTT games and learn
Browse files Browse the repository at this point in the history
  • Loading branch information
shayakbanerjee committed Dec 15, 2017
1 parent dccb590 commit 18dcc4d
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 14 deletions.
8 changes: 4 additions & 4 deletions board.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,17 +50,17 @@ def getWinState(listOfThree):
else:
self.decision = TTTBoardDecision.DRAW

def makeMove(self, who, i, j): # who is PLAYER_X or PLAYER_O
def makeMove(self, who, i, j, verbose=True): # who is PLAYER_X or PLAYER_O
if self.board[i][j] != GridStates.EMPTY:
print 'That location is not empty'
return
print '%s moves'%(who)
#print '%s moves'%(who)
self.board[i][j] = who
#self.printBoard()
self.determineBoardState()
if self.decision == TTTBoardDecision.DRAW:
if self.decision == TTTBoardDecision.DRAW and verbose is True:
print 'This TTT game was drawn!'
elif self.decision != TTTBoardDecision.ACTIVE:
elif self.decision != TTTBoardDecision.ACTIVE and verbose is True:
print 'This TTT game was won by %s'%(GridStates.PLAYER_X if self.decision == TTTBoardDecision.WON_X else GridStates.PLAYER_O)

def printBoard(self):
Expand Down
38 changes: 30 additions & 8 deletions game.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
from board import TTTBoardDecision, GridStates, TTTBoard
from ultimateboard import UTTTBoard, UTTTBoardDecision
from player import RandomTTTPlayer, RLTTTPlayer
from ultimateplayer import RandomUTTTPlayer, RLUTTTPlayer
from plotting import drawXYPlotByFactor
import os

class GameSequence(object):
def __init__(self, numberOfGames, player1, player2):
def __init__(self, numberOfGames, player1, player2, BoardClass=TTTBoard, BoardDecisionClass=TTTBoardDecision):
self.player1 = player1
self.player2 = player2
self.numberOfGames = numberOfGames
self.BoardClass = BoardClass
self.BoardDecisionClass = BoardDecisionClass

def playAGame(self, board):
while board.getBoardDecision() == TTTBoardDecision.ACTIVE:
while board.getBoardDecision() == self.BoardDecisionClass.ACTIVE:
self.player1.setBoard(board, GridStates.PLAYER_X)
self.player2.setBoard(board, GridStates.PLAYER_O)
pState1 = self.player1.makeNextMove()
Expand All @@ -23,22 +28,39 @@ def playAGame(self, board):
def playGamesAndGetWinPercent(self):
results = []
for i in range(self.numberOfGames):
board = TTTBoard()
board = self.BoardClass()
results.append(self.playAGame(board))
xpct, opct, drawpct = float(results.count(TTTBoardDecision.WON_X))/float(self.numberOfGames), \
float(results.count(TTTBoardDecision.WON_O))/float(self.numberOfGames), \
float(results.count(TTTBoardDecision.DRAW))/float(self.numberOfGames)
xpct, opct, drawpct = float(results.count(self.BoardDecisionClass.WON_X))/float(self.numberOfGames), \
float(results.count(self.BoardDecisionClass.WON_O))/float(self.numberOfGames), \
float(results.count(self.BoardDecisionClass.DRAW))/float(self.numberOfGames)
return (xpct, opct, drawpct)

if __name__ == '__main__':
def playTTTAndPlotResults():
learningPlayer = RLTTTPlayer()
randomPlayer = RandomTTTPlayer()
results = []
numberOfSetsOfGames = 40
for i in range(numberOfSetsOfGames):
games = GameSequence(100, learningPlayer, randomPlayer)
games = GameSequence(100, randomPlayer, learningPlayer)
results.append(games.playGamesAndGetWinPercent())
plotValues = {'X Win Fraction': zip(range(numberOfSetsOfGames), map(lambda x: x[0], results)),
'O Win Fraction': zip(range(numberOfSetsOfGames), map(lambda x: x[1], results)),
'Draw Fraction': zip(range(numberOfSetsOfGames), map(lambda x: x[2], results))}
drawXYPlotByFactor(plotValues, 'Set Number', 'Fraction')

def playUltimateAndPlotResults():
learningPlayer = RLUTTTPlayer()
randomPlayer = RandomUTTTPlayer()
results = []
numberOfSetsOfGames = 1
for i in range(numberOfSetsOfGames):
games = GameSequence(1000, learningPlayer, randomPlayer, BoardClass=UTTTBoard, BoardDecisionClass=UTTTBoardDecision)
results.append(games.playGamesAndGetWinPercent())
plotValues = {'X Win Fraction': zip(range(numberOfSetsOfGames), map(lambda x: x[0], results)),
'O Win Fraction': zip(range(numberOfSetsOfGames), map(lambda x: x[1], results)),
'Draw Fraction': zip(range(numberOfSetsOfGames), map(lambda x: x[2], results))}
drawXYPlotByFactor(plotValues, 'Set Number', 'Fraction')

if __name__ == '__main__':
#playTTTAndPlotResults()
playUltimateAndPlotResults()
4 changes: 2 additions & 2 deletions ultimateboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ def makeMove(self, who, whichBoard, whichLocation):
if tttboard.getGrid(i, j) != GridStates.EMPTY:
print 'That location is not empty'
return
tttboard.makeMove(who, i, j)
self.printBoard()
tttboard.makeMove(who, i, j, verbose=False)
#self.printBoard()
self.determineBoardState()
if self.decision == UTTTBoardDecision.DRAW:
print 'This Ultimate-TTT game was drawn!'
Expand Down

0 comments on commit 18dcc4d

Please sign in to comment.