简体   繁体   English

蟒蛇。 相邻空格的清单检查清单

[英]Python. List of list check for adjacent spaces

Hi I have been trying to build a Tic Tac Toe game in Python, and therefore I am checking a list of lists for adjacent symbols. 嗨,我一直在尝试用Python构建井字游戏,因此我正在检查相邻符号的列表清单。 I know the code is not elegant. 我知道代码不是很优雅。 But my main concern is that this routine is giving me random results. 但我主要担心的是,该例程给我带来了随机结果。 Can you guys see why? 你们知道为什么吗?

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 "

If you put 如果你把

 if a and b and c == 'x'

you are checking if a is nonzero and b is nonzero and c is equal to 'x' (where any nonempty string counts as nonzero) 您正在检查a是否为非零,b是否为非零,c是否等于'x'(其中任何非空字符串都算作非零)

If you put 如果你把

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

that should tell you if all three variables are equal to 'x' 它应该告诉您三个变量是否都等于“ x”

I don't know if this is the only issue, but you can't group comparison like that: 我不知道这是否是唯一的问题,但是您不能像这样进行分组比较:

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

You have to write: 你必须写:

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

Or 要么

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

Or 要么

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM