private ChessBoard(ChessBoard board, int?turn = null) : this() { this.IsClone = true; this.Game = board.Game; this.Turn = turn ?? board.Turn; this.HalfMovesSinceCaptureOrPawn = board.HalfMovesSinceCaptureOrPawn; this.Pieces = board.Pieces.Select(p => p.Clone(this)).ToList(); this.State = board.State; this.StateFlags = board.StateFlags; this.UpdateBoardState(); }
public void Reset() { this.Turn = 1; this.HalfMovesSinceCaptureOrPawn = 0; this.State = ChessGameState.Playing; this.StateFlags = ChessBoardStateFlags.WhiteCastleKingside | ChessBoardStateFlags.WhiteCastleQueenside | ChessBoardStateFlags.BlackCastleKingside | ChessBoardStateFlags.BlackCastleQueenside; this.Pieces.Clear(); this.ResetPlayerPieces(this.Game.Players[0].Colour, 1); this.ResetPlayerPieces(this.Game.Players[1].Colour, -1); this.UpdateBoardState(); }
public void ExecuteMove(ChessMove move) { this.ExecutingMove = null; if (!move.IsValid) { return; } if (!this.ValidateMove(move, out ChessPiece movingPiece, out ChessPiece targetPiece)) { return; } this.HalfMovesSinceCaptureOrPawn++; if (movingPiece.PieceType == ChessPieceType.Pawn) { this.HalfMovesSinceCaptureOrPawn = 0; } if (targetPiece != null) { targetPiece.Capture(); this.HalfMovesSinceCaptureOrPawn = 0; } if (movingPiece.PieceType == ChessPieceType.King) { this.StateFlags &= ~(movingPiece.Colour == ChessPlayerColour.White ? ChessBoardStateFlags.WhiteCastleKingside : ChessBoardStateFlags.BlackCastleKingside); this.StateFlags &= ~(movingPiece.Colour == ChessPlayerColour.White ? ChessBoardStateFlags.WhiteCastleQueenside : ChessBoardStateFlags.BlackCastleQueenside); } else if (movingPiece.PieceType == ChessPieceType.Rook) { if (movingPiece.Location.X == 0) { this.StateFlags &= ~(movingPiece.Colour == ChessPlayerColour.White ? ChessBoardStateFlags.WhiteCastleQueenside : ChessBoardStateFlags.BlackCastleQueenside); } else if (movingPiece.Location.X == 7) { this.StateFlags &= ~(movingPiece.Colour == ChessPlayerColour.White ? ChessBoardStateFlags.WhiteCastleKingside : ChessBoardStateFlags.BlackCastleKingside); } } movingPiece.Move(move.to); if (move.promoteTo.HasValue) { movingPiece.Promote(move.promoteTo.Value); } else if (move.castleFrom.HasValue) { this.GetPieceFromLocation(move.castleFrom.Value).Move(move.castleTo.Value); } else if (move.targetLocation.HasValue) { this.GetPieceFromLocation(move.targetLocation.Value).Capture(); this.HalfMovesSinceCaptureOrPawn = 0; } if (!this.IsClone) { this.Turn++; } this.UpdateBoardState(); }