Skip to content

Instantly share code, notes, and snippets.

@markostam
Last active June 3, 2016 05:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save markostam/9a578841b561e60d147e69bfea614f36 to your computer and use it in GitHub Desktop.
Save markostam/9a578841b561e60d147e69bfea614f36 to your computer and use it in GitHub Desktop.
python tic tac toe game. marko s for the recurse center coding challenge.
'''
tic tac toe game in python
marko stamenovic
june 1 2016
recurse center coding challenge
'''
#initialize board
xsos = [' ',' ',' ',' ',' ',' ',' ',' ',' ']
#list of possible moves
possMoves = ['a1','b1','c1','a2','b2','c2','a3','b3','c3']
#past moves
pastmoves=[]
print 'welcome to tic tac toe!!!\n'
def main():
'''
main function loop
'''
p1,p2 = choosePiece()
win = False
count=1
#play game
while win == False:
#check for a tie
if len(pastmoves) == 9:
win = True
#alternate turns between players
elif count%2 == 0: #player 2 turn
drawBoard(xsos)
move,player = inputMove(possMoves,p2)
makeMove(move,p2)
win = checkWin(xsos)
else: #player 1 turn
drawBoard(xsos)
move,player = inputMove(possMoves,p1)
makeMove(move,p1)
win = checkWin(xsos)
count+=1
#check if it's a tie or someone won
if len(pastmoves) == 9:
catsGame()
else:
congrats(player)
playAgain()
def drawBoard(xsos):
'''
function to draw board
'''
print ' ___ ___ ___ '
print ' | %s | %s | %s |' %(xsos[6],xsos[7],xsos[8])
print ' 3 | ___ | ___ | ___ |'
print ' | %s | %s | %s |' %(xsos[3],xsos[4],xsos[5])
print ' 2 | ___ | ___ | ___ |'
print ' | %s | %s | %s |' %(xsos[0],xsos[1],xsos[2])
print ' 1 | ___ | ___ | ___ |'
print ' a b c\n\n'
def choosePiece():
'''
function to choose which player is x's or o's
'''
possiblePieces = ['x\'s','x','xs','o\'s','o','os']
p1 = raw_input('player 1, would you like to be x\'s or o\'s? ')
p1 = p1.lower()
while p1 not in possiblePieces:
p1 = raw_input('that\'s not a piece! enter either x\'s or o\'s: ')
p1 = p1.lower()
if p1 in possiblePieces[0:3]:
p1 = ['x',1]
p2 = ['o',2]
else:
p1 = ['o',1]
p2 = ['x',2]
return p1,p2
def inputMove(poss,player):
'''
input move to board position code
and check for input errors
'''
global pastmoves
move = raw_input('player %i, enter move: ' %player[1])
move = move.lower()
while move not in poss:
move = raw_input('that\'s not even a move!\n\nplayer %s enter a valid move (e.g. \'a3\'): ' %player[1])
move = move.lower()
while move in pastmoves:
move = raw_input('that space is already taken\n\nplayer %s enter a valid move (e.g. \'a3\'): ' %player[1])
move = move.lower()
pastmoves.append(move)
return move,player
def makeMove(move,player):
'''
translate board position code into
master board list and
edit global board list accordingly
'''
for i in xrange(len(xsos)):
if move == possMoves[i]:
xsos[i] = player[0]
def checkWin(xsos):
'''
check for a winning combination with a list of booleans
'''
#list of winning combinations
winCond = [xsos[0] == xsos[1] == xsos[2] in ['x','o'],
xsos[0] == xsos[3] == xsos[6] in ['x','o'],
xsos[0] == xsos[4] == xsos[8] in ['x','o'],
xsos[3] == xsos[4] == xsos[5] in ['x','o'],
xsos[6] == xsos[4] == xsos[2] in ['x','o'],
xsos[7] == xsos[4] == xsos[1] in ['x','o'],
xsos[6] == xsos[7] == xsos[8] in ['x','o'],
xsos[8] == xsos[5] == xsos[2] in ['x','o']]
if True in winCond:
return True
else:
return False
def congrats(player):
'''
win eye candy
'''
drawBoard(xsos)
print '\nplayer %i wins! woo-hoo!' %player[1]
print " .-=========-."
print " \\\'-=======-'/"
print " _| .=. |_"
print ' ((| {{*}} |))'
print ' \| /|\ |/'
print ' | player%i |' %player[1]
print ' \_ _/'
print ' _`) (`_'
print ' _/_______\_'
print ' /___________\\'
def catsGame():
'''
tie eye candy
'''
drawBoard(xsos)
print 'cat\'s game (meow)'
print ' _'
print ' | \\'
print ' | |'
print ' | |'
print ' |\ | |'
print ' /, ~\ / /'
print ' X `-.....-------./ /'
print ' ~-. ~ ~ |'
print ' \ / |'
print ' \ /_ ___\ /'
print ' | /\ ~~~~~ \ |'
print ' | | \ || |'
print ' | |\ \ || )'
print ' (_/ (_/ ((_/'
def playAgain():
'''
check if user wants to play again or quit
'''
global xsos, pastmoves
poss = ['y','n']
yn = raw_input('\nplay again (y/n)? ')
yn = yn.lower()
while yn not in poss:
yn = raw_input('invalid entry. play again (y/n)? ')
yn = yn.lower()
if yn == 'y':
#reinitialize
xsos = [' ',' ',' ',' ',' ',' ',' ',' ',' ']
pastmoves = []
main()
else:
print '\nsee ya next time'
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment