Design: Chess Piece Movement System
Design focuses on piece movement logic and rules using polymorphism. It excludes UI, game state management, and network communication.
Functional Requirements
Non-Functional Requirements
Jump into concepts and practice - no test required
+---------------------+
| Board |
| - grid: 8x8 array |
+----------+----------+
|
v
+---------------------+ +---------------------+
| Piece (base) |<------+ PieceFactory |
| - position | | - createPiece(type) |
| + getValidMoves() | +---------------------+
+----------+----------+
|
+----------+----------+----------+----------+----------+----------+
| | | | | | |
v v v v v v v
Pawn Rook Knight Bishop Queen King CustomPiece
(move rules implemented in each subclass using polymorphism)move() method, so no need to check piece type before moving.self as the first parameter.def move(self): pass correctly declares a method with self and no implementation.class Piece:
def move(self):
return "Base move"
class Knight(Piece):
def move(self):
return "L-shaped move"
pieces = [Piece(), Knight()]
for p in pieces:
print(p.move())Knight overrides move() to return "L-shaped move".Piece, prints "Base move"; second is Knight, prints "L-shaped move".class Piece:
def move(self):
pass
class Bishop(Piece):
def move():
print("Diagonal move")
b = Bishop()
b.move()Bishop.move() lacks self parameter, so it is not a proper instance method.b.move() passes self automatically, causing a TypeError due to missing parameter.move() method to define interface.move() logic, enabling extension without modifying base code.