I am trying to program a chess computer. I am having a weird problem with a dictionary here. As you can see in the code below, I am only editing the pieces attribute of the Player object saved at 'w' in my dictionary self.players
. However, when I run the code, for both Player objects created (Player('w')
and Player('b')
), the d[(1,1)].piece
object appears in their respective Player.pieces['R']
. How can this be?
(In the code, only the class Board
should be really important however I included the other classes so the code works).
import numpy as np
class Board:
def __init__(self,standard=True):
self.players={}
self.players['w']=Player('w')
self.players['b']=Player('b')
d={}
d[(1,1)]=Field(np.array([1,1]),'ds','ds'+'.png')
d[(1,1)].piece=Rook(np.array([1,1]),'w',self)
self.players['w'].pieces['R'].append(d[(1,1)].piece)
class Player:
def __init__(self,color,rooks=[],bishops=[],knights=[],queen=[],king=[],pawns=[]):
self.color=color
self.pieces={}
self.pieces['R']=rooks
class Pieces:
def __init__(self,position,color,board):
self.position=position
self.color=color
if self.color=='w':
self.enemycolor='b'
else:
self.enemycolor='w'
self.moves=[]
self.board=board
class Rook(Pieces):
def __init__(self,position,color,board):
super().__init__(position,color,board)
self.prev_move=False
self.typ='R'
class Field:
def __init__(self,position,color,pic,piece=None):
self.piece=piece
self.position=position
self.color=color
self.pic=pic
b=Board()
print(b.players['w'].pieces['R'])
print(b.players['b'].pieces['R'])