Here is a game of tic-tac-toe I created to run in python 2.7. I am still learning and feel 400+ lines of code is a bit lengthy for such a small application. However, trying to get some feedback on how I could have put the game together better (and shorter), while still retaining full functionality.
Any and all feedback is welcome. Please note that I have only been working with Python for a few weeks so anything too advanced I may not be able to wrap my head around.
# Game of Tic-Tac-Toe
import os
import sys
def Start():
# Variables
a1 = "1"
a2 = "2"
a3 = "3"
a4 = "4"
a5 = "5"
a6 = "6"
a7 = "7"
a8 = "8"
a9 = "9"
location = "0"
# Splash Screen
os.system('cls' if os.name == 'nt' else 'clear')
print "Welcome to Tic-Tac-Toe!"
print """
| |
---|---|---
| |
---|---|---
| |
You may press CTRL+C at any time to quit this game.
Beware that progress is not saved.
Who shall go first, X or O?
"""
start = raw_input("Please enter \"X\" or \"O\" when ready> ")
if start == "X" or start == "x":
Turn_X(a1, a2, a3, a4, a5, a6, a7, a8, a9, location)
elif start == "O" or start == "o":
Turn_O(a1, a2, a3, a4, a5, a6, a7, a8, a9, location)
else:
Start(a1, a2, a3, a4, a5, a6, a7, a8, a9, location)
def Turn_X(a1, a2, a3, a4, a5, a6, a7, a8, a9, location):
os.system('cls' if os.name == 'nt' else 'clear')
print """
%s | %s | %s
---|---|---
%s | %s | %s
---|---|---
%s | %s | %s
""" % (a1, a2, a3, a4, a5, a6, a7, a8, a9)
# Ask for play
print "It is Player X\'s turn"
location = raw_input("Please enter a location to play (1-9)> ")
Place_X(a1, a2, a3, a4, a5, a6, a7, a8, a9, location)
def Place_X(a1, a2, a3, a4, a5, a6, a7, a8, a9, location):
if location == a1:
a1 = "X"
print """
%s | %s | %s
---|---|---
%s | %s | %s
---|---|---
%s | %s | %s
""" % (a1, a2, a3, a4, a5, a6, a7, a8, a9)
elif location == a2:
a2 = "X"
print """
%s | %s | %s
---|---|---
%s | %s | %s
---|---|---
%s | %s | %s
""" % (a1, a2, a3, a4, a5, a6, a7, a8, a9)
elif location == a3:
a3 = "X"
print """
%s | %s | %s
---|---|---
%s | %s | %s
---|---|---
%s | %s | %s
""" % (a1, a2, a3, a4, a5, a6, a7, a8, a9)
elif location == a4:
a4 = "X"
print """
%s | %s | %s
---|---|---
%s | %s | %s
---|---|---
%s | %s | %s
""" % (a1, a2, a3, a4, a5, a6, a7, a8, a9)
elif location == a5:
a5 = "X"
print """
%s | %s | %s
---|---|---
%s | %s | %s
---|---|---
%s | %s | %s
""" % (a1, a2, a3, a4, a5, a6, a7, a8, a9)
elif location == a6:
a6 = "X"
print """
%s | %s | %s
---|---|---
%s | %s | %s
---|---|---
%s | %s | %s
""" % (a1, a2, a3, a4, a5, a6, a7, a8, a9)
elif location == a7:
a7 = "X"
print """
%s | %s | %s
---|---|---
%s | %s | %s
---|---|---
%s | %s | %s
""" % (a1, a2, a3, a4, a5, a6, a7, a8, a9)
elif location == a8:
a8 = "X"
print """
%s | %s | %s
---|---|---
%s | %s | %s
---|---|---
%s | %s | %s
""" % (a1, a2, a3, a4, a5, a6, a7, a8, a9)
elif location == a9:
a9 = "X"
print """
%s | %s | %s
---|---|---
%s | %s | %s
---|---|---
%s | %s | %s
""" % (a1, a2, a3, a4, a5, a6, a7, a8, a9)
else:
Bad_Entry_X(a1, a2, a3, a4, a5, a6, a7, a8, a9, location)
Win_Check_X(a1, a2, a3, a4, a5, a6, a7, a8, a9, location)
def Bad_Entry_X(a1, a2, a3, a4, a5, a6, a7, a8, a9, location):
os.system('cls' if os.name == 'nt' else 'clear')
print """
%s | %s | %s
---|---|---
%s | %s | %s
---|---|---
%s | %s | %s
""" % (a1, a2, a3, a4, a5, a6, a7, a8, a9)
# Ask for play
print "Player X chose %s." % location
print "This is an invalid location."
location = raw_input("Please enter a location for Player X to play (1- 9)> ")
Place_X(a1, a2, a3, a4, a5, a6, a7, a8, a9, location)
def Win_Check_X(a1, a2, a3, a4, a5, a6, a7, a8, a9, location):
# Win checks
if (a1 == "X" and a2 == "X" and a3 == "X"):
a1 = "-"
a2 = "-"
a3 = "-"
Win_X(a1, a2, a3, a4, a5, a6, a7, a8, a9)
if (a4 == "X" and a5 == "X" and a6 == "X"):
a4 = "-"
a5 = "-"
a6 = "-"
Win_X(a1, a2, a3, a4, a5, a6, a7, a8, a9)
if (a7 == "X" and a8 == "X" and a9 == "X"):
a7 = "-"
a8 = "-"
a9 = "-"
Win_X(a1, a2, a3, a4, a5, a6, a7, a8, a9)
if (a1 == "X" and a4 == "X" and a7 == "X"):
a1 = "|"
a4 = "|"
a7 = "|"
Win_X(a1, a2, a3, a4, a5, a6, a7, a8, a9)
if (a2 == "X" and a5 == "X" and a8 == "X"):
a2 = "|"
a5 = "|"
a8 = "|"
Win_X(a1, a2, a3, a4, a5, a6, a7, a8, a9)
if (a3 == "X" and a6 == "X" and a9 == "X"):
a3 = "|"
a6 = "|"
a9 = "|"
Win_X(a1, a2, a3, a4, a5, a6, a7, a8, a9)
if (a1 == "X" and a5 == "X" and a9 == "X"):
a1 = "\\"
a5 = "\\"
a9 = "\\"
Win_X(a1, a2, a3, a4, a5, a6, a7, a8, a9)
if (a3 == "X" and a5 == "X" and a7 == "X"):
a3 = "/"
a5 = "/"
a7 = "/"
Win_X(a1, a2, a3, a4, a5, a6, a7, a8, a9)
else:
if a1 != "1" and a2 != "2" and a3 != "3" and a4 != "4" and a5 != "5" and a6 != "6" and a7 != "7" and a8 != "8" and a9 != "9":
Win_T(a1, a2, a3, a4, a5, a6, a7, a8, a9)
else:
Turn_O(a1, a2, a3, a4, a5, a6, a7, a8, a9, location)
def Turn_O(a1, a2, a3, a4, a5, a6, a7, a8, a9, location):
os.system('cls' if os.name == 'nt' else 'clear')
print """
%s | %s | %s
---|---|---
%s | %s | %s
---|---|---
%s | %s | %s
""" % (a1, a2, a3, a4, a5, a6, a7, a8, a9)
# Ask for play
print "It is Player O\'s turn"
location = raw_input("Please enter a location to play (1-9)> ")
Place_O(a1, a2, a3, a4, a5, a6, a7, a8, a9, location)
def Place_O(a1, a2, a3, a4, a5, a6, a7, a8, a9, location):
if location == a1:
a1 = "O"
print """
%s | %s | %s
---|---|---
%s | %s | %s
---|---|---
%s | %s | %s
""" % (a1, a2, a3, a4, a5, a6, a7, a8, a9)
elif location == a2:
a2 = "O"
print """
%s | %s | %s
---|---|---
%s | %s | %s
---|---|---
%s | %s | %s
""" % (a1, a2, a3, a4, a5, a6, a7, a8, a9)
elif location == a3:
a3 = "O"
print """
%s | %s | %s
---|---|---
%s | %s | %s
---|---|---
%s | %s | %s
""" % (a1, a2, a3, a4, a5, a6, a7, a8, a9)
elif location == a4:
a4 = "O"
print """
%s | %s | %s
---|---|---
%s | %s | %s
---|---|---
%s | %s | %s
""" % (a1, a2, a3, a4, a5, a6, a7, a8, a9)
elif location == a5:
a5 = "O"
print """
%s | %s | %s
---|---|---
%s | %s | %s
---|---|---
%s | %s | %s
""" % (a1, a2, a3, a4, a5, a6, a7, a8, a9)
elif location == a6:
a6 = "O"
print """
%s | %s | %s
---|---|---
%s | %s | %s
---|---|---
%s | %s | %s
""" % (a1, a2, a3, a4, a5, a6, a7, a8, a9)
elif location == a7:
a7 = "O"
print """
%s | %s | %s
---|---|---
%s | %s | %s
---|---|---
%s | %s | %s
""" % (a1, a2, a3, a4, a5, a6, a7, a8, a9)
elif location == a8:
a8 = "O"
print """
%s | %s | %s
---|---|---
%s | %s | %s
---|---|---
%s | %s | %s
""" % (a1, a2, a3, a4, a5, a6, a7, a8, a9)
elif location == a9:
a9 = "O"
print """
%s | %s | %s
---|---|---
%s | %s | %s
---|---|---
%s | %s | %s
""" % (a1, a2, a3, a4, a5, a6, a7, a8, a9)
else:
Bad_Entry_O(a1, a2, a3, a4, a5, a6, a7, a8, a9, location)
Win_Check_O(a1, a2, a3, a4, a5, a6, a7, a8, a9, location)
def Bad_Entry_O(a1, a2, a3, a4, a5, a6, a7, a8, a9, location):
os.system('cls' if os.name == 'nt' else 'clear')
print """
%s | %s | %s
---|---|---
%s | %s | %s
---|---|---
%s | %s | %s
""" % (a1, a2, a3, a4, a5, a6, a7, a8, a9)
# Ask for play
print "Player O chose %s." % location
print "This is an invalid location."
location = raw_input("Please enter a location for Player O to play (1-9)> ")
Place_O(a1, a2, a3, a4, a5, a6, a7, a8, a9, location)
def Win_Check_O(a1, a2, a3, a4, a5, a6, a7, a8, a9, location):
# Win checks
if (a1 == "O" and a2 == "O" and a3 == "O"):
a1 = "-"
a2 = "-"
a3 = "-"
Win_O(a1, a2, a3, a4, a5, a6, a7, a8, a9)
if (a4 == "O" and a5 == "O" and a6 == "O"):
a4 = "-"
a5 = "-"
a6 = "-"
Win_O(a1, a2, a3, a4, a5, a6, a7, a8, a9)
if (a7 == "O" and a8 == "O" and a9 == "O"):
a7 = "-"
a8 = "-"
a9 = "-"
Win_O(a1, a2, a3, a4, a5, a6, a7, a8, a9)
if (a1 == "O" and a4 == "O" and a7 == "O"):
a1 = "|"
a4 = "|"
a7 = "|"
Win_O(a1, a2, a3, a4, a5, a6, a7, a8, a9)
if (a2 == "O" and a5 == "O" and a8 == "O"):
a2 = "|"
a5 = "|"
a8 = "|"
Win_O(a1, a2, a3, a4, a5, a6, a7, a8, a9)
if (a3 == "O" and a6 == "O" and a9 == "O"):
a3 = "|"
a6 = "|"
a9 = "|"
Win_O(a1, a2, a3, a4, a5, a6, a7, a8, a9)
if (a1 == "O" and a5 == "O" and a9 == "O"):
a1 = "\\"
a5 = "\\"
a9 = "\\"
Win_O(a1, a2, a3, a4, a5, a6, a7, a8, a9)
if (a3 == "O" and a5 == "O" and a7 == "O"):
a3 = "/"
a5 = "/"
a7 = "/"
Win_O(a1, a2, a3, a4, a5, a6, a7, a8, a9)
else:
if a1 != "1" and a2 != "2" and a3 != "3" and a4 != "4" and a5 != "5" and a6 != "6" and a7 != "7" and a8 != "8" and a9 != "9":
Win_T(a1, a2, a3, a4, a5, a6, a7, a8, a9)
else:
Turn_X(a1, a2, a3, a4, a5, a6, a7, a8, a9, location)
def Win_X(a1, a2, a3, a4, a5, a6, a7, a8, a9):
os.system('cls' if os.name == 'nt' else 'clear')
print "Player X Wins"
print """
%s | %s | %s
---|---|---
%s | %s | %s
---|---|---
%s | %s | %s
""" % (a1, a2, a3, a4, a5, a6, a7, a8, a9)
Replay()
def Win_O(a1, a2, a3, a4, a5, a6, a7, a8, a9):
os.system('cls' if os.name == 'nt' else 'clear')
print "Player O Wins"
print """
%s | %s | %s
---|---|---
%s | %s | %s
---|---|---
%s | %s | %s
""" % (a1, a2, a3, a4, a5, a6, a7, a8, a9)
Replay()
def Win_T(a1, a2, a3, a4, a5, a6, a7, a8, a9):
os.system('cls' if os.name == 'nt' else 'clear')
print "The game is a tied game."
print "Congratulations! Noone has lost."
print """
%s | %s | %s
---|---|---
%s | %s | %s
---|---|---
%s | %s | %s
""" % (a1, a2, a3, a4, a5, a6, a7, a8, a9)
Replay()
def Replay():
over = raw_input("Would you like to play again? (Y / N) ")
if over == "Y" or over == "y":
Start()
elif over == "N" or over == "n":
exit(0)
else:
Replay()
Start()
#Winning combos-
# (1 2 3)
# (4 5 6)
# (7 8 9)
# (1 4 7)
# (2 5 8)
# (3 6 9)
# (1 5 9)
# (3 5 7)