Loading...
For each class, define the attributes (data) it will hold and the methods (functions) that operate on the attributes. Ensure they align with the object's responsibilities and adhere to the principle of encapsulation.
class Board:
def __init__(self):
self.board = [[None for _ in range(8)] for _ in range(8)]
self.initializeBoard()
def movePiece(self, originRow, originCol, destinationRow, destinationCol):
if self.board[originRow][originCol].checkMove(destinationRow, destinationCol):
currentPiece = self.board[originRow][originCol]
self.board[originRow][originCol] = None
self.board[destinationRow][destinationCol] = currentPiece
def initializePawns(self, row, color):
for col in range(len(self.board)):
self.board[row][col] = Pawn(color=color)
def initializeAdvancePiece(self, row, color):
self.board[row][0] = Rook(color=color)
self.board[row][7] = Rook(color=color)
self.board[row][1] = Knight(color=color)
self.board[row][6] = Knight(color=color)
self.board[row][2] = Bishop(color=color)
self.board[row][5] = Bishop(color=color)
self.board[row][3] = Queen(color=color)
self.board[row][4] = King(color=color)
def initializeBlack(self):
initializePawns(1,"Black")
initializeAdvancePiece(0,"Black")
def initializeWhite(self):
initializePawns(6,"White")
initializeAdvancePiece(7,"White")
def initializeBoard(self):
self.initializeBlack()
self.initializeWhite()
class ChessPiece:
def init(self, name, color, row, col):
self.name = name
self.color = color
self.row = row
self.col = col
def checkMove(self, destinationRow, destinationCol):
# checks if piece can move to new spot
pass
class Pawn(ChessPiece):
def init(self, name="Pawn", color, row, col):
super().init(name, color, row, col)
def checkMove(self, destinationRow, destinationCol):
# checks if piece can move to new spot
class King(ChessPiece):
def init(self, name="Pawn", color, row, col):
super().init(name, color, row, col)
def checkMove(self, destinationRow, destinationCol):
possibleMovementSet = {
[self.row-1,self.col],
[self.row+1,self.col],
[self.row-1,self.col-1],
[self.row+1,self.col-1],
[self.row-1,self.col+1],
[self.row+1,self.col+1],
[self.row,self.col-1],
[self.row,self.col+1],
}
return [destinationRow,destinationCol] in possibleMoveSet
class Queen(ChessPiece):
def init(self, name="Pawn", color, row, col):
super().init(name, color, row, col)e, color)
def checkMove(self, destinationRow, destinationCol):
dr = abs(self.row - destinationRow)
dc = abs(self.col - destinationCol)
diagonalMove = dr == dc
horizontalMove = self.row == destinationRow and self.col != destinationCol
verticalMove = self.col == destinationCol and self.row != desinationRow
straightMove = horizontalMove or verticalMove
return diagonalMove or straightMove
class Bishop(ChessPiece):
def init(self, name="Pawn", color, row, col):
super().init(name, color, row, col)
def checkMove(self, destinationRow, destinationCol):
dr = abs(self.row - destinationRow)
dc = abs(self.col - destinationCol)
return dr == dc
class Rook(ChessPiece):
def init(self, name="Pawn", color, row, col):
super().init(name, color, row, col)
def checkMove(self, destinationRow, destinationCol):
horizontalMove = self.row == destinationRow and self.col != destinationCol
verticalMove = self.col == destinationCol and self.row != desinationRow
return horizontalMove or verticalMove
class Knight(ChessPiece):
def init(self, name="Pawn", color, row, col):
super().init(name, color, row, col)
def checkMove(self, destinationRow, destinationCol):
possibleMovementSet = {
[self.row-2,self.col+1],
[self.row-2,self.col-1],
[self.row+2,self.col+1],
[self.row+2,self.col-1],
[self.row+1,self.col-2],
[self.row-1,self.col-2],
[self.row+1,self.col+2],
[self.row-1,self.col+2],
}
return [destinationRow, destinationCol] in possibleMoveSet
class Game:
def init(self):
self.playerTurn = "Black"
self.board = Board()
def playerTurn(self, color, originRow, originCol, destinationRow, destinationCol):
if color == self.playerTurn:
if self.board.movePiece(originRow, originCol, destinationRow, destinationCol):
if self.playerTurn == "Black":
self.playerTurn = "White"
else:
self.playerturn = "Black"
Single Responsibility:
Board class responsible for state of chess board
Game responsible for orchestrating game
Chess Piece responsible for piece movement
Open/Close:
Chess pieces are open for extension and closed for modification
Liskov:
Any Chess Piece can be replaced specific chess piece
ISP:
Nothing?
DIP:
Currently passing in concrete board into Game but in the future we can create an IBoard interface
Designs:
Factory method to generate chess pieces for the board