Skip to content

Instantly share code, notes, and snippets.

@horstjens
Created December 15, 2015 06:39
Show Gist options
  • Save horstjens/4adc56d3de6c2392f440 to your computer and use it in GitHub Desktop.
Save horstjens/4adc56d3de6c2392f440 to your computer and use it in GitHub Desktop.
tictactoe in python for 2 players
"""tictactoe game for 2 players
from blogpost: http://thebillington.co.uk/blog/posts/writing-a-tic-tac-toe-game-in-python by BILLY REBECCHI,
slightly improved by Horst JENS"""
from __future__ import print_function
choices = []
for x in range (0, 9) :
choices.append(str(x + 1))
playerOneTurn = True
winner = False
def printBoard() :
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')
while not winner :
printBoard()
if playerOneTurn :
print( "Player 1:")
else :
print( "Player 2:")
try:
choice = int(input(">> "))
except:
print("please enter a valid field")
continue
if choices[choice - 1] == 'X' or choices [choice-1] == 'O':
print("illegal move, plase try again")
continue
if playerOneTurn :
choices[choice - 1] = 'X'
else :
choices[choice - 1] = 'O'
playerOneTurn = not playerOneTurn
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()
print ("Player " + str(int(playerOneTurn + 1)) + " wins!\n")
@thebillington
Copy link

Hey Horst, thanks so much for doing a write up of the code!

@sayo96
Copy link

sayo96 commented Aug 8, 2017

Thanks a lot for writing this code mate.It would be nice if you had an explanation for this.

@speedowagon
Copy link

yeah its awesome...i want to know if anyone can help me
i want to make it so that people can play it on a network or via hotspot

@juvers
Copy link

juvers commented Jan 23, 2018

You might want to define the numbers that draw the game. Also might be nice to ask if players want to play again

@sspathare97
Copy link

For the following cases, printBoard is called twice -

X X X
X O O
X O O

X O O
X X O
X O X

The conditions on lines 52 and 56 should include (not winner) and

@innat
Copy link

innat commented Apr 29, 2018

A very efficient way to create Tic Tac Toe game in python.
tic_tic_toe.py

@amolngt
Copy link

amolngt commented May 30, 2018

same code with "Lost" condition

from future import print_function

choices = []
checkturns = []
for x in range(0, 9):
choices.append(str(x + 1))

playerOneTurn = True
winner = False

def printBoard():
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')

while not winner:
printBoard()

if (len(checkturns) == 9 and winner == False):
    print("Both lost")
    break

if playerOneTurn:
    print("Player 1:")
else:
    print("Player 2:")

try:
    choice = int(input(">> "))
    checkturns.append(choice)
except:
    print("please enter a valid field")
    continue
if choices[choice - 1] == 'X' or choices[choice-1] == 'O':
    print("illegal move, plase try again")
    continue

if playerOneTurn:
    choices[choice - 1] = 'X'
else:
    choices[choice - 1] = 'O'

playerOneTurn = not playerOneTurn

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()

if winner == True:
print("Player " + str(int(playerOneTurn + 1)) + " wins!\n")

@vikeshambade
Copy link

tnx............. bro

@vivek1339
Copy link

Amazing code but it doesn't have a way to show the game is draw

@vivek1339
Copy link

Copy link

ghost commented Apr 19, 2019

Doesn't stop when game draws...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment