# Design a Chess Game

Solution:

These types of questions are asked in interviews to Judge the Object-Oriented Design skill of a candidate. So, first of all, we should think about the classes.

The main classes will be:

1. Cell: A spot represents one block of the `8×8 grid` and an optional piece.
2. Piece: The basic building block of the system, every piece will be placed on a spot. Piece class is an abstract class. The extended classes (Pawn, King, Queen, Rook, Knight, Bishop) implements the abstracted operations.
3. ChessBoard: Board is an 8×8 set of boxes containing all active chess pieces.
4. Player: The player class represents one of the participants playing the game.
5. Move: Represents a game move, containing the starting and ending spot. The Move class will also keep track of the player who made the move.
6. 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.
7. Let’s look at the details. These codes are self-explanatory. You can have a look at the properties/variables and methods of different classes.
`Constant`
```package lld.chess;

public class Constant {

public enum GameStatus {

ACTIVE, PAUSED, FORTFEIGHT, BLACK_WIN, WHITE_WIN;
}

public enum Color {

BLACK, WHITE;

}
}
```
`ChessGame`
```package lld.chess;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ChessGame {

private Game game;
private List<Player> playerList;

// This is a stub which should be in database
private Map<Integer, Game> gameRepository = new HashMap<>();

public int startGame(Player playerWithWhitePiece, Player playerWithBlackPiece) {
game = new Game(playerWithWhitePiece, playerWithBlackPiece);
return game.getId();
}

public boolean isGameEnded(int gameId) {
Game game = gameRepository.get(gameId);
return game.isGameEnded();
}

public Player getWinner(int gameId) {
Game game = gameRepository.get(gameId);
return game.getWinner();
}
}
```
`Game`
```package lld.chess;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class Game {

private int id;
private ChessBoard chessBoard;
private List<Move> moveList;
private Player[] players;
private Player currentPlayer;
private Constant.GameStatus gameStatus;

private static final Random random = new Random();

public Game(Player player1, Player player2) {
id = random.nextInt();
chessBoard = new ChessBoard();
moveList = new ArrayList<>();
players = new Player[]{player1, player2};
currentPlayer = player1;
gameStatus = Constant.GameStatus.ACTIVE;
}

public int getId() {
return id;
}

public Player getCurrentPlayer() {
return currentPlayer;
}

public Constant.GameStatus getGameStatus() {
return gameStatus;
}

public void makeMove(Move move, Player player) {
chessBoard.updateMove(move);
changeTurn();
}

public boolean isGameEnded() {
return false;
}

public Player getWinner() {
if (isGameEnded()) {
return currentPlayer;
}
return null;
}

private void changeTurn() {
currentPlayer = currentPlayer ==
players[0] ? players[1] : players[0];
}
}
```
`ChessBoard`
```package lld.chess;

public class ChessBoard {

private Cell[][] board = new Cell[8][8];

public void updateMove(Move move) {

}

private void reset() {

}
}
```
`Cell`
```package lld.chess;

public class Cell {

private Piece piece;
private int[][] position;
private Constant.Color color;
}
```
`Move`
```package lld.chess;

public class Move {

private int[][] fromPosition;
private int[][] toPosition;

private Player player;
private Piece piece;
private Piece killedPiece;
}
```
`Player`
```package lld.chess;

public class Player {

private int gameId;
private String name;
private String email;
private Constant.Color color;
}
```
`Piece`
```package lld.chess;

import java.util.ArrayList;
import java.util.List;

public abstract class Piece {

Constant.Color color;

public abstract boolean move(int[][] fromPosition, int[][] toPosition);

public abstract List<int[][]> possibleMoves(int[][] fromPosition);

public abstract boolean validate(int[][] fromPosition, int[][] toPosition);
}

class Knight extends Piece {

public boolean move(int[][] fromPosition, int[][] toPosition) {
return false;
}

public List<int[][]> possibleMoves(int[][] fromPosition) {
return new ArrayList<>();
}

public boolean validate(int[][] fromPosition, int[][] toPosition) {
return true;
}

}

class Bishop extends Piece {

public boolean move(int[][] fromPosition, int[][] toPosition) {
return false;
}

public List<int[][]> possibleMoves(int[][] fromPosition) {
return new ArrayList<>();
}

public boolean validate(int[][] fromPosition, int[][] toPosition) {
return true;
}

}

class Rook extends Piece {

public boolean move(int[][] fromPosition, int[][] toPosition) {
return false;
}

public List<int[][]> possibleMoves(int[][] fromPosition) {
return new ArrayList<>();
}

public boolean validate(int[][] fromPosition, int[][] toPosition) {
return true;
}

}

class King extends Piece {

public boolean move(int[][] fromPosition, int[][] toPosition) {
return false;
}

public List<int[][]> possibleMoves(int[][] fromPosition) {
return new ArrayList<>();
}

public boolean validate(int[][] fromPosition, int[][] toPosition) {
return true;
}
}
```

Categories: LLD