Skip to content

Instantly share code, notes, and snippets.

@adamwespiser
Last active October 16, 2023 03:08
Show Gist options
  • Save adamwespiser/2663317 to your computer and use it in GitHub Desktop.
Save adamwespiser/2663317 to your computer and use it in GitHub Desktop.
A basic python script that plays a human player in a game of tic tac toe
#!/usr/bin/python
# Copyright Adam Wespiser, 2012
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
class gameBoard:
board = [0]*9
glyph = '_'
openSpaces = [ x for x in range(9)]
xPositions = []
oPositions = []
winArray =[7, 56, 73, 84, 146, 273, 292, 448]
corners=[0,2,6,8]
xWinStates=[[0,1,2],[3,4,5],[6,7,8],[0,3,6],[1,4,7],[2,5,8],[0,4,8],[2,4,6]]
oWinStates=[[0,1,2],[3,4,5],[6,7,8],[0,3,6],[1,4,7],[2,5,8],[0,4,8],[2,4,6]]
indexToWinState=[[0,3,6],[0,4],[0,5,7],
[1,3],[1,4,6,7],[1,5],
[2,3,7],[2,4],[2,5,6]]
def __init__(self):
self.board = [0] * 9
self.openSpaces = [ x for x in range(9)]
def boardToInt(self):
base = 1
oSum = 0
xSum = 0
for i in range(0,9):
if (self.board[i] == 'O'):
oSum = oSum + base
if (self.board[i] == self.glyph):
xSum = xSum + base
base = base * 2
#print "oInt:",oSum,"xInt",xSum
return oSum,xSum
def checkForWin(self):
oInt, xInt = self.boardToInt()
for winInt in self.winArray:
if (oInt & winInt == winInt):
print "player 1 is the winner!!!!!"
return False
if (xInt & winInt == winInt):
print "computer wins!!!!!"
return False
return True
def setBoard(self,index):
if (len(self.openSpaces) > 0):
#print "trying to set O to", index
self.board[index] = self.glyph
#print "index is ", index
#print "open spaces are", self.openSpaces
self.openSpaces.remove(index)
def findComputerMove(self):
#print "the open spaces are", self.openSpaces
weights = [0]*9
for w in range(0,9):
if ((w in self.corners) and (w in self.openSpaces)):
weights[w] = 3
#print "weights: ",weights
if (4 in self.openSpaces):
self.setBoard(4)
else:
for space in self.openSpaces:
#print "space: ",space
for winStateIndex in self.indexToWinState[space]:
#print "looking at winstate:",winStateIndex
#print "\t",self.xWinStates[winStateIndex]
localWinState = self.xWinStates[winStateIndex]
if (self.twoPosAreSameInWS(localWinState,'X') > -1):
weights[self.twoPosAreSameInWS(localWinState,'X')] += 100
print "winState has two x"
if (self.twoPosAreSameInWS(localWinState,'O') > -1):
weights[self.twoPosAreSameInWS(localWinState,'O')] += 100
#print weights
move = self.getIndexOfMaxWeight(weights)
self.setBoard(move)
def getIndexOfMaxWeight(self, weights):
#print weights
max = -1
index = -1
for i in range(0,len(weights)):
if (weights[i] >= max):
max = weights[i]
index = i
#print "index is",index
return index
def xInWinState(self,winStates):
for space in winStates:
if self.gameBoard[space] == 'X':
return True
return False
def glyphInWinState(self,winStates):
for space in winStates:
if (self.gameBoard[space] == self.glyph):
return True
return False
def twoPosAreSameInWS(self,winState,g):
#print "checking winState", winState
count=0
openIndex = -1
for i in winState:
if (self.board[i] == g):
#print "found a match at", i
count = count + 1
if (self.board[i] == 0):
openIndex = i
if(count > 1):
return openIndex
else:
return -1
def movesLeft(self):
if (len(self.openSpaces) > 0):
return True
else:
return False
def spaceOpen(self,index):
space = self.board[index]
if (space == 0):
return True
else:
return False
def updateBoard(self,index,inputGlyph):
if (index in self.openSpaces):
self.board[index] = inputGlyph
self.openSpaces.remove(index)
return True
else:
return False
def printBoard(self):
print "GameBoard: Index:"
for i in range(3):
j = i*3
print self.board[j],self.board[j+1],self.board[j+2],"\t\t",j,j+1,j+2
intPlayer = 0
x =gameBoard()
x.printBoard()
while(x.movesLeft() and x.checkForWin()):
if (intPlayer == 0):
userInput=input('please enter an index to occupy: ')
if (userInput > -1 and userInput < 9 and userInput in x.openSpaces):
if (x.updateBoard(userInput,'X')):
intPlayer = 1
if (x.movesLeft() == False):
break
else:
print "moves left::",x.movesLeft()
x.findComputerMove()
intPlayer = 0
x.checkForWin()
x.printBoard()
print "end of game"
@Rogue1-TW
Copy link

Hi, i'm still practicing basic Python and i had the same idea of converting the win state to a byte representation. Seems easier to compare bit to bit

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment