2

假设我有一个这样的列表:

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,]

表示二维板上的空间。
如果玩家一去,它将变为 1。
如果玩家二去,它将变为 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

我将如何检查对角线?

4

3 回答 3

3

首先,认识到只有两条对角线。对于范围(boxes)中的某些 i,它们上每个框的坐标是(i,i)或(boxes-i-1,i)。那应该可以帮助您弄清楚。

于 2012-12-06T19:09:10.997 回答
1

尝试这样的事情,使用all()

i=0to开始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
于 2012-12-06T19:08:41.760 回答
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))))
于 2012-12-06T19:44:40.073 回答