Skip to content

Instantly share code, notes, and snippets.

@Spockuto
Last active January 31, 2018 09:40
Show Gist options
  • Save Spockuto/519eb46862a96d66fd0e2f07c945608c to your computer and use it in GitHub Desktop.
Save Spockuto/519eb46862a96d66fd0e2f07c945608c to your computer and use it in GitHub Desktop.
"""tictactoe game for 2 players"""
from __future__ import print_function
#Step 1 Deciding Parameters
choices = []
choice = 0
playerOneTurn = True
winner = False
#Step 2 : Designing the Game Board
for x in range (0, 9) :
choices.append(str(x + 1))
def printBoard() :
global choices
print( '\n -----')
print( '|' + choices[0] + '|' + choices[1] + '|' + choices[2] + '|')
print( ' -----')
print( '|' + choices[3] + '|' + choices[4] + '|' + choices[5] + '|')
print( ' -----')
print( '|' + choices[6] + '|' + choices[7] + '|' + choices[8] + '|')
print( ' -----\n')
def getAndValidateInput():
global playerOneTurn
global choices
global choice
if playerOneTurn :
print( "Player 1:")
else :
print( "Player 2:")
#Validating moves
try:
choice = int(input(">> "))
except:
print("please enter a valid field")
getAndValidateInput()
if choice < 0 or choice > 9 :
print("please enter a valid field")
getAndValidateInput()
if choices[choice - 1] == 'X' or choices [choice-1] == 'O':
print("illegal move, plase try again")
getAndValidateInput()
def decidingTurn():
global playerOneTurn
global choices
if playerOneTurn :
choices[choice - 1] = 'X'
else :
choices[choice - 1] = 'O'
playerOneTurn = not playerOneTurn
def checkWinner():
global choices
global winner
for x in range (0, 3) :
y = x * 3
if (choices[y] == choices[(y + 1)] and choices[y] == choices[(y + 2)]) :
winner = True
printBoard()
if (choices[x] == choices[(x + 3)] and choices[x] == choices[(x + 6)]) :
winner = True
printBoard()
if((choices[0] == choices[4] and choices[0] == choices[8]) or
(choices[2] == choices[4] and choices[4] == choices[6])) :
winner = True
printBoard()
while not winner :
printBoard()
getAndValidateInput()
decidingTurn()
checkWinner()
print ("Player " + str(int(playerOneTurn + 1)) + " wins!\n")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment