Ejemplo n.º 1
0
        public void UnMovePiece(Move move)
        {
            Position to   = move.To;
            Position from = move.From;

            // Pawn demotion
            if (move.Promotion)
            {
                Board.DemotePawn(move);
            }

            if (move.Piece is King)
            {
                King king = move.Piece as King;

                if (move.Piece.MoveCount == 1)
                {
                    if (king.Color == PieceColor.White)
                    {
                        Board.UpdatePieceEnum(king, PieceEnum.WhiteKing);
                    }
                    else if (king.Color == PieceColor.Black)
                    {
                        Board.UpdatePieceEnum(king, PieceEnum.BlackKing);
                    }
                }

                if (move.Castle)
                {
                    Board.MovePiece(move.CastleMove.To, move.CastleMove.From);
                    move.CastleMove.Piece.MoveCount--;
                }
            }

            Board.MovePiece(to, from);
            move.Piece.MoveCount--;

            if (!(move.Capture is Empty))
            {
                Board.AddPiece(move.Capture.Position.Row, move.Capture.Position.Col, move.Capture);
            }

            if (progress.Count > 0)
            {
                progress.Pop();
            }
            if (moves.Count > 0)
            {
                moves.Pop();
            }
            if (prevHashes.Count > 0)
            {
                prevHashes.Pop();
            }

            if (CurrentTurn == PieceColor.White)
            {
                CurrentTurn = PieceColor.Black;
                if (moves.Count > 0 && moves.Peek().EnPassantMove)
                {
                    Board.UpdatePieceEnum(moves.Peek().Piece, PieceEnum.WhitePawnEnPassant);
                }
            }
            else
            {
                CurrentTurn = PieceColor.White;
                if (moves.Count > 0 && moves.Peek().EnPassantMove)
                {
                    Board.UpdatePieceEnum(moves.Peek().Piece, PieceEnum.BlackPawnEnPassant);
                }
            }
        }
Ejemplo n.º 2
0
        internal void MovePiece(Move move)
        {
            Position to   = move.To;
            Position from = move.From;

            if (move.Capture is King)
            {
                Debug.WriteLine("Not supposed to happen!");
            }

            // Pawn promotion
            if (move.Promotion)
            {
                Board.PromotePawn(move);
            }

            if (move.Piece is Pawn)
            {
                Pawn pawn = move.Piece as Pawn;

                if (move.EnPassantMove && pawn.Color == PieceColor.White)
                {
                    Board.UpdatePieceEnum(pawn, PieceEnum.WhitePawnEnPassant);
                }
                else if (move.EnPassantMove && pawn.Color == PieceColor.Black)
                {
                    Board.UpdatePieceEnum(pawn, PieceEnum.BlackPawnEnPassant);
                }
            }

            if (move.Piece is King)
            {
                King king = move.Piece as King;

                if (move.Castle)
                {
                    Board.MovePiece(move.CastleMove.From, move.CastleMove.To);
                    move.CastleMove.Piece.MoveCount++;
                }

                if (king.Color == PieceColor.White)
                {
                    Board.UpdatePieceEnum(king, PieceEnum.WhiteKingNoCastle);
                }
                else if (king.Color == PieceColor.Black)
                {
                    Board.UpdatePieceEnum(king, PieceEnum.BlackKingNoCastle);
                }
            }

            if (!(move.Piece is Pawn) || move.Capture is Empty)
            {
                if (progress.Count > 0)
                {
                    progress.Push(progress.Peek() + 1);
                }
                else
                {
                    progress.Push(1);
                }
            }
            else
            {
                progress.Push(0);
            }

            if (!(move.Capture is Empty))
            {
                Board.RemovePiece(move.Capture.Position.Row, move.Capture.Position.Col);
            }

            Board.MovePiece(from, to);
            move.Piece.MoveCount++;

            moves.Push(move);
            prevHashes.Push(Board.Hash);

            if (CurrentTurn == PieceColor.White)
            {
                CurrentTurn = PieceColor.Black;
                ClearPassant(PieceColor.Black);
            }
            else
            {
                CurrentTurn = PieceColor.White;
                ClearPassant(PieceColor.White);
            }

            if (IsThreatened(Board.GetKing(CurrentTurn)))
            {
                move.CheckBonus = true;
            }
        }