Make chessboard setup more efficient
Closed this issue · 11 comments
Right now each square is initialized to an empty piece in Board.__init__
. Then, in Board.setup
, each square is looped over and potentially replaced with another piece.
We can probably do that all in one go, and halve the work we need to do for setup. I'm thinking about moving the "which piece goes on this square?" logic to Square.__init__
, which already has access to its row & column
wouldnt that make init more messy? also, we can use the current setup for a 'play again' prompt after a winning condition is fullfilled
Delegating the logic to Square.reset
and calling it in Square.__init__
instead of blindly assigning a NonePiece
, we can solve both problems! If we do it right, we can simply go:
# in Board
def reset(self):
for square in self:
square.reset()
And Square.reset
will contain the logic for deciding which piece to place.
i see. i like it, but how do you plan to assign default values to the squares?
What do you mean default? If you mean the blank square, that will be in the plethora of if
s in Square.reset
you call reset on square, right? how does square know what it should reset to?
Yes. Because Square
has row
and column
properties!
so you give square a self.default with the setup logic? sounds good.
Oh, I see what you're saying. So basically cache the right piece after Square.reset
figures it out, right? That way Square.reset
can be like:
def reset(self):
if self.default is not None:
if self.row == 1: self.default= Pawn(WHITE)
elif self.row == 6: self.default= Pawn(BLACK)
elif self.row in (0, 7):
color = BLACK if self.default== 7 else WHITE
if self.column in (0, 7): self.default= Rook(color)
elif self.column in (1, 6): self.default= Knight(color)
elif self.column in (2, 5): self.default= Bishop(color)
elif self.column == 3: self.default= Queen(color)
elif self.column == 4: self.default= King(color)
self.piece = self.default
(Man, GitHub is not a good IDE)
yeah. otherwise that'd be setup but more complicated
Ok. I'll get to work on it and open a PR.
Closed with #17