python绘制黑白棋盘_python – 在二维数组中创建一个黑白棋盘
是否有更好(和更短)的方式来创建像数组一样的棋盘.董事会的要求是:>板子可以是不同的尺寸(在我的例子中它是3×3)>板的左下方应始终为黑色>黑色方块用“B”表示,白色方块用“W”表示我有的代码:def isEven(number):return number % 2 == 0board = [["B" for x in range(3)] for x in range(3)]if isEven(l
是否有更好(和更短)的方式来创建像数组一样的棋盘.董事会的要求是:
>板子可以是不同的尺寸(在我的例子中它是3×3)
>板的左下方应始终为黑色
>黑色方块用“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']
更多推荐
所有评论(0)