Наследование: IPiece, IModifiable
Пример #1
0
        public static PieceType GetPieceType(ChessPiece piece)
        {
            var pieceType = piece.GetType();

            if (typeof(Pawn) == pieceType)
                return PieceType.Pawn;

            if (typeof(Knight) == pieceType)
                return PieceType.Knight;

            if (typeof(Bishop) == pieceType)
                return PieceType.Bishop;

            if (typeof(Rook) == pieceType)
                return PieceType.Rook;

            if (typeof(Queen) == pieceType)
                return PieceType.Queen;

            if (typeof(King) == pieceType)
                return PieceType.King;

            return PieceType.Empty;
        }
Пример #2
0
        private void ValidateIsLegalMove(Move move, ChessPiece piece)
        {
            var teamName = Enum.GetName(typeof(Team), piece.Team);
            var pieceName = Enum.GetName(typeof(PieceType), piece.PieceType);

            var orderedMoves = Game.Moves.OrderBy(i => i.MoveId);

            if (!piece.IsLegalMove(_board.Squares, move, orderedMoves))
                throw new Exception("This is not a legal move for a " + teamName + " " + pieceName + ".");
        }
Пример #3
0
        private static Square NewSquare(int column, int row, ChessPiece piece)
        {
            if (piece != null)
            {
                piece.CurrentColumn = column;
                piece.CurrentRow = row;
                piece.Alive = true;
            }

            return new Square { Column = column, Row = row, ChessPiece = piece };
        }
Пример #4
0
        private bool PiecesCanCheckmate(ChessPiece[] pieces)
        {
            var kings = pieces.Count(p => p.PieceType == PieceType.King);
            var queens = pieces.Count(p => p.PieceType == PieceType.Queen);
            var rooks = pieces.Count(p => p.PieceType == PieceType.Rook);
            var bishops = pieces.Count(p => p.PieceType == PieceType.Bishop);
            var knights = pieces.Count(p => p.PieceType == PieceType.Knight);
            var pawns = pieces.Count(p => p.PieceType == PieceType.Pawn);

            if (kings < 1)
                throw new Exception("Your king is gone! Please report this error to an admin.");
            if (pawns > 0 || queens > 0 || rooks > 0)
                return true;
            if (bishops > 1 || knights > 1) //Forcing a checkmate with 2 knights is near impossible
                return true;
            if (bishops > 0 && knights > 0)
                return true;

            return false;
        }
Пример #5
0
        private void ValidateIsCurrentTeam(ChessPiece piece)
        {
            var teamName = Enum.GetName(typeof(Team), piece.Team);

            if (piece.Team != TeamToMove())
                throw new Exception("It is not this " + teamName + "'s turn.");
        }
Пример #6
0
        private void PerformMove(Move move, ChessPiece defender, ChessPiece piece)
        {
            if (FitsEnPassantCriteria(move, defender, piece))
            {
                PerformEnPassant(move);
            }

            if (FitsCastleCriteria(move, piece))
            {
                MoveRookForCastle(move);
            }

            if (defender != null)
            {
                if (piece.Team == Team.Dark)
                    Game.DarkScore += defender.ScoreValue;
                if (piece.Team == Team.Light)
                    Game.LightScore += defender.ScoreValue;
            }

            piece.Move(_board.Squares, move);

            IncrementMoveData(move);
        }
Пример #7
0
 private void MarkGameProgress(ChessPiece piece, ChessPiece defender)
 {
     if (piece.PieceType == PieceType.Pawn || defender != null)
         Game.MoveCountSinceProgress = 0;
 }
Пример #8
0
 private static bool FitsEnPassantCriteria(Move move, ChessPiece defender, ChessPiece piece)
 {
     return defender == null && piece.PieceType == PieceType.Pawn && Math.Abs(move.ColumnChange) == 1;
 }
Пример #9
0
 private static bool FitsCastleCriteria(Move move, ChessPiece piece)
 {
     return piece.PieceType == PieceType.King && Math.Abs(move.ColumnChange) > 1;
 }
Пример #10
0
 private bool IsPawnsSecondMoveInProperDirection(Move move, Entities.Move lastMove, ChessPiece piece)
 {
     return piece.PieceType == PieceType.Pawn
            && lastMove.EndRow == move.EndRow - LegalDirectionByTeam()
            && lastMove.EndColumn == move.EndColumn
            && piece.MoveCount == 1;
 }