Last active
February 20, 2017 09:58
-
-
Save kammitama5/3e1bb6fb2c9da9e3b07700c62ea1f728 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
def draw_board(boardValues): | |
# draw board func | |
print('|' + boardValues[0] + '|' + boardValues[1] + '|' + boardValues[2] + '|') | |
print(' ---') + ('-----') | |
print('|' + boardValues[3] + '|' + boardValues[4] + '|' + boardValues[5] + '|') | |
print(' --------') | |
print('|' + boardValues[6] + '|' + boardValues[7] + '|' + boardValues[8] + '|') | |
return | |
# choose Move Player 2 --> always even turn | |
def chooseMove(boardValues, playerSymbol): | |
Move = False | |
while(not Move): | |
print draw_board(boardValues) | |
if playerSymbol == 'X': | |
otherPlayer = 'O' | |
else: | |
otherPlayer = 'X' | |
choice = int(raw_input("Hi Player. Please pick a number from 1 to 9")) -1 | |
if (boardValues[choice] != playerSymbol) and (boardValues[choice] != otherPlayer): | |
boardValues[choice] = playerSymbol | |
Move = True | |
return WinDraw(boardValues) | |
# def chooseMove1(boardValues): | |
# # not a valid move automatically | |
# Move = False | |
# while(not Move): | |
# print draw_board(boardValues) | |
# choice = int(raw_input("Hi Player 1! Please pick a number from 1 to 9")) -1 | |
# # Enters Turn | |
# Error check -> between 1 and 9 -> while between 1 and 9 | |
# if (boardValues[choice] != 'X' and boardValues[choice] != 'O'): | |
# boardValues[choice] = 'X' | |
# Move = True | |
# return WinDraw(boardValues) | |
# def chooseMove2(boardValues): | |
# # not a valid move automatically | |
# Move = False | |
# while(not Move): | |
# print draw_board(boardValues) | |
# choice = int(raw_input("Hi Player 2! Please pick a number from 1 to 9")) -1 | |
# # Enters Turn | |
# # Error check -> between 1 and 9 | |
# if (boardValues[choice] != 'O' and boardValues[choice] != 'X'): | |
# boardValues[choice] = 'O' | |
# Move = True | |
# return WinDraw(boardValues) | |
# Check Horizontal for a win with values either | |
#[1, 2, 3], [4, 5, 6], [ 7, 8, 9] of same type | |
def checkHorizontal(boardValues): | |
Win=False | |
if(boardValues[0] == boardValues[1] == boardValues[2]): | |
Win = True | |
elif (boardValues[3] == boardValues[4] == boardValues[5]): | |
Win = True | |
elif (boardValues[6] == boardValues[7] == boardValues[8]): | |
Win = True | |
return Win | |
#Check Vertical for a win with values either | |
#[0, 3, 6], [1, 4, 7], [2, 5, 8] | |
def checkVertical(boardValues): | |
Win=False | |
if(boardValues[0] == boardValues[3] == boardValues[6]): | |
Win = True | |
elif (boardValues[1] == boardValues[4] == boardValues[7]): | |
Win = True | |
elif (boardValues[2] == boardValues[5] == boardValues[8]): | |
Win = True | |
return Win | |
#Check Diagonal for a win with values either | |
#[0, 4, 8], [2, 4, 6] | |
def checkDiagonal(boardValues): | |
Win=False | |
if(boardValues[0] == boardValues[4] == boardValues[8]): | |
Win = True | |
elif (boardValues[2] == boardValues[4] == boardValues[6]): | |
Win = True | |
return Win | |
# check for win or draw | |
def WinDraw(boardValues): | |
Win = False | |
if (checkHorizontal(boardValues) == True): | |
Win = True | |
elif (checkVertical(boardValues) == True): | |
Win = True | |
elif (checkDiagonal(boardValues) == True): | |
Win = True | |
return Win | |
# draw board | |
boardList = ["1","2","3","4","5","6","7","8","9"] # -> board | |
draw_board(boardList) | |
anyWin = False # -> no winners to start | |
playerTurn = 0 #-> starts at even | |
print "Player 1, please pick a name." | |
player1 = raw_input().capitalize() | |
print "Player 2, please pick a name." | |
player2 = raw_input().capitalize() | |
while (not anyWin and playerTurn < 9): | |
if (playerTurn % 2 == 0): | |
anyWin = chooseMove(boardList, "X") | |
else: | |
anyWin = chooseMove(boardList, "O") | |
playerTurn = playerTurn + 1 | |
draw_board(boardList) | |
playerTurn = playerTurn - 1 | |
if (not anyWin): | |
print "Draw!" | |
elif (playerTurn % 2 == 0): # Even play is player 1 | |
print "Player 1 wins!" | |
else: | |
print "Player 2 wins!" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment