I'm trying to model a chess game in python 3 with object-oriented programming.
One of the class i am using is Board which contain a matrix (8x8 list of list) with the other object.
I also created a simple __str__
method to visualize the chess board.
Here is some part of the Board class definition :
class Board:
def __init__(self, pieces: list):
self.container = [[pieces[i] for i in range(8)], # Whites
[pieces[i] for i in range(8, 16)], # White pawns
[None] * 8,
[None] * 8,
[None] * 8,
[None] * 8,
[pieces[i] for i in range(16, 24)], # Black pawns
[pieces[i] for i in range(24, 32)]] # Blacks
def __str__(self):
str_copy = self.container.copy()
for i in range(8):
for j in range(8):
if isinstance(str_copy[i][j], King):
str_copy[i][j] = 'K'
elif isinstance(str_copy[i][j], Queen):
str_copy[i][j] = 'Q'
elif isinstance(str_copy[i][j], Rook):
str_copy[i][j] = 'R'
elif isinstance(str_copy[i][j], Knight):
str_copy[i][j] = 'N'
elif isinstance(str_copy[i][j], Bishop):
str_copy[i][j] = 'B'
elif isinstance(str_copy[i][j], Pawn):
str_copy[i][j] = 'P'
elif str_copy[i][j] is None:
str_copy[i][j] = '_'
return ''.join([str(line) + '\n' for line in str_copy])
My problem is that at some point in my code Board.container
seems to be overwritten by the str_copy
.
I really can't figure out why.
Here is the full code if you want to have a look at it: pastebin
Thanks a lot for your help !