Exemple #1
0
        public override ReadOnlyCollection <MoreDetailedMove> GetValidMoves(Position from, bool returnIfAny, ChessGame game, Func <Move, bool> gameMoveValidator)
        {
            List <MoreDetailedMove> validMoves = new List <MoreDetailedMove>();
            Piece piece = game.GetPieceAt(from);
            int   l0    = game.BoardHeight;
            int   l1    = game.BoardWidth;

            int[][] directions = new int[][] { new int[] { 2, 1 }, new int[] { -2, -1 }, new int[] { 1, 2 }, new int[] { -1, -2 },
                                               new int[] { 1, -2 }, new int[] { -1, 2 }, new int[] { 2, -1 }, new int[] { -2, 1 } };
            foreach (int[] dir in directions)
            {
                if ((int)from.File + dir[0] < 0 || (int)from.File + dir[0] >= l1 ||
                    from.Rank + dir[1] < 1 || from.Rank + dir[1] > l0)
                {
                    continue;
                }
                MoreDetailedMove move = new MoreDetailedMove(from, new Position(from.File + dir[0], from.Rank + dir[1]), piece.Owner, null, piece, false, CastlingType.None, null, false, false, false);
                if (gameMoveValidator(move))
                {
                    move.Promotion = null;
                    move.Castling  = CastlingType.None;
                    game.WouldBeInCheckOrCheckmatedAfter(move, ChessUtilities.GetOpponentOf(move.Player), out bool inCheck, out bool checkmated);
                    move.IsChecking     = inCheck;
                    move.IsCheckmate    = checkmated;
                    move.AssociatedGame = game;

                    validMoves.Add(move);
                    if (returnIfAny)
                    {
                        return(new ReadOnlyCollection <MoreDetailedMove>(validMoves));
                    }
                }
            }
            return(new ReadOnlyCollection <MoreDetailedMove>(validMoves));
        }
Exemple #2
0
        public override ReadOnlyCollection <MoreDetailedMove> GetValidMoves(Position from, bool returnIfAny, ChessGame game, Func <Move, bool> gameMoveValidator)
        {
            ChessUtilities.ThrowIfNull(from, "from");
            List <MoreDetailedMove> validMoves = new List <MoreDetailedMove>();
            Piece piece = game.GetPieceAt(from);
            int   l0    = game.BoardHeight;
            int   l1    = game.BoardWidth;

            int[][] directions;
            if (piece.Owner == Player.White)
            {
                directions = new int[][] { new int[] { 0, 1 }, new int[] { 0, 2 }, new int[] { 1, 1 }, new int[] { -1, 1 } };
            }
            else
            {
                directions = new int[][] { new int[] { 0, -1 }, new int[] { 0, -2 }, new int[] { -1, -1 }, new int[] { 1, -1 } };
            }
            foreach (int[] dir in directions)
            {
                if ((int)from.File + dir[0] < 0 || (int)from.File + dir[0] >= l1 ||
                    from.Rank + dir[1] < 1 || from.Rank + dir[1] > l0)
                {
                    continue;
                }
                Move        move  = new Move(from, new Position(from.File + dir[0], from.Rank + dir[1]), piece.Owner);
                List <Move> moves = new List <Move>();
                if ((move.NewPosition.Rank == 8 && move.Player == Player.White) || (move.NewPosition.Rank == 1 && move.Player == Player.Black))
                {
                    foreach (char pieceChar in ValidPromotionPieces.Where(x => char.IsUpper(x)))
                    {
                        moves.Add(new Move(move.OriginalPosition, move.NewPosition, move.Player, pieceChar));
                    }
                }
                else
                {
                    moves.Add(move);
                }
                foreach (Move m in moves)
                {
                    MoreDetailedMove mm = new MoreDetailedMove(m, piece, false, CastlingType.None, null, false, false, false);
                    if (gameMoveValidator(mm))
                    {
                        mm.Castling = CastlingType.None;
                        game.WouldBeInCheckOrCheckmatedAfter(mm, ChessUtilities.GetOpponentOf(move.Player), out bool inCheck, out bool checkmated);
                        mm.IsChecking     = inCheck;
                        mm.IsCheckmate    = checkmated;
                        mm.AssociatedGame = game;

                        validMoves.Add(mm);
                        if (returnIfAny)
                        {
                            return(new ReadOnlyCollection <MoreDetailedMove>(validMoves));
                        }
                    }
                }
            }
            return(new ReadOnlyCollection <MoreDetailedMove>(validMoves));
        }
