开发者

Checkers board structure

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:
开发者_如何学Go                _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 ''


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


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


All you need to do is:

print _matrix[row][col],

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


I'd go for something like this:

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

It keeps the math to a minimum.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