用于2D板的Python检查列表

时间:2012-12-06 19:05:01

标签: python list

说我有这样的清单:

board = 
[[0, 0, 0, 0, 0,], 
[0, 0, 0, 0, 0,], 
[0, 0, 0, 0, 0,], 
[0, 0, 0, 0, 0,], 
[0, 0, 0, 0, 0,]

表示2D板上的空间 如果玩家一个人去,它将变为1 如果玩家2进入,它将变为2。

胜利条件是:如果行或列完全填充或对角线。

以下是横向或纵向获胜者的功能:

def horizontal_winner(board, boxes):
    '''
    function will find if the horizontal win conditions apply
    given 2 inputs. the board, a list and the number of boxes
    - board - the 2D board of the game
    - boxes - number of boxes per side
    ''' 
    for i in range(boxes):
        player_1 = 0
        player_2 = 0
        for j in range(boxes):
            if board[i][j] == 1:# first iteration - [1, 0, 0, 0, 0]
                player_1 += 1
                print("p1: " + str(player_1))
            elif board[i][j] == 2:# first iteration - [2, 0, 0, 0, 0]
                player_2 += 1
                print("p2: " + str(player_2))
        if player_1 == boxes:
            return True
        elif player_2 == boxes:
            return False


def vertical_winner(board, boxes):
    '''
    function will find if the vertical win conditions apply
    given 2 inputs. the board, a list and the number of boxes per side
    - board - the 2D board of the game
    - boxes - number of boxes per side
    '''
    for i in range(boxes):
        player_1 = 0
        player_2 = 0 
        for j in range(boxes):
            if board[j][i] == 1:# first iteration - [1, 0, 0, 0, 0]
                player_1 += 1
            elif board[j][i] == 2:# first iteration - [2, 0, 0, 0, 0]
                player_2 += 1
        if player_1 == boxes:
            return True
        elif player_2 == boxes:
            return False

我如何对角检查?

3 个答案:

答案 0 :(得分:3)

首先,要认识到只有2个对角线。并且它们上的每个框的坐标是(i,i)或(box-i-1,i),对于范围内的一些i(框)。这应该可以帮助你搞清楚。

答案 1 :(得分:1)

使用all()

尝试这样的事情

i=0开始到i=len(board)-1,可以使用board[i][i]获取对角元素:

In [116]: board=[[1, 0, 0, 0, 0,], 
[0, 1, 0, 0, 0,], 
[0, 0, 1, 0, 0,], 
[0, 0, 0, 1, 0,], 
[0, 0, 0, 0, 1,]]

In [117]: all(board[i][i]==board[0][0] for  i in range(len(board)))
Out[117]: True

In [119]: [board[i][i] for  i in range(len(board))]  #value of diagonal elements
Out[119]: [1, 1, 1, 1, 1]

# another example:

In [120]: board=[[0, 0, 0, 0, 0,], 
[0, 1, 0, 0, 0,], 
[0, 0, 1, 0, 0,], 
[0, 0, 0, 1, 0,], 
[0, 0, 0, 0, 1,]]

In [121]: [board[i][i] for  i in range(len(board))]
Out[121]: [0, 1, 1, 1, 1]

In [122]: all(board[i][i]==board[0][0] for  i in range(len(board)))
Out[122]: False

答案 2 :(得分:0)

你必须分别考虑四个案例。我将用例子解释

单行连续1次

>>> board1 =[[0, 0, 0, 0, 0,],
[1, 1, 1, 1, 1,],
[0, 0, 0, 0, 0,],
[0, 0, 0, 0, 0,],
[0, 0, 0, 0, 0,]]
>>> any(map(all,board1))
True

单列中连续1个

>>> board2 =[[0, 1, 0, 0, 0,],
[0, 1, 0, 0, 0,],
[0, 1, 0, 0, 0,],
[0, 1, 0, 0, 0,],
[0, 1, 0, 0, 0,]]
>>> any(map(all,zip(*board2)))
True

前向对角线连续1'

>>> board3 =[[1, 0, 0, 0, 0,],
[0, 1, 0, 0, 0,],
[0, 0, 1, 0, 0,],
[0, 0, 0, 1, 0,],
[0, 0, 0, 0, 1,]]
>>> all(list(islice(chain(*board3),0,None,len(board4)+1)))
True

反向对角连续1'

>>> board4 =[[0, 0, 0, 0, 1,],
[0, 0, 0, 1, 0,],
[0, 0, 1, 0, 0,],
[0, 1, 0, 0, 0,],
[1, 0, 0, 0, 0,]]
>>> all(list(islice(chain(*reversed(board4)),0,None,len(board4)+1)))
True

总结

from itertools import chain, isclice
any(any(map(all,board)),
    any(map(all,zip(*board))),
    all(list(islice(chain(*board),0,None,len(board4)+1))),
    all(list(islice(chain(*reversed(board)),0,None,len(board4)+1))))