views:

115

answers:

4

I am implementing a checkers game board with python. Here is how I generate the board structure as an [8][8] array:

 _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 board like:

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

But the result is:

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

What's wrong?


UPD: Hm, I didn't think this will be important. I made my code easier but here is the exact code 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 ''
+4  A: 

Update for newly posted code

The problem is with the line:

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

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

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

End update


Your code works for me. Only thing that needs fixing is:

print _matrix[row][col],

instead of:

print _matrix[row][col]

To avoid printing every character on a different line.

I also had to reorder the functions to get the code to run (put _darkQuad at the top).

interjay
Yeah, I use class and made code simplier. Please, look updates.
Ockonal
Great! Many thanks ;)
Ockonal
+1  A: 

I am not able to reproduce this problem. Modifying the code you posted so that it will run:

# 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

gives me:

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

(I also made two changes that didn't affect the result but that reflect better usage. I changed if _darkQuad(row, col) == True: to if _darkQuad(row, col):, which is how you do if statements and I changed xrange(0, 8) to xrange(8), which is the typical usage.)

Mike Graham
Please, see updates for the post
Ockonal
The problem is solved but thanks for the сouncil ;)
Ockonal
A: 

All you need to do is:

print _matrix[row][col],

The comma tells the interpreter to skip the carriage return after the print statement.

Dana
A: 

I'd go for something like this:

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

It keeps the math to a minimum.

hughdbrown