2014-08-30 88 views
0

您好我一直在嘗試在Python中構建井字遊戲,因此我正在檢查相鄰符號的列表清單。我知道代碼並不優雅。但我主要關心的是,這個例程給我隨機結果。你們能看到爲什麼嗎?Python。相鄰空格列表檢查列表

def winx(self): 
    if self.current_table [0][0] and self.current_table [0][1] and self.current_table[0][2]== "x": 
     print "Good Boy, you won" 
     self.winner=1 
    elif self.current_table [1][0] and self.current_table [1][1] and self.current_table[1][2]== "x": 
     print "Good Boy, you won" 
     self.winner=1 
    elif self.current_table [2][0] and self.current_table [2][1] and self.current_table[2][2]== "x": 
     print "Good Boy, you won" 
     self.winner=1 
    elif self.current_table [0][0] and self.current_table [1][0] and self.current_table[2][0]== "x": 
     print "Good Boy, you won" 
     self.winner=1 
    elif self.current_table [0][1] and self.current_table [1][1] and self.current_table[2][1]== "x": 
     print "Good Boy, you won" 
     self.winner=1 
    elif self.current_table [0][2] and self.current_table [1][2] and self.current_table[2][2]== "x": 
     print "Good Boy, you won" 
     self.winner=1 
    elif self.current_table [0][0] and self.current_table [1][1] and self.current_table[2][2]== "x": 
     print "Good Boy, you won" 
     self.winner=1 
    elif self.current_table [0][2] and self.current_table [1][1] and self.current_table[2][0]== "x": 
     print "Good Boy, you won" 
     self.winner=1 
    else: 
     self.winner=None " 

回答

2

如果你把

if a and b and c == 'x' 

要檢查,如果是非零和b是非零和c是等於「X」 (其中任何非空字符串算作是非零)

如果你把

if a==b==c=='x' 

應該告訴你,如果這三個變量等於「X」

+0

Maan ....我的大腦融化了..謝謝! – 2014-08-30 20:28:11

1

我不知道這是不是唯一的問題,但你不能像組間比較:

if self.current_table[0][0] \ 
    and self.current_table[0][1] \ 
    and self.current_table[0][2]== "x": 
#        ^^^^^^ 

你必須寫:

if self.current_table[0][0] == "x" \ 
    and self.current_table [0][1] == "x" \ 
    and self.current_table[0][2]== "x": 

或者

if self.current_table[0][0] == \ 
    self.current_table[0][1] == \ 
    self.current_table[0][2] == "x": 

if (self.current_table[0][0],self.current_table [0][1],self.current_table[0][2]) == ("x","x","x"):