列出项目任何东西

时间:2017-06-26 16:50:41

标签: python python-2.7

所以我正在写一个tic tac toe游戏,我使用它作为列表的格式:

board = [["0", "1", "2"],
         ["3", "4", "5"],
         ["6", "7", "8"]]

因此,当我为tic tac toe编写ai时,我遇到了这些问题,

for i in board:
    if i == ["X","X"," "]:
        return i[2]
    elif i == ["X"," ","X"]:
        return i[1]
    elif i == [" ","X","X"]:
        return i[0]
for i in horzboard:
    if i == ["X","X"," "]:
        return i[2]
    elif i == ["X"," ","X"]:
        return i[1]
    elif i == [" ","X","X"]:
        return i[0]

它无法工作,因为python是特定的项目,所以有一种方法,我可以告诉python这是我想要的(例如* s是什么):

for i in board:
    if i == ["X","X",*]:
        return i[2]
    elif i == ["X",*,"X"]:
        return i[1]
    elif i == [*,"X","X"]:
        return i[0]
for i in horzboard:
    if i == ["X","X",*]:
        return i[2]
    elif i == ["X",*,"X"]:
        return i[1]
    elif i == [*,"X","X"]:
        return i[0]

谢谢!

1 个答案:

答案 0 :(得分:0)

如果您试图在电路板中找到不是' X'的值,这是您的示例代码所暗示的,那么我将使用您可以传递boardhorzboard,根据你的例子。

# This will return a list of all non-X values in the entire board

def return_marks(board):

    mark_list = [(arr,mark) for arr in board for mark in arr if mark != 'X']

        return mark_list

# If you just want the first non-X value to cause the function to stop and return that non-X value, then try this.

def return_marks(board):    
    for row in board:

        for mark in row:

            if mark != 'X':
                return mark