Created
February 4, 2018 20:51
-
-
Save xlucasdemelo/052e0a33c41c0da9ac2cab18ecfc5582 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
playerOne = "" | |
playerTwo = "" | |
def choosePlayer( player ): | |
var = raw_input("Player "+ player +": choose X or O: ").upper() | |
while (var != 'X' and var != 'O'): | |
var = raw_input("Player "+ player +": choose X or O: ").upper() | |
print "you entered", var | |
return var | |
def drawGame(gameMatrix): | |
def printIndex(line, column): | |
try: | |
value = gameMatrix[line][column] | |
return str(value) | |
except IndexError: | |
return "" | |
print( printIndex(0,0) + " | " + printIndex(0,1) + " | " + printIndex(0,2) ) | |
print( printIndex(1,0) + " | " + printIndex(1,1) + " | " + printIndex(1,2) ) | |
print( printIndex(2,0) + " | " + printIndex(2,1) + " | " + printIndex(2,2) ) | |
def validateGameEnd(gameMatrix): | |
def isLineEqual( gameMatrix ): | |
for i in range(0, len(gameMatrix) ): | |
if ( ( gameMatrix[i][0] != "" and gameMatrix[i][1] != "" and gameMatrix[i][2] != "" ) | |
and gameMatrix[i][0] == gameMatrix[i][1] == gameMatrix[i][2] ): | |
return True | |
return False | |
def isColumnEqual( gameMatrix ): | |
for i in range(0, len(gameMatrix) ): | |
if ( ( gameMatrix[0][i] != "" and gameMatrix[1][i] != "" and gameMatrix[2][i] != "" ) | |
and gameMatrix[0][i] == gameMatrix[1][i] == gameMatrix[2][i] ): | |
return True | |
return False | |
def isDiagonalEqual( gameMatrix ): | |
for i in range(0, len(gameMatrix) ): | |
if ( ( gameMatrix[0][0] != "" and gameMatrix[1][1] != "" and gameMatrix[2][2] != "" ) | |
and gameMatrix[0][0] == gameMatrix[1][1] == gameMatrix[2][2] ): | |
return True | |
if ( ( gameMatrix[0][2] != "" and gameMatrix[1][1] != "" and gameMatrix[2][1] != "" ) | |
and gameMatrix[0][2] == gameMatrix[1][1] == gameMatrix[2][1] ): | |
return True | |
return False | |
return True if isLineEqual( gameMatrix ) is True else True if isColumnEqual( gameMatrix ) else True if isDiagonalEqual( gameMatrix ) else False | |
def play(playerOne, playerTwo): | |
playerOneTurn = True | |
gameEnded = False | |
gameMatrix = [["","",""],["","",""],["","",""]] | |
#Function to read a player's move | |
def readPlayerMove( indexType ): | |
def validateMove(move): | |
try: | |
move = int(move) | |
except ValueError: | |
print("enter a number") | |
return -1 | |
if (move < 0 or move > 2): | |
print("Invalid index") | |
return -1 | |
return move | |
move = validateMove( raw_input("Select "+ indexType +": ") ) | |
return move | |
#Game runs here | |
while( not gameEnded ): | |
drawGame(gameMatrix) | |
validMove = -1 | |
while( validMove == -1 ): | |
print( "\n Player {} Turn \n".format( "Player One" if playerOneTurn is True else "Player Two" ) ) | |
row = readPlayerMove("row") | |
column = readPlayerMove("column") | |
validMove = True if ( row != -1 ) and column != -1 else -1 | |
if (gameMatrix[row][column] == ""): | |
validMove = True | |
else: | |
validMove = -1 | |
gameMatrix[row][column] = playerOne if playerOneTurn is True else playerTwo | |
gameEnded = validateGameEnd(gameMatrix) | |
playerOneTurn = not playerOneTurn | |
drawGame(gameMatrix) | |
print( "End of the game ! {} WIN !!!".format( "Player Two" if playerOneTurn is True else "Player One" ) ) | |
def main(): | |
playerOne = choosePlayer("One") | |
playerTwo = 'X' if playerOne == 'O' else 'O' | |
play(playerOne, playerTwo) | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment