2014-08-30 84 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"):