Search code examples
pythonalgorithmfunctiontic-tac-toe

Python tic-tac-toe


tac-toe game in python2, but I can't stop the game when the program meets 3-Xs or 3-Os This is my code here :

a=[]
for i in range(0,10):
    a.append(" ")
def table():
    print "  " + a[1]+ "  |  " +a[2]+ "  |  " +a[3] +"  "
    print "-----------------"
    print "  " + a[4]+ "  |  " +a[5]+ "  |  " +a[6] +"  "
    print "-----------------"
    print "  " + a[7]+ "  |  " +a[8]+ "  |  " +a[9] +"  "



table()
#x = None
def play():
    global x
    for i in range(1,10):
        x= None
        if (i%2==1):
            while ((x>9 or x<0) ):
                x=int(raw_input("Player1, it's your turn : "))
            while ((a[x]=="O" or a[x]=="X")):
                x=int(raw_input("Player1, it's your turn : "))
            a[x]='X'
            table()

        if (i%2==0):
            while ((x>9 or x<0) ):
                x=int(raw_input("Player2, it's your turn : "))
            while ((a[x]=="O" or a[x]=="X")):
                x=int(raw_input("Player2, it's your turn : "))
            a[x]='O'
            table()

            if win() or win2():
                return



def win():
    if (a[1]=="X" and a[2]=="X" and a[3]=="X") or \
     (a[4]=="X" and a[5]=="X" and a[6]=="X") or \
     (a[7]=="X" and a[8]=="X" and a[9]=="X") or \
     (a[1]=="X" and a[4]=="X" and a[7]=="X") or \
     (a[2]=="X" and a[5]=="X" and a[8]=="X") or \
     (a[3]=="X" and a[6]=="X" and a[9]=="X") or \
     (a[1]=="X" and a[5]=="X" and a[9]=="X") or \
     (a[3]=="X" and a[5]=="X" and a[7]=="X") : print "PLAYER1 WON, Congratulations !!!"

#######


def win2():
    if (a[1]=="O" and a[2]=="O" and a[3]=="O") or \
     (a[4]=="O" and a[5]=="O" and a[6]=="O") or \
     (a[7]=="O" and a[8]=="O" and a[9]=="O") or \
     (a[1]=="O" and a[4]=="O" and a[7]=="O") or \
     (a[2]=="O" and a[5]=="O" and a[8]=="O") or \
     (a[3]=="O" and a[6]=="O" and a[9]=="O") or \
     (a[1]=="O" and a[5]=="O" and a[9]=="O") or \
     (a[3]=="O" and a[5]=="O" and a[7]=="O"): print "PLAYER2 WON, Congratulations !!!"



    #else: print "Egalitate !!!"



def egal():
    if (not win() and not win2() ):
        print "Egalitate !" 





play()
#win()
#egal()

As you see I've coded :

if win() or win2():
    return

Unfortunately it doesn't quit the program when it meets either one TRUE.


Solution

  • win should return a Boolean value indicating whether the player wins

    def win():
        return (a[1]=="X" and a[2]=="X" and a[3]=="X") or \
         (a[4]=="X" and a[5]=="X" and a[6]=="X") or \
         (a[7]=="X" and a[8]=="X" and a[9]=="X") or \
         (a[1]=="X" and a[4]=="X" and a[7]=="X") or \
         (a[2]=="X" and a[5]=="X" and a[8]=="X") or \
         (a[3]=="X" and a[6]=="X" and a[9]=="X") or \
         (a[1]=="X" and a[5]=="X" and a[9]=="X") or \
         (a[3]=="X" and a[5]=="X" and a[7]=="X")
    

    In play you call win, and if it returns TRUE, then print and return.

    if win():
        print "PLAYER1 WON, Congratulations !!!"
        return
    

    You can of course do the same for win2.