2013-05-02 31 views
4

有沒有更好(也是更短)的如何創建類似棋盤棋盤的方法。對董事會的要求是:在二維陣列中創建黑白棋盤

  • 板可以是不同的大小(以我的例子是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

我敢肯定,你可以有使用模量一定的情況下。每個位置的顏色將基於行和列號。 – Evo510 2013-05-02 21:02:02

+1

國際海事組織,採取預先打擊和使用位面板! - 它們在一切方面都嚴格優越:內存佔用,計算速度......它比嵌套的字符串列表更適合更優雅的設計。 – hexparrot 2013-05-02 21:17:55

+0

你有沒有考慮將它列爲一個單子列表,按順序填入適當的值,然後將它重新組裝成一個MxN網格? – 2013-05-02 21:06:40

回答

8

如何:

>>> 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']] 
+0

+1,如果我真的很棒,這就是我所要做的。 – John 2013-05-02 21:08:10

+1

爲什麼要用索引來做這個奇特的東西? '(i + j)%2'就足夠了。 – 2013-05-02 21:25:01

+0

@android:不,它不是。如果你只是使用(i + j)%2,你不會總是得到「董事會左下角的方塊應該始終是黑色的」,並且你的逆轉技巧不會出現在我身上。 – DSM 2013-05-03 01:54:55

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] 
+0

爲什麼downvote? – John 2013-05-02 21:09:45

+0

它不能正常工作n = 4,因爲左下角不是黑色 – piokuc 2013-05-02 21:22:32

+0

@piokuc好抓,固定。 – John 2013-05-02 21:31:58

0
for i in range(len(board)): 
    for j in range(len(board)): 
     if isEven(i + j + len(board)): 
      board[i][j] = "W" 
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)] 
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」)。

+0

左下角應該是'B' – piokuc 2013-05-02 21:17:59

+0

@piokuc謝謝,修正。我所要做的就是改變開始順序。直到這個時候,棋盤纔有了定位才意識到! – Hooked 2013-05-02 21:19:54

+0

在問題 – piokuc 2013-05-02 21:20:22

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'] 
0

隨着一個線numpy的代碼,而無需循環:

import numpy as np 

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

並且輸出是:

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

要用填充陣列和B

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

,輸出是:

array([['B', 'W', 'B'], 
     ['W', 'B', 'W'], 
     ['B', 'W', 'B']]) 
+0

中明確指出哪一行是一行? – 2016-07-05 13:42:37

+0

行'chessbool =(np.arange(3)[:,None] + np.arange(3))%2 == 0'。其他行填寫'W'和'B'布爾數組 – 2016-07-05 13:44:20

+0

只有第一行輸出是'array([[True,False,True], [False,True,False], [True ,False,True]])' – 2016-07-05 13:44:53