Skip to content

Instantly share code, notes, and snippets.

Created March 17, 2017 21:16
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 anonymous/7daf39095a6fbafb0a0c280481d9296c to your computer and use it in GitHub Desktop.
Save anonymous/7daf39095a6fbafb0a0c280481d9296c to your computer and use it in GitHub Desktop.
import random
theBoard = {'top-L': ' ', 'top-M': ' ', 'top-R': ' ',
'mid-L': ' ', 'mid-M': ' ', 'mid-R': ' ',
'low-L': ' ', 'low-M': ' ', 'low-R': ' '}
def printBoard(board):
print(board['top-L'] + '|' + board['top-M'] + '|' + board['top-R'])
print('-+-+-')
print(board['mid-L'] + '|' + board['mid-M'] + '|' + board['mid-R'])
print('-+-+-')
print(board['low-L'] + '|' + board['low-M'] + '|' + board['low-R'])
def winCondition(turn):
if turn in ((theBoard['low-L']) and (theBoard['low-M']) and (theBoard['low-R'])):
congrats(turn)
exit(0)
elif turn in ((theBoard['low-L']) and (theBoard['mid-L']) and (theBoard['top-L'])):
congrats(turn)
exit(0)
elif turn in ((theBoard['mid-L']) and (theBoard['mid-M']) and (theBoard['mid-R'])):
congrats(turn)
exit(0)
elif turn in ((theBoard['top-L']) and (theBoard['top-M']) and (theBoard['top-R'])):
congrats(turn)
exit(0)
elif turn in ((theBoard['low-M']) and (theBoard['mid-M']) and (theBoard['top-M'])):
congrats(turn)
exit(0)
elif turn in ((theBoard['low-R']) and (theBoard['mid-R']) and (theBoard['top-R'])):
congrats(turn)
exit(0)
elif turn in ((theBoard['low-R']) and (theBoard['mid-M']) and (theBoard['top-L'])):
congrats(turn)
exit(0)
elif turn in ((theBoard['low-L']) and (theBoard['mid-M']) and (theBoard['top-R'])):
congrats(turn)
exit(0)
def congrats(turn):
print("Congratulations! the " + turn + " player wins.")
turn = random.choice(['X', 'O'])
for i in range(9):
printBoard(theBoard)
print('it\'s ' + turn + ' turn now, where do you want to place ' + turn + '?')
move = input()
if move not in theBoard.keys():
print("You can only choose top-L/M/R, mid-L/M/R, and low-L/M/R")
i -= 1
continue
theBoard[move] = turn
winCondition(turn)
if turn == 'O':
turn = 'X'
else:
turn = 'O'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment