5

有没有更好(和更短)的方法来创建像数组一样的棋盘。对董事会的要求是:

  • 板可以有不同的尺寸(在我的例子中是 3x3)
  • 棋盘的左下角应始终为黑色
  • 黑色方块由 呈现"B",白色方块由 呈现"W"

我拥有的代码:

def isEven(number):
    return number % 2 == 0

board = [["B" for x in range(3)] for x in range(3)]
if isEven(len(board)):
    for rowIndex, row in enumerate(board):
        if isEven(rowIndex + 1):
            for squareIndex, square in enumerate(row):
                if isEven(squareIndex + 1):
                    board[rowIndex][squareIndex] = "W"
        else:
            for squareIndex, square in enumerate(row):
                if not isEven(squareIndex + 1):
                    board[rowIndex][squareIndex] = "W"
else:
    for rowIndex, row in enumerate(board):
        if not isEven(rowIndex + 1):
            for squareIndex, square in enumerate(row):
                if isEven(squareIndex + 1):
                    board[rowIndex][squareIndex] = "W"
        else:
            for squareIndex, square in enumerate(row):
                if not isEven(squareIndex + 1):
                    board[rowIndex][squareIndex] = "W"

for row in board:
    print row

输出:

['B', 'W', 'B']
['W', 'B', 'W']
['B', 'W', 'B']
4

7 回答 7

10

怎么样:

>>> n = 3
>>> board = [["BW"[(i+j+n%2+1) % 2] for i in range(n)] for j in range(n)]
>>> print board
[['B', 'W', 'B'], ['W', 'B', 'W'], ['B', 'W', 'B']]
>>> n = 4
>>> board = [["BW"[(i+j+n%2+1) % 2] for i in range(n)] for j in range(n)]
>>> print board
[['W', 'B', 'W', 'B'], ['B', 'W', 'B', 'W'], ['W', 'B', 'W', 'B'], ['B', 'W', 'B', 'W']]
于 2013-05-02T21:06:30.860 回答
2

有点像黑客,但是

print [["B" if abs(n - row) % 2 == 0 else "W" for n in xrange(3)] for row in xrange(3)][::-1]

这似乎是需求蠕变或其他东西=)

def make_board(n):
    ''' returns an empty list for n <= 0 '''
    return [["B" if abs(c - r) % 2 == 0 else "W" for c in xrange(n)] for r in xrange(n)][::-1]
于 2013-05-02T21:04:19.077 回答
2

这是一个itertools解决方案:

from itertools import cycle
N = 4

colors = cycle(["W","B"])
row_A  = [colors.next() for _ in xrange(N)]
if not N%2: colors.next()
row_B  = [colors.next() for _ in xrange(N)]

rows = cycle([row_A, row_B])
board = [rows.next() for _ in xrange(N)]

因为N=4这给了

['W', 'B', 'W', 'B']
['B', 'W', 'B', 'W']
['W', 'B', 'W', 'B']
['B', 'W', 'B', 'W']

如果您确保添加每个新行并循环到行列表中,这应该可以扩展为多种颜色(例如一块“B”、“W”、“G”的板)。

于 2013-05-02T21:14:58.187 回答
0
for i in range(len(board)):
    for j in range(len(board)):
        if isEven(i + j + len(board)):
            board[i][j] = "W"
于 2013-05-02T21:05:16.827 回答
0

这个正确地将左下角设置为“B”,始终:

def board(n):
    def line(n, offset):
        return [(i+offset) % 2 and 'W' or 'B' for i in range(n)]
    return [line(n,i) for i in range(n+1,1,-1)]
于 2013-05-02T21:12:48.663 回答
0

粗暴容易理解。另外,可以生成一个矩形板:

def gen_board(width, height):
    bw = ['B', 'W']
    l = [[bw[(j + i) % 2] for j in range(width)] for i in range(height)]
    # this is done to make sure B is always bottom left
    # alternatively, you could do the printing in reverse order
    l.reverse()

    ## or, we could ensure B is always bottom left by adjusting the index
    #offset = height%2 + 1
    #l = [[bw[(j + i + offset) % 2] for j in range(width)] for i in range(height)]
    return l

def print_board(b):
    for row in b:
        print row

试驾:

>>> print_board(gen_board(4, 3))
['B', 'W', 'B', 'W']
['W', 'B', 'W', 'B']
['B', 'W', 'B', 'W']
于 2013-05-02T21:22:17.190 回答
0

使用一行 numpy 代码,没有 for 循环:

import numpy as np

chessbool = (np.arange(3)[:, None] + np.arange(3)) % 2 == 0

输出是:

array([[ True, False,  True],
       [False,  True, False],
       [ True, False,  True]]

W用and填充数组B

chessboard = np.where(chessbool,'B','W')

输出是:

array([['B', 'W', 'B'],
       ['W', 'B', 'W'],
       ['B', 'W', 'B']])
于 2016-07-05T13:25:03.473 回答