Python Code:
#Tic Tac Toe 2 - Python 3 MODIFIED: 7/12/2018
#by Austin Metzner
import sys #for the sys.exit functionality
#These are the rules for the user to read, not contained in main for obvious reasons #I could make it a function but nah
print ("Welcome to Tic Tac Toe!")
print (" ") #Python 2.7 TROLOLOLOL
print ("User 1 goes first")
print (" ")
print ("Here is how you play this version of Tic Tac Toe:")
print (" ")
print ("Imagine a Tic Tac Toe grid. Starting in the top left corner")
print ("the grid is assigned the number 1, and the one directly to the")
print ("right is assigned number 2. So the bottom right of the grid")
print ("would be number 9. To select where you want to go, select a")
print ("number between 1 and 9 accordingly when it is your turn.")
print (" ")
print ("Also, wait a solid 4 seconds for your encouragement to show")
print ("Good luck! May the better man win.")
print (" ")
line0 = (" | | ")
line1 = ("_1_|_2_|_3_")
line2 = ("_4_|_5_|_6_")
line3 = ("_7_|_8_|_9_")
line4 = (" | | ")
def drawBoard(): #Created this for the sole purpose of when you restart the game
print (line0)
print (line1)
print (line2)
print (line3)
print (line4)
#These determine whether a player wins through the didIwin function
userMoves = []
user1ins = []
user2ins = []
def replaceNumber(var,line0, line1, line2, line3,line4, turn): #This is how user 1 makes a move, player 2 has the same function but with O's
global userMoves
if(turn%2 != 0):
global user1ins
var = moveValid(var) #checks if move is a number 1-9
var = isSpotTaken(var) #Makes sure the valid move is not a move that has already been made
if var == "1":
line1 = line1.replace("1","X") #swaps out the number the user chose for an x
elif var == "2":
line1 = line1.replace("2","X")
elif var == "3":
line1 = line1.replace("3","X")
elif var == "4":
line2 = line2.replace("4","X")
elif var == "5":
line2 = line2.replace("5","X")
elif var == "6":
line2 = line2.replace("6","X")
elif var == "7":
line3 = line3.replace("7","X")
elif var == "8":
line3 = line3.replace("8","X")
elif var == "9":
line3 = line3.replace("9","X")
else:
print("Error")
user1ins.append(var) #Updates the list of user inputs for didIwin
userMoves.append(var) #Updates the list for the purpose of isSpotTaken
else:
global user2ins
var = moveValid(var)
var = isSpotTaken(var)
if var == "1":
line1 = line1.replace("1","O")
elif var == "2":
line1 = line1.replace("2","O")
elif var == "3":
line1 = line1.replace("3","O")
elif var == "4":
line2 = line2.replace("4","O")
elif var == "5":
line2 = line2.replace("5","O")
elif var == "6":
line2 = line2.replace("6","O")
elif var == "7":
line3 = line3.replace("7","O")
elif var == "8":
line3 = line3.replace("8","O")
elif var == "9":
line3 = line3.replace("9","O")
else:
print("Error")
user2ins.append(var)
userMoves.append(var)
print (line0)
print (line1)
print (line2)
print (line3)
print (line4)
return(line0, line1, line2, line3,line4) #This makes sure that the playing board is updated
#This is my use of the Twitter API to encourage the players of my game
#Api class is on line 2205 in twitter.py and has all the useful functions, control+F allows you to search this schtuff up
#These next two functions check if either of the users have won
def didIwin(list1, user): #list 1 is gonna be the user's input list, isn't called until after the third user input
userInsSet = set(list1) #sets don't have to be ordered
winCombos = [['1','2','3'],['4','5','6'],['7','8','9'],['1','4','7'],['2','5','8'],['3','6','9'],['1','5','9'],['3','5','7']]
#winCombos is a set of all the possible win combinations
for item in winCombos: #This loop makes it easier to go through and check if there are any win matches
didTheyWin = userInsSet.issuperset(item) #Checks to see if userins is super set to any win combos, returns True or False
if didTheyWin is True:
print ("Player ",user," WINS!")
return True
#playAgain = input("PLAY AGAIN? (enter 'q' to quit): ")
#if playAgain != ("q"):
# main()
#else:
# print (" ")
# sys.exit("Thanks for playing!")
def didIwinFinal(list1): #Did as separate definition for the sole purpose of saying "DRAW"
userInsSet = set(list1)
winCombos = [['1','2','3'],['4','5','6'],['7','8','9'],['1','4','7'],['2','5','8'],['3','6','9'],['1','5','9'],['3','5','7']]
for item in winCombos:
didTheyWin = userInsSet.issuperset(item) #Checks to see if userins is super set to any win combos
if didTheyWin is True:
print ("Player 1 WINS!")
return
#playAgain = input("PLAY AGAIN? (enter 'q' to quit): ")
#if playAgain != "q":
# main()
print ("DRAW!")
def isSpotTaken(var): #checks to see if input hasn't already been used #this works again now
while var in userMoves:
print ("Try another space: ")
var = input() #str(input()) DOES NOT WORK IN 2.7
return var
def moveValid(var): #Checks to see if input is even a number
while var not in "1 2 3 4 5 6 7 8 9".split(): # worked with python 3?
print("Try another space: ")
var = input() #for some reason this needs to be a string
return var
def eraseLists(): #Trying to figure out how to reset lists after calling main() again
global userMoves
userMoves = []
global user1ins
user1ins = []
global user2ins
user2ins = []
#return userMoves, user1ins, user2ins
#Because userMoves and ins are global variables, the lists cannot reset. Maybe try BOOLEAN LOGIC??
def main():
line0 = (" | | ")
line1 = ("_1_|_2_|_3_")
line2 = ("_4_|_5_|_6_")
line3 = ("_7_|_8_|_9_")
line4 = (" | | ")
drawBoard()
global userMoves
global user1ins
global user2ins
#userMoves, user1ins, user2ins =
eraseLists()
turn = 1
while(turn < 10):
if (turn%2 != 0):
user1 = input("User 1 select your move: ")
line0, line1, line2, line3,line4 = replaceNumber(user1, line0, line1, line2, line3,line4, turn)
if(turn<9):
if (didIwin(user1ins,1) == True):
break
else:
didIwinFinal(user1ins)
else:
user2 = input("User 2 select your move: ")
line0, line1, line2, line3,line4 = replaceNumber(user2, line0, line1, line2, line3,line4, turn)
if(didIwin(user2ins,2) == True):
break
turn += 1
#The loop that forces the user to hit q in order for the game to quit
playAgain = input("PLAY AGAIN? (enter 'q' to quit): ")
if playAgain != "q":
main()
else:
sys.exit("Thanks for playing!")
main()