Grokking the Object Oriented Design Interview
Ask Author
Back to course home

0% completed

Vote For New Content
Design Chess
Table of Contents

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible

Let's design a system to play online chess.

Chess is a two-player strategy board game played on a chessboard, which is a checkered gameboard with 64 squares arranged in an 8×8 grid. There are a few versions of game types that people play all over the world. In this design problem, we are going to focus on designing a two-player online chess game.

Image

System Requirements

We'll focus on the following set of requirements while designing the game of chess:

  1. The system should support two online players to play a game of chess.

  2. All rules of international chess will be followed.

  3. Each player will be randomly assigned a side, black or white.

  4. Both players will play their moves one after the other. The white side plays the first move.

  5. Players can't cancel or roll back their moves.

  6. The system should maintain a log of all moves by both players.

  7. Each side will start with 8 pawns, 2 rooks, 2 bishops, 2 knights, 1 queen, and 1 king.

  8. The game can finish either in a checkmate from one side, forfeit or stalemate (a draw), or resignation.

Use case diagram

We have two actors in our system:

  • Player: A registered account in the system, who will play the game. The player will play chess moves.
  • Admin: To ban/modify players.

Here are the top use cases for chess:

  • Player moves a piece: To make a valid move of any chess piece.
  • Resign or forfeit a game: A player resigns from/forfeits the game.
  • Register new account/Cancel membership: To add a new member or cancel an existing member.
  • Update game log: To add a move to the game log.
Image

Class diagram

Here are the main classes for chess:

  • Player: Player class represents one of the participants playing the game. It keeps track of which side (black or white) the player is playing.

  • Account: We'll have two types of accounts in the system: one will be a player, and the other will be an admin.

  • Game: This class controls the flow of a game. It keeps track of all the game moves, which player has the current turn, and the final result of the game.

  • Box: A box represents one block of the 8x8 grid and an optional piece.

  • Board: Board is an 8x8 set of boxes containing all active chess pieces.

  • Piece: The basic building block of the system, every piece will be placed on a box. This class contains the color the piece represents and the status of the piece (that is, if the piece is currently in play or not). This would be an abstract class and all game pieces will extend it.

  • Move: Represents a game move, containing the starting and ending box. The Move class will also keep track of the player who made the move, if it is a castling move, or if the move resulted in the capture of a piece.

  • GameController: Player class uses GameController to make moves.

  • GameView: Game class updates the GameView to show changes to the players.

Image
Image

Activity diagrams

Make move: Any Player can perform this activity. Here are the set of steps to make a move:

Image

Code

Here is the code for the top use cases.

Enums, DataTypes, Constants: Here are the required enums, data types, and constants:

Python3
Python3
. . . .

Box: To encapsulate a cell on the chess board:

Python3
Python3
. . . .

Piece: An abstract class to encapsulate common functionality of all chess pieces:

Python3
Python3
. . . .

King: To encapsulate King as a chess piece:

Python3
Python3
. . . .

Knight: To encapsulate Knight as a chess piece:

Python3
Python3
. . . .

Board: To encapsulate a chess board:

Python3
Python3
. . . .

Player: To encapsulate a chess player:

Python3
Python3
. . . .

Move: To encapsulate a chess move:

Python3
Python3
. . . .

Game: To encapsulate a chess game:

Python3
Python3
. . . .

.....

.....

.....

Like the course? Get enrolled and start learning!

Table of Contents

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible