Created
March 17, 2017 21:16
-
-
Save anonymous/7daf39095a6fbafb0a0c280481d9296c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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