Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 巨蟒抽搐_Python_Algorithm_Function_Tic Tac Toe - Fatal编程技术网

Python 巨蟒抽搐

Python 巨蟒抽搐,python,algorithm,function,tic-tac-toe,Python,Algorithm,Function,Tic Tac Toe,python2中的tac-toe游戏,但当程序遇到3-Xs或3-Os时,我无法停止游戏 这是我的代码: 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]

python2中的tac-toe游戏,但当程序遇到3-Xs或3-Os时,我无法停止游戏 这是我的代码:

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()
不幸的是,当它遇到一个TRUE时,它没有退出程序。

win应该返回一个布尔值,指示玩家是否获胜

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")
在游戏中,您调用win,如果返回TRUE,则打印并返回


当然,您也可以对win2执行同样的操作。

您不会从这些函数返回任何内容,因此它始终不会返回任何内容。是的,但我需要退出函数播放,我确实返回了以退出函数…但您不会从win或win2返回任何内容。
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")
if win():
    print "PLAYER1 WON, Congratulations !!!"
    return