Exemple #3
0
        public override ReadOnlyCollection <MoreDetailedMove> GetValidMoves(Position from, bool returnIfAny, ChessGame game, Func <Move, bool> gameMoveValidator)
        {
            List <MoreDetailedMove> validMoves = new List <MoreDetailedMove>();
            Piece piece = game.GetPieceAt(from);
            int   l0    = game.BoardHeight;
            int   l1    = game.BoardWidth;

            for (int i = -7; i < 8; i++)
            {
                if (i == 0)
                {
                    continue;
                }
                if (from.Rank + i > 0 && from.Rank + i <= l0 &&
                    (int)from.File + i > -1 && (int)from.File + i < l1)
                {
                    MoreDetailedMove move = new MoreDetailedMove(from, new Position(from.File + i, from.Rank + i), piece.Owner, null, piece, false, CastlingType.None, null, false, false, false);
                    if (gameMoveValidator(move))
                    {
                        move.Promotion = null;
                        move.Piece     = piece;
                        move.Castling  = CastlingType.None;
                        game.WouldBeInCheckOrCheckmatedAfter(move, ChessUtilities.GetOpponentOf(move.Player), out bool inCheck, out bool checkmated);
                        move.IsChecking     = inCheck;
                        move.IsCheckmate    = checkmated;
                        move.AssociatedGame = game;

                        validMoves.Add(move);
                        if (returnIfAny)
                        {
                            return(new ReadOnlyCollection <MoreDetailedMove>(validMoves));
                        }
                    }
                }
                if (from.Rank - i > 0 && from.Rank - i <= l0 &&
                    (int)from.File + i > -1 && (int)from.File + i < l1)
                {
                    MoreDetailedMove move = new MoreDetailedMove(from, new Position(from.File + i, from.Rank - i), piece.Owner, null, piece, false, CastlingType.None, null, false, false, false);
                    if (gameMoveValidator(move))
                    {
                        move.Promotion = null;
                        move.Piece     = piece;
                        move.Castling  = CastlingType.None;
                        game.WouldBeInCheckOrCheckmatedAfter(move, ChessUtilities.GetOpponentOf(move.Player), out bool inCheck, out bool checkmated);
                        move.IsChecking     = inCheck;
                        move.IsCheckmate    = checkmated;
                        move.AssociatedGame = game;

                        validMoves.Add(move);
                        if (returnIfAny)
                        {
                            return(new ReadOnlyCollection <MoreDetailedMove>(validMoves));
                        }
                    }
                }
            }
            return(new ReadOnlyCollection <MoreDetailedMove>(validMoves));
        }
Exemple #4
0
        public override ReadOnlyCollection <MoreDetailedMove> GetValidMoves(Position from, bool returnIfAny, ChessGame game, Func <Move, bool> gameMoveValidator)
        {
            ChessUtilities.ThrowIfNull(from, "from");
            List <MoreDetailedMove> validMoves = new List <MoreDetailedMove>();
            Piece        piece      = game.GetPieceAt(from);
            int          l0         = game.BoardHeight;
            int          l1         = game.BoardWidth;
            List <int[]> directions = new List <int[]>()
            {
                new int[] { 0, 1 }, new int[] { 1, 0 }, new int[] { 0, -1 }, new int[] { -1, 0 },
                new int[] { 1, 1 }, new int[] { 1, -1 }, new int[] { -1, 1 }, new int[] { -1, -1 }
            };

            if (piece.Owner == Player.White && game.InitialWhiteKingFile == File.E && from.File == game.InitialWhiteKingFile && from.Rank == 1)
            {
                if (game.InitialWhiteRookFileKingsideCastling == File.H)
                {
                    directions.Add(new int[] { 2, 0 });
                }
                if (game.InitialWhiteRookFileQueensideCastling == File.A)
                {
                    directions.Add(new int[] { -2, 0 });
                }
            }
            if (piece.Owner == Player.Black && game.InitialBlackKingFile == File.E && from.File == game.InitialBlackKingFile && from.Rank == 8)
            {
                if (game.InitialBlackRookFileKingsideCastling == File.H)
                {
                    directions.Add(new int[] { 2, 0 });
                }
                if (game.InitialBlackRookFileQueensideCastling == File.A)
                {
                    directions.Add(new int[] { -2, 0 });
                }
            }
            if ((piece.Owner == Player.White ? game.InitialWhiteKingFile : game.InitialBlackKingFile) == from.File && from.Rank == (piece.Owner == Player.White ? 1 : 8))
            {
                if (piece.Owner == Player.White)
                {
                    int d1 = game.InitialWhiteRookFileKingsideCastling - from.File;
                    int d2 = game.InitialWhiteRookFileQueensideCastling - from.File;
                    if (Math.Abs(d1) != 1)
                    {
                        directions.Add(new int[] { d1, 0 });
                    }
                    if (Math.Abs(d2) != 1)
                    {
                        directions.Add(new int[] { d2, 0 });
                    }
                }
                else
                {
                    int d1 = game.InitialBlackRookFileKingsideCastling - from.File;
                    int d2 = game.InitialBlackRookFileQueensideCastling - from.File;
                    if (Math.Abs(d1) != 1)
                    {
                        directions.Add(new int[] { d1, 0 });
                    }
                    if (Math.Abs(d2) != 1)
                    {
                        directions.Add(new int[] { d2, 0 });
                    }
                }
            }
            foreach (int[] dir in directions)
            {
                if ((int)from.File + dir[0] < 0 || (int)from.File + dir[0] >= l1 ||
                    from.Rank + dir[1] < 1 || from.Rank + dir[1] > l0)
                {
                    continue;
                }
                MoreDetailedMove move = new MoreDetailedMove(from, new Position(from.File + dir[0], from.Rank + dir[1]), piece.Owner, null, piece, false, CastlingType.None, null, false, false, false);
                if (gameMoveValidator(move))
                {
                    move.Promotion = null;
                    game.WouldBeInCheckOrCheckmatedAfter(move, ChessUtilities.GetOpponentOf(move.Player), out bool inCheck, out bool checkmated);
                    move.IsChecking     = inCheck;
                    move.IsCheckmate    = checkmated;
                    move.AssociatedGame = game;

                    validMoves.Add(move);
                    if (returnIfAny)
                    {
                        return(new ReadOnlyCollection <MoreDetailedMove>(validMoves));
                    }
                }
            }
            return(new ReadOnlyCollection <MoreDetailedMove>(validMoves));
        }