OOD - Chess Game


Game:

  • Contains the Board and 2 Players
  • Commands List (for history tracking)

Board (Singleton):

  • Hold spots with 8*8
  • Initialize the piece when game start
  • Move Piece
  • Remove Piece

Spot:

  • Hold Pieces

Piece (Abstract):

  • Hold the color to represent the affiliation.
  • Extended by concreted classes with 8 Pawns, 2 Rooks, 2 Bishops, 2, Knights, 1 Queen, 1 King
  • Concreted classes define the detail step approach

Player (Abstract):

  • Has a list of piece reference it owns.
  • Concreted classes for Human and Computer players

Command

  • Piece
  • Destination x, y
class Game {
    Board board;
    
    Player p1;
    Player p2;

    // constructor
    public Game() {
        board = new Board();
    } 

    public boolean enterPlayer(Player p) {
        // assign p1, p2
        // board.initSpots(p);
    }

    public void processTurn(Player p) {
        // get user input: next move
        // board.executeMove(p);
    }
    
    public startGame() {
        // enterPlayer p1, p2

        // processTurn(player)
    }
}

class Board {
    private Spot[][] spots;
    private boolean win;
    
    public Board() {
        spots = new Spot[8][8];
        win = false;
    }

    public boolean initSpots(Player p) {
        // set spots for init pieces: spots[][].setSpot(p.getPieces())
    }

    public boolean executeMove(Player p) {
        // get move cmd: p.getCurCmd()
        // check valid move: p.isValid()
        //   if invalid, rm cmd: p.removeCurCmd()
        // check occupied
        //   if same color, rm cmd: p.removeCurCmd()
        //   if diff color, set spot: spots[][].setSpot(piece)
        //     check if win: king got killed
        //   clear piece old spot: spots[][].clearSpot()
    }
}

// Spot class can combine into Board class
class Spot {
    int row;
    int col;
    Piece piece;

    public Spot(int row, int col) {}

    public Piece setSpot(Piece piece) { // return old piece
        // set old piece to dead: p.setAlive(false)
        // set piece = new piece
    }
    public Piece getSpot() {}
    public Piece clearSpot() {} // empty this spot
}

class Piece {
    private int row;
    private int col;

    private boolean alive;
    private int color;

    public Piece(int row, int col, boolean alive, int color) {}

    public boolean isAlive() {}
    public void setAlive(boolean alive) {}
    public int getRow() {}
    public void setRow(int row) {}
    public int getCol() {}
    public void getCol(int col) {}
    public boolean isValid(Board b, int curRow, int curCol, int row, int col) {} // based on move rules
}

public class King extends Piece{ 
    @Override
    public boolean isValid(Board board, int fromX, int fromY, int toX, int toY) {}    
}
// ..... for Queen, Rook, Bishop, Pawn

class Player {
    public int color;
    List<Piece> pieces = new ArrayList<>();
    List<Command> commands = new ArrayList<>();

    public Player(int color) {
        // ...
        initializePieces();
    }

    public void initializePieces(){}

    public List<Piece> getPieces() {}
    public Command getCurCmd() {}
    public void removeCurCmd() {}
}

class Command {
    Piece piece;
    int curX, curY, desX, desY;

    public Command(Piece piece, int curX, int curY, int desX, int desY) {
        this.piece = piece; 
        this.curX = curX;
        this.curY = curY;
        this.desX = desX;
        this.desY = desY;
    }
}

Reference

https://massivetechinterview.blogspot.com/2015/07/design-chess-game-using-oo-principles.html


Author: Zijun Zhou
Reprint policy: All articles in this blog are used except for special statements CC BY 4.0 reprint policy. If reproduced, please indicate source Zijun Zhou !
  TOC