Small library to help you to manage an entire chess game and serialize it to Json.
- Create new game with start or specific chessmen positions or with Json serialized format;
- Get acceptable cells for every chessman;
- Make movements and get correct game status after it;
- Serialize game to Json.
How does serialized game look like you can find here: StartPositions.json. Line breaks were added just for readability.
Board is two-dimensional array (8 rows and 8 columns). Board stored in BoardCells property of ChessBoard instance.
// _________________________________
// 0 | | | | | | | | |
// _________________________________
// 1 | | | | | | | | |
// _________________________________
// 2 | | | | | | | | |
// _________________________________
// 3 | | | | | | | | |
// _________________________________
// 4 | | | | | | | | |
// _________________________________
// 5 | | | | | | | | |
// _________________________________
// 6 | | | | | | | | |
// _________________________________
// 7 | | | | | | | | |
// _________________________________
//
// 0 1 2 3 4 5 6 7
Cell cell = chessboard.BoardCells[3, 5]; // cell on 3 row and 5 column
var chessBoard = new ChessBoard();
chessBoard.SetStartPosition(); // Set default start positions for all chessmen and GameStatus.WhiteTurn status.
var chessBoard = new ChessBoard(boardCells); // BoardCell[8, 8]
var chessBoard = new ChessBoard(serializedChessBoard); // string
Status is enum that can be:
- WhiteTurn,
- BlackTurn,
- ShahForWhite,
- ShahForBlack,
- WhiteWin,
- BlackWin,
- StalemateForBlack,
- StalemateForWhite,
- Draw
GameStatus status = chessboard.Status;
Also new status returned as result of MoveChessman method:
GameStatus status = chessboard.MoveChessman(oldPosition, newPosition);
string status = chessboard.GetSerializedChessBoard();
To make movement you just need to specify old and new cells:
Cell oldPosition = new Cell(3, 5);
Cell newPosition = new Cell(3, 6);
GameStatus status = chessboard.MoveChessman(oldPosition, newPosition);
Also, in case of Pawn moving to the last row, you need to specify new type for Pawn transformation (promotion):
ChessmenType? newType = ChessmenType.Queen; // Can’t be as King or Pawn
GameStatus status = chessboard.MoveChessman(oldPosition, newPosition, newType);
You can get acceptable cells for any chessman. You need to specify it’s cell:
Cell queenCell = new Cell(3, 5);
List<Cell> acceptableCells = chessboard.GetAcceptableCells(queenCell);
It will set Status as BlackWin or WhiteWin depeding on specified Color.
chessboard.GiveUp(Color.Black);
Just checking that game status is not WhiteWin, BlackWin, StalemateForWhite, StalemateForBlack or Draw;
bool isGameOver = chessboard.IsGameOver();