Last active
June 25, 2020 18:56
-
-
Save jlliu/e71dc893ad5f901fdfa72994a3299ebf 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
validInputs = ['0','1','2']; | |
spaceStates = { | |
"blank": '___', | |
"X": '_X_', | |
"O": '_O_'} | |
#Checks for a win based on what player you are and where you just made a move | |
def checkForWin(board,xPos,yPos,currentPlayer): | |
intendedState = spaceStates[currentPlayer] | |
return ( | |
#Win State 1: Fills a row | |
((board[0][yPos] == intendedState and board[1][yPos] == intendedState and board[2][yPos] == intendedState) | |
#Win State 2: Fills a column | |
or (board[xPos][0] == intendedState and board[xPos][1] == intendedState and board[xPos][2] == intendedState) | |
#Win State 3: Fills a diagonal | |
or (board[0][0] == intendedState and board[1][1] == intendedState and board[2][2] == intendedState) | |
#Win State 4: Fills the other diagonal | |
or (board[2][0] == intendedState and board[1][1] == intendedState and board[0][2] == intendedState)) | |
) | |
class Game: | |
def __init__(self): | |
self.board = [['___']*3, ['___']*3, ['___']*3]; | |
self.gameFinished = False | |
self.winner = "" | |
self.currentPlayer = 'X' | |
self.movesCompleted = 0 | |
#Function to change the player for the turn | |
def togglePlayer(self): | |
if (self.currentPlayer == 'X'): | |
self.currentPlayer = 'O' | |
elif (self.currentPlayer == 'O'): | |
self.currentPlayer = 'X' | |
#Function to respond to a move based on input | |
def respondToMove(self,xPos,yPos): | |
#Case 0: Inputs are out of bounds for the matrix | |
if (xPos not in validInputs or yPos not in validInputs): | |
print("Sorry, choose again") | |
xPos_new = input('Choose an x coord:\n') | |
yPos_new = input('Choose an y coord:\n') | |
self.respondToMove(xPos_new, yPos_new) | |
#Case 1: new position is taken | |
elif (self.board[int(xPos)][int(yPos)] != spaceStates["blank"]): | |
print('Sorry, choose again.') | |
xPos_new = input('Choose an x coord:\n') | |
yPos_new = input('Choose an y coord:\n') | |
self.respondToMove(xPos_new, yPos_new) | |
#Case 2: new position is valid | |
else: | |
xPos = int(xPos) | |
yPos = int(yPos) | |
if (self.currentPlayer == 'X'): | |
self.board[xPos][yPos] = spaceStates["X"] | |
else: | |
self.board[xPos][yPos] = spaceStates["O"] | |
self.movesCompleted += 1 | |
#Case 2a: new position results in a win | |
if (checkForWin(self.board,xPos,yPos,self.currentPlayer)): | |
self.gameFinished = True | |
#Case 2b: new position is valid and doesn't result in a win | |
else: | |
self.togglePlayer() | |
def printState(self): | |
print('\n\n') | |
print(' x ') | |
print('') | |
print(' 0 1 2') | |
print(' ___ ___ ___ ') | |
print(f' 0 |{self.board[0][0]}|{self.board[1][0]}|{self.board[2][0]}|') | |
print(f'y 1 |{self.board[0][1]}|{self.board[1][1]}|{self.board[2][1]}|') | |
print(f' 2 |{self.board[0][2]}|{self.board[1][2]}|{self.board[2][2]}|\n\n') | |
def main(): | |
game = Game() | |
#Keep increasing the turns until there is a winner or the board is full | |
while (game.gameFinished == False and game.movesCompleted < 9): | |
game.printState() | |
print(f'Turn {game.movesCompleted+1}: Player {game.currentPlayer}\'s move!') | |
xPos = input('Choose an x coord:\n') | |
yPos = input('Choose an y coord:\n') | |
game.respondToMove(xPos,yPos) | |
if (game.gameFinished): | |
print(f'\n\n________________\n') | |
print(f'PLAYER {game.currentPlayer} WON!!!') | |
game.printState() | |
else: | |
print(f'\n\n________________\n') | |
print(f'TIE ! NO PLAYERS WON') | |
game.printState() | |
main(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment