Esempio n. 1
0
        public static void ChecknFlagEnpassant(List <Cell> cells, ChessMove move, bool undoMove = false)
        {
            //Check if Pawn that went 2 steps - (Enpassant)
            if (move.PieceMoved is Pawn && Math.Abs(move.From.ID.Y - move.To.ID.Y) == 2)
            {
                int   step       = (move.To.ID.Y - move.From.ID.Y) / 2;
                Point firstStep  = new Point(0, step);
                Cell  passedCell = cells.NextCell(move.From, firstStep);

                if (undoMove)
                {
                    passedCell.enPassantPawn = null;
                }
                else
                {
                    passedCell.enPassantPawn = move.PieceMoved;
                }

                // Set first step as Enpassant - (Link Pawn) remove next turn
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Returns list of All legal ChessMoves for a GamePiece
        /// </summary>
        /// <param name="piece">GamePiece that is being investigated</param>
        public List <ChessMove> PossibleMoves(GamePiece piece)
        {
            if (piece is null)
            {
                return(null); // illegal - Empty space
            }
            Player           activePlayer  = piece.TeamColor == PlayerOne.TeamColor ? PlayerOne : PlayerTwo;
            Cell             fromCell      = Cells.GetCell(piece.Location);
            List <ChessMove> possibleMoves = new List <ChessMove>(); // Returning List of Possible Moves
            GamePiece        king          = activePlayer.MyPieces.Find(gp => gp is King);

            //List<BlindMove> blindMoves = fromCell.Piece.BlindMoves(); // Blind move instructions for Gamepiece
            foreach (BlindMove bMove in fromCell.Piece.BlindMoves())             // Blind move instructions for Gamepiece
            {
                Cell      focusCell = Cells.NextCell(fromCell, bMove.Direction); // The Starting Point
                int       moveCount = 0;
                Condition moveType  = Condition.Neutral;

                // Increment through Instruction
                while (!(focusCell is null) && bMove.Limit != moveCount && moveType == Condition.Neutral)
                {
                    moveType = MovePossibility(fromCell.Piece, fromCell, focusCell, bMove.Condition);

                    // ADD to List of Possible ChessMoves
                    if (moveType == Condition.Neutral || moveType == Condition.Attack || moveType == Condition.enPassant || moveType == Condition.Castling)
                    {
                        ChessMove move;
                        if (moveType == Condition.enPassant) // *Special Move - Pawn captures Pawn via Enpassant
                        {
                            GamePiece captured = Cells.Find(c => !(c.enPassantPawn is null)).enPassantPawn;
                            move = new ChessMove(fromCell, focusCell, fromCell.Piece, captured, moveType, Cells.GetCell(captured.Location));
                        }
                        else if (moveType == Condition.Castling) // *Special Move - Castling, insert Rook into ChessMove
                        {
                            Rook rook       = (Rook)Cells.GetCell((Point)bMove.OtherInfo).Piece;
                            int  xDirection = bMove.Direction.X / 2;
                            Cell rookFrom   = Cells.GetCell(rook.Location);
                            Cell rookTo     = Cells.GetCell(new Point(fromCell.ID.X + xDirection, fromCell.ID.Y));

                            ChessMove rookMove = new ChessMove(rookFrom, rookTo, rook, null, Condition.Castling);

                            move = new ChessMove(fromCell, focusCell, fromCell.Piece, null, moveType, rookMove);
                        }
                        else // Regular Move
                        {
                            move = new ChessMove(fromCell, focusCell, fromCell.Piece, focusCell.Piece, moveType);
                        }

                        //Look in the future
                        move = MovePiece(move, true);
                        bool isKingSafe = IsSafe(king.Location, activePlayer);
                        move = UndoMovePiece(move, true);

                        if (isKingSafe)
                        {
                            possibleMoves.Add(move);
                        }
                    }

                    focusCell = Cells.NextCell(focusCell, bMove.Direction);
                    moveCount++;
                }
            }

            return(possibleMoves);
        }