Checkers Board Structure

I use a panel with checkers with python. This is how I create the board structure as an array [8] [8]:

 _matrix = []
 for i in xrange(8):
     _matrix.append( [' '] * 8 )


 for row in xrange(0, 8):
        for col in xrange(0, 8):
            if _darkQuad(row, col) == True:
                _matrix[row][col] = '#'
            else:
                _matrix[row][col] = '-'

def _darkQuad(row, col):
    return ((row%2) == (col%2))

def _printDebugBoard():
    for row in xrange(0, 8):
        for col in xrange(0, 8):
            print _matrix[row][col]
        print ''

This should do my advice, like:

 # - # - # - # -
 - # - # - # - #
 ...

But the result:

- - - - - - - - 
# # # # # # # # 
- - - - - - - - 
# # # # # # # # 
- - - - - - - - 
# # # # # # # # 
- - - - - - - - 
# # # # # # # # 

What's wrong?


UPD : Hmm, I didn’t think it would be important. I simplified the code, but here is the exact code that I use:

class gameSquare(object):
    def __init__(self):
        self.validSquare = False
        self.symbol = ''

    def printSymbol(self):
        print self.symbol,
-------
def _validateSquares(self):
    for row in xrange(0, 8):
        for col in xrange(0, 8):
            if self._darkQuad(row, col) == True:
                self._matrix[row][col].validSquare = False
                self._matrix[row][col].symbol = '#'
            else:
                self._matrix[row][col].validSquare = True
                self._matrix[row][col].symbol = '-'

--------
for i in xrange(8):
    self._matrix.append( [gameSquare()] * 8 )

-------
def _printDebugBoard(self):
    print ''
    for row in xrange(0, 8):
        for col in xrange(0, 8):
            self._matrix[row][col].printSymbol()
        print ''
+3
source share
4 answers

Update for recently published code

The problem is the line:

self._matrix.append( [gameSquare()] * 8 )

This will create 8 references to each line for the same object. When you change one of them, it will actually change the whole line. To fix this:

self._matrix.append( [gameSquare() for _ in xrange(8)] )

Final update


Your code works for me. The only thing needed to fix it:

print _matrix[row][col],

instead:

print _matrix[row][col]

To avoid printing each character on a different line.

, ( _darkQuad ).

+5

. , , :

# Moved this function up here so it can be called.
def _darkQuad(row, col): 
    return row % 2 == col % 2

def _printDebugBoard():
    for row in xrange(8):
        for col in xrange(8):
            print _matrix[row][col],
            # Added the comma here ^ so that I don't get unwanted newlines
        print ''

_matrix = []
for i in xrange(8):
    _matrix.append([' '] * 8)


for row in xrange(8):
    for col in xrange(8):
        if _darkQuad(row, col): 
            _matrix[row][col] = '#'
        else:
            _matrix[row][col] = '-'

_printDebugBoard() # called the _printDebugBoard function

:

# - # - # - # - 
- # - # - # - # 
# - # - # - # - 
- # - # - # - # 
# - # - # - # - 
- # - # - # - # 
# - # - # - # - 
- # - # - # - # 

( , , . if _darkQuad(row, col) == True: if _darkQuad(row, col):, , xrange(0, 8) xrange(8), .)

+1

All you have to do is:

print _matrix[row][col],

A comma tells the interpreter to skip carriage returns after the print instruction. A.

0
source

I would do something like this:

matrix = ['#-' * 4, '-#' * 4] * 4

Keeps math to a minimum.

0
source

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


All Articles