Creating a black and white checkerboard in a two-dimensional array

Is there a better (and shorter) way to create a chessboard, such as an array. Board Requirements:

  • Pay
  • can be of different sizes (in my example it is 3x3)
  • the bottom left square of the board should always be black
  • the black square is represented by "B" , the white square is represented by "W"

The code I have is:

 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 

Output:

 ['B', 'W', 'B'] ['W', 'B', 'W'] ['B', 'W', 'B'] 
+4
source share
7 answers

What about:

 >>> 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']] 
+8
source

Kind of hack but

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

This is similar to creep requirements or something =)

 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] 
+2
source

Here is the itertools solution:

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

For N=4 this gives

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

This should be extensible for several colors (for example, the board that goes β€œB”, β€œW”, β€œG”), if you are sure to add each new line and loop to the list of lines.

+2
source
 for i in range(len(board)): for j in range(len(board)): if isEven(i + j + len(board)): board[i][j] = "W" 
0
source

This correctly sets the bottom left corner to "B", always:

 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)] 
0
source

It’s cruel to understand. In addition, you can create a rectangular panel:

 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 

Test Drive:

 >>> print_board(gen_board(4, 3)) ['B', 'W', 'B', 'W'] ['W', 'B', 'W', 'B'] ['B', 'W', 'B', 'W'] 
0
source

With single line numpy code without loop:

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

and output:

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

To populate an array of W and B :

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

and output:

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

Source: https://habr.com/ru/post/1478869/


All Articles