My problem with my TIC TAC TOE game is that it does not save end the game when someone has won if they have more than four moves.
THe method of my code to check if someone has won is that it goes over a list of list. The list inside the list has three values.
the problem is that if the player has made more than three moves it wont work.
any advice?
import random
def savegame():
fout = open('tictactoe.txt','w')
fout.write(row1)
fout.write(row2)
fout.write(row3)
fout.close()
return none
a1 = ' '
a2 = ' '
a3 = ' '
b1 = ' '
b2 = ' '
b3 = ' '
c1 = ' '
c2 = ' '
c3 = ' '
#boardspace = ('a1','a2','a3','b1','b2','b3','c1','c2','c3')
#this is the board
def board():
global boardspace
row1 = (' ' + a1 + ' | ' + a2 + ' | ' + a3)
print(row1)
print('-'*20)
row2 = (' ' + b1 + ' | ' + b2 + ' | ' + b3)
print(row2)
print('-'*20)
row3 = (' ' + c1 + ' | ' + c2 + ' | ' + c3)
print(row3)
print()
done = [['a1','a2','a3'],['b1','b2','b3'],['c1','c2','c3'],['a1','b1','c1'],['a2','b2','c2'],['a3','b3','c3'],['a1','b2','c3'],['a3','b2','c1']]
x = 'a'
possiblemoves = ['a1','a2','a3','b1','b2','b3','c1','c2','c3']
computermoves = []
playermoves = []
#predict computerchoice
def compgive():
global a1
global a2
global a3
global b1
global b2
global b3
global c1
global c2
global c3
global x
b = len(possiblemoves)
if b == 0:
x = 'endgame'
print("draw")
return x
computerpredictor = random.randint(0,b-1)
computerchoice = possiblemoves[computerpredictor]
if computerchoice == "a1":
a1 = ' X '
computermoves.append(computerchoice)
computermoves.sort ()
possiblemoves.remove(computerchoice)
elif computerchoice == 'a2':
a2 = ' X '
computermoves.append(computerchoice)
computermoves.sort ()
possiblemoves.remove(computerchoice)
elif computerchoice == 'a3':
a3 = ' X '
computermoves.append(computerchoice)
computermoves.sort ()
possiblemoves.remove(computerchoice)
elif computerchoice == 'b1':
b1 = ' X '
computermoves.append(computerchoice)
computermoves.sort ()
possiblemoves.remove(computerchoice)
elif computerchoice == 'b2':
b2 = ' X '
computermoves.append(computerchoice)
computermoves.sort ()
possiblemoves.remove(computerchoice)
elif computerchoice == 'b3':
b3 = ' X '
computermoves.append(computerchoice)
computermoves.sort ()
possiblemoves.remove(computerchoice)
elif computerchoice == 'c1':
c1 = ' X '
computermoves.append(computerchoice)
computermoves.sort ()
possiblemoves.remove(computerchoice)
elif computerchoice == 'c2':
c2 = ' X '
computermoves.append(computerchoice)
computermoves.sort ()
possiblemoves.remove(computerchoice)
elif computerchoice == 'c3':
c3 = ' X '
computermoves.append(computerchoice)
computermoves.sort ()
possiblemoves.remove(computerchoice)
#trial begins
#start here
while x != 'endgame':
print (possiblemoves)
if computermoves in done:
x = 'endgame'
print("comp")
elif playermoves in done:
x = 'endgame'
print("player")
#start of the game
else:
board()
playerchoice = input("Enter your move: ")
playerchoice = playerchoice.lower()
if playerchoice in possiblemoves:
#add X or O to boardspace for player/computer choice and sorts choices
if playerchoice == "a1":
a1 = ' O '
playermoves.append(playerchoice)
playermoves.sort ()
possiblemoves.remove(playerchoice)
compgive()
board()
elif playerchoice == 'a2':
a2 = ' O '
playermoves.append(playerchoice)
playermoves.sort ()
possiblemoves.remove(playerchoice)
compgive()
board()
elif playerchoice == 'a3':
a3 = ' O '
playermoves.append(playerchoice)
playermoves.sort ()
possiblemoves.remove(playerchoice)
compgive()
board()
elif playerchoice == 'b1':
b1 = ' O '
playermoves.append(playerchoice)
playermoves.sort ()
possiblemoves.remove(playerchoice)
compgive()
board()
elif playerchoice == 'b2':
b2 = ' O '
playermoves.append(playerchoice)
playermoves.sort ()
possiblemoves.remove(playerchoice)
compgive()
board()
elif playerchoice == 'b3':
b3 = ' O '
playermoves.append(playerchoice)
playermoves.sort ()
possiblemoves.remove(playerchoice)
compgive()
board()
elif playerchoice == 'c1':
c1 = ' O '
playermoves.append(playerchoice)
playermoves.sort ()
possiblemoves.remove(playerchoice)
compgive()
board()
elif playerchoice == 'c2':
c2 = ' O '
playermoves.append(playerchoice)
playermoves.sort ()
possiblemoves.remove(playerchoice)
compgive()
board()
elif playerchoice == 'c3':
c3 = ' O '
playermoves.append(playerchoice)
playermoves.sort ()
possiblemoves.remove(playerchoice)
compgive()
board()
elif computermoves in done:
x = 'endgame'
print("comp")
elif playermoves in done:
x = 'endgame'
print("player")
elif len(possiblemoves) == 0:
x = 'endgame'
elif playerchoice in playermoves or computermoves:
print("already taken")
else:
print('sorry try again')