0

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')
2
Contributors
1
Reply
2
Views
4 Years
Discussion Span
Last Post by woooee
0

What does "won't work" mean? Also, use a list that you pass to the functions instead of the global variables, a1, a2, etc.

You are comparing a 3 item sub-list in done to a list that is 4 items which will never be equal.

done = [['a1','a2','a3']]
for testing in [['a1', 'a2'], ['a1', 'a2', 'a3'], ['a1', 'a2', 'a3', 'b1']]:
    print "testing", testing,
    if testing in done:
        print "True"
    else:
        print "False"  

Compare them individually instead, and you'll have to apply this yourself, so you want square numbers 0-8 for example and a list containing who occupies the square, so if list[0], 1, & 2 are all "X" or all "O" you have a winner.

def winner(control_list, player_list):
    for element in control_list:
        if element not in player_list:
            return "False " + element
    return "True"

done = [['a1','a2','a3'], ['b1', 'b2', 'b3']]
for player_list in [['a1', 'a2'], ['a1', 'a2', 'a3'], ['a1', 'a2', 'a3', 'b1']]:
    print "\n---------- player list =", player_list, "-"*20
    for sub_list in done:
        print "testing", sub_list,
        print winner(sub_list, player_list)
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.