Problem
Design a chess game using object oriented principles.
Solution
The Chess game has the following classes
- Board
- Player
- Piece
- Square
- ChessGame
Each piece then is responsible to move itself from one square to another. Player class holds references to all pieces he owns and is also responsible for their creation (Should player create Pieces?) . Player has a method takeTurn which in turn calls a method movePiece which belongs to the piece Class which changes the location of the piece from its current location to another location.
This is how the piece will look like :
public enum PieceType { None, Pawn, Knight, Bishop, Rook, Queen, King } public enum PieceColor { White, Black } public struct Piece { public PieceType Type { get; set; } public PieceColor Color { get; set; } }
Also, square will contain the position
public class Square { public int X { get; set; } public int Y { get; set; } }
Finally we have to compose Move, board and finally game :
public class Board { private Square[][] squareSet; public Piece[][] pieceSet { get; set; } public Board Clone() { ... } } public class Move { public Square From { get; } public Square To { get; } public Piece PieceMoved { get; } public Piece PieceCaptured { get; } public PieceType Promotion { get; } public string AlgebraicNotation { get; } } public class Game { public Board Board { get; } public List<Move> Movelist { get; } public PieceType Turn { get; set; } public Square DoublePawnPush { get; set; } // Used for tracking valid en passant captures public int Halfmoves { get; set; } Player p1,p2; public bool CanWhiteCastleA { get; set; } public bool CanWhiteCastleH { get; set; } public bool CanBlackCastleA { get; set; } public bool CanBlackCastleH { get; set; } //methods private void createAndPlacePieces(){ //generate pieces using a factory method //for e.g. config[1][0] = PieceFactory("Pawn",color); } private void setTurn(color) { turn = color; currentPlayer = (turn==black)?p2 : p1; } private void Play() { while(CheckStatus!=GameOver) { changeTurn(); // calls movePiece on the Piece object } } } public interface IGameRules { // .... }
The basic idea is that Game/Board/etc simply store the state of the game. You can manipulate them to e.g. set up a position, if that's what you want. I have a class that implements my IGameRules interface that is responsible for:
- Determining what moves are valid, including castling and en passant.
- Determining if a specific move is valid.
- Determining when players are in check/checkmate/stalemate.
- Executing moves.
Game
object which they can inspect to determine which moves are valid.Note that I do not store player information on
Game
. I have a separate class Table
that is responsible for storing game metadata such as who was playing, when the game took place, etc.Here is the class diagram (courtesy) :
Thanks. Please share your views/corrections :)
References - tian runhe .
how does the game initialized and started from main method?
ReplyDeleteWhichever method you are calling to intialized, we will first call createAndPlacePieces() method where we will generate all the pieces from some factory class, position them on the board. And finally we set turn, and "white" will get the first chance. Then we will enter into the while loop, which will continue until there is checkMate or players concur draw.
DeleteHope this is helpful. Let me know if you have any other question?
Is this written by you or you have copy pasted from other links? I have seen this code in other links.
ReplyDeleteHi Anonymous, I have already given the links I have referred in references and courtesy, you can click on them and read more.
DeleteSince square has x, y coordinates why do you have 2d sqaure array and 2d piece array in Board class ?
ReplyDeleteNice catch. Square contains the position details, and we could just use `List` instead of Piece set. (apologies for such a late response)
Delete