Skip to content

Instantly share code, notes, and snippets.

@lesander
Last active June 24, 2020 14:35
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 lesander/39b0b1d6eac4db9b0801d2087e654b67 to your computer and use it in GitHub Desktop.
Save lesander/39b0b1d6eac4db9b0801d2087e654b67 to your computer and use it in GitHub Desktop.
Tic Tac Toe in Python
#!/usr/bin/env/python3
#
# Tic Tac Toe in Python 3
# Licensed under the MIT License
#
# This was created as part
# of a one-hour code challenge.
import subprocess
gameRunning = True
currentPlayer = 1
currentGame = False
def cleanGame():
game = [ ' ', ' ', ' ',
' ', ' ', ' ',
' ', ' ', ' ' ]
return game
def printGame(game):
outputString = ''
for pos, i in enumerate(game):
outputString += '[' + str(i) + '] '
#print('[' + str(game[i]) + ']', end=' ')
#print(pos)
if (pos+1) % 3 == 0:
# newline
outputString += "\n"
print(outputString)
def switchCurrentPlayer(cp):
if (cp == 1):
return 2
else:
return 1
def updateGame(x, y, game, player):
# y
#
# 3 0 0 0
# 2 0 0 0
# 1 0 0 0
# 1 2 3 x
x = int(x)
y = int(y)
# determine row to 'select'
if y == 0:
startPos = 6
elif y == 1:
startPos = 3
else:
startPos = 0
# determine final position by 'selecting' column.
if x == 0:
startPos = startPos
# startPos is already correct
elif x == 1:
startPos += 1
else:
startPos += 2
# is the position not already taken?
if game[startPos] != ' ':
try:
ignore = input('That position is already taken!')
pass
except Exception as e:
pass
global currentPlayer
currentPlayer = switchCurrentPlayer(currentPlayer)
return game
# update game array
if player == 1:
signature = 'X'
else:
signature = 'O'
game[startPos] = signature
return game
def requestInput(prompt):
# probably don't need a function for this.
# derp
line = input(prompt)
# we expect 1 to 3
# make sure the integer is within our bounds.
integer = int(line)
if integer > 3 or integer < 1:
print('Invalid range')
return requestInput(prompt)
return integer-1
def runGameLogic(game, player):
# 0 0 0
# 0 0 0
# 0 0 0
# there are eight possible combinations to win.
# too lazy to implement a smart algorithm tbh
global signatures
signatures = [ 'X', 'O' ]
for s in signatures:
# game = [ '0', '1', '2',
# '3', '4', '5',
# '6', '7', '8' ]
# horizontal wins
if game[0] == s and game[1] == s and game[2] == s:
return s
elif game[3] == s and game[4] == s and game[5] == s:
return s
elif game[6] == s and game[7] == s and game[8] == s:
return s
# vertical wins
elif game[0] == s and game[3] == s and game[6] == s:
return s
elif game[1] == s and game[4] == s and game[7] == s:
return s
elif game[2] == s and game[5] == s and game[8] == s:
return s
# diagonal wins
elif game[0] == s and game[4] == s and game[8] == s:
return s
elif game[6] == s and game[4] == s and game[2] == s:
return s
# no winner yet..
return True
while gameRunning:
# let's get this party started
subprocess.call(["printf", "'\033c"])
# create a new game if none is set.
if currentGame == False:
currentGame = cleanGame()
print('Player one is a cross (X), player two is a zero (O).')
else:
print('')
signatures = [ 'X', 'O' ]
print('Current player is ' + str(currentPlayer) + ' ('+signatures[currentPlayer-1]+')')
# print the current game.
printGame(currentGame)
# request x and y input.
xcord = requestInput('x: ')
ycord = requestInput('y: ')
# update the current game and run our game logic.
currentGame = updateGame(xcord, ycord, currentGame, currentPlayer)
result = runGameLogic(currentGame, currentPlayer)
# determine if the game has a winner.
if result == True:
# True = continue game
if currentPlayer == 1:
currentPlayer = 2
else:
currentPlayer = 1
else:
# we have a winner
gameRunning = False
subprocess.call(["printf", "'\033c"])
print('')
printGame(currentGame)
print('Player ' + str(signatures.index(result)+1) + ' with signature ' + result + ' has won')
break
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment