Wednesday, March 11, 2015

[LLD] Design Chess Game

class Point {
    private:
        int x, y;
    public:
        Point(int i, int j); // x = i; y = j;
        int GetX();
        int GetY();
        void SetX(int i);
        void SetY(int j);
        void SetPoint(int i, int j); x = i; y = j;
}

class History {
    private:
        Board *board;
        Point from; // the point a piece was moved from
        Point to; // the point a piece was moved to
        Piece killed_piece; // remembers which piece was killed
        History *next;
        History *prev;
    public:
        History(Point f, Point t, Board* b, History *p);
        SetNext(History *n);
        void UndoLastMove();
        void RedoUndoneMove();
}

class Board {
    private:
        Piece* the_board[width][height];
        Color turn;
        History* first_history;
        History* last_history;
        Point en_passant:
    public:
        Board(); // creates board with pieces in initial configuration;  white's turn
        Piece* PieceAt(Point p); // return the piece at location p
        Piece* PieceAt(int x, int y);        // return the piece at location (x,y)
        PlacePieceAt(Piece* p, Point* pt);    // place piece p at location pt
        void Move(Point p1, Point p2);    // move piece at p1 to p2
        void Move(Point p1, Point p2, Point ep); // move piece at p1 to p2
        void TryToMove(Point p1, Point p2);
        Point GetEnPassant();
}

enum MoveType {
ILLEGAL,
NORMAL,
CASTLE,
DOUBLESTEP,
ENPASSANT
}

class Piece {
    private:
        Board* board;
        Color color;
        Point location;
    protected:
        virtual MoveType CanMove(Point p);
    public:
        Piece(Point p, Color c, Board* b);
        Point GetLocation();
        Color GetColor();
        void SetLocation(Point p);
        void SetLocation(int x, int y);
        virtual void TryToMove(Point p);
}

class Pawn : Piece {
    private:
        int direction;  // 1 for up, -1 for down
    protected:
        bool CanMove(Point p);
    public:
        Pawn(Point p, Color c, Board * b, int d);
        void TryToMove(Point p);
}

class Bishop : Piece {
    protected:
        MoveType CanMove(Point p);
}

Rook, Knight etc. we can easily think of it.

No comments:

Post a Comment