Design: Board and Piece Hierarchy
Focus on the class hierarchy and interactions for board and pieces. Do not cover UI or network communication.
Functional Requirements
Non-Functional Requirements
Jump into concepts and practice - no test required
Board and Piece Hierarchy Diagram
+-----------------+ uses +----------------+
| Board |<-----------------| Position |
|-----------------| +----------------+
| - size | ^
| - grid | |
| + addPiece() | +----------------+
| + movePiece() | | Piece |
| + removePiece() | |----------------|
+-----------------+ | - position |
| | - color |
| contains | + validMoves() |
v +----------------+
+-----------------+ ^
| Cell | |
|-----------------| +----------------+
| - position | | Pawn, Rook, |
| - piece | | Knight, etc. |
+-----------------+ +----------------+Piece class in a board game design?Piece class holds these.King that extends a base Piece class in a typical object-oriented design?extends keyword and calls super() in constructor.class King extends Piece { constructor(position) { super(position); } }.console.log(board.pieces[0].type);?
class Piece {
constructor(type, position) {
this.type = type;
this.position = position;
}
}
class Board {
constructor() {
this.pieces = [];
}
addPiece(piece) {
this.pieces.push(piece);
}
}
const board = new Board();
board.addPiece(new Piece('Knight', 'B1'));
Piece with type 'Knight' and position 'B1' is created and added to board.pieces.board.pieces[0] refers to the first piece, so board.pieces[0].type is 'Knight'.class Piece {
constructor(type, position) {
this.type = type;
this.position = position;
}
}
class Queen extends Piece {
constructor(position) {
this.type = 'Queen';
this.position = position;
}
}super() before using this.this.type and this.position without calling super(), causing an error.Piece lets each piece implement its own move logic, enabling easy extension.