示例#1
0
        public Piece ShiftPiece(Piece piece, Square end)
        {
            var startPiece   = piece.Clone() as Piece;
            var endPiece     = Squares.FirstOrDefault(x => x.Point.Equals(end.Point))?.Piece;
            var saveEndPiece = endPiece?.Clone() as Piece;

            Squares.FirstOrDefault(x => x.Point.Equals(end.Point)).Piece        = Squares.FirstOrDefault(x => x.Point.Equals(piece.Point)).Piece;
            Squares.FirstOrDefault(x => x.Point.Equals(startPiece.Point)).Piece = null;

            return(saveEndPiece);
        }
示例#2
0
        public Piece IsKingChecked(Color turn)
        {
            var enemyPieces      = GetAllPiecesByColor(GetOtherColor(turn));
            var playerKingSquare = Squares.FirstOrDefault(x => x.Point.Equals(GetKingByColor(turn).Point));

            foreach (var enemyPiece in enemyPieces)
            {
                if (enemyPiece.CanBeMovedToSquare(playerKingSquare))
                {
                    var dir = enemyPiece.ChooseRightDirection(playerKingSquare.Point);
                    if (!IsPieceBlocking(enemyPiece, playerKingSquare, dir))
                    {
                        return(enemyPiece);
                    }
                }
            }

            return(null);
        }
示例#3
0
        public List <Square[]> CanCastle(Piece start)
        {
            List <Square[]> square = new List <Square[]>();

            if (!(start is King))
            {
                return(square);
            }

            var rooks = GetRookNearKing(start);

            foreach (var rook in rooks)
            {
                if (start.IsFirstMove && rook.IsFirstMove)
                {
                    var clone    = Clone() as Board;
                    var dir      = start.ChooseRightDirection(rook.Point);
                    var allMoves = start.Point.AllMovesWithinDirection(rook.Point, dir).Take(2);
                    if (allMoves.Count() < 2)
                    {
                        return(square);
                    }

                    Square s = null;
                    foreach (var move in allMoves)
                    {
                        var end = clone.Squares.FirstOrDefault(x => x.Point.Equals(move));
                        clone.ShiftPiece(clone.GetKingByColor(start.Color), end);
                        if (clone.IsKingChecked(start.Color) != null)
                        {
                            return(square);
                        }
                    }

                    square.Add(new Square[] { Squares.FirstOrDefault(x => x.Point.Equals(allMoves.ElementAt(1))), Squares.FirstOrDefault(x => x.Point.Equals(rook.Point)) });
                }
            }

            return(square);
        }
示例#4
0
        public List <Move> CalcPossibleMoves(Piece piece)
        {
            var res = new List <Move>();

            foreach (var dir in piece.Directions)
            {
                var allMoves = piece.Point.AllMovesWithinDirection(dir);
                foreach (var end in allMoves)
                {
                    var square = Squares.FirstOrDefault(x => x.Point.Equals(end));
                    if (piece.CanMoveWithoutColliding(square, this))
                    {
                        var clonedBoard = PredictBoard(piece, square);
                        if (clonedBoard.IsKingChecked(piece.Color) == null)
                        {
                            var add = CanCastle(piece);
                            foreach (var castle in add)
                            {
                                if (!piece.CheckCollision(castle[0], this))
                                {
                                    var cMove = new Castle(this, piece, castle[0], castle[1]);
                                    cMove.OnInitiatePawnPromotion += Move_OnInitiatePawnPromotion;
                                    cMove.OnPieceCaptured         += Move_OnPieceCaptured;
                                    res.Add(cMove);
                                }
                            }

                            var move = new Move(this, piece, square);
                            move.OnInitiatePawnPromotion += Move_OnInitiatePawnPromotion;
                            move.OnPieceCaptured         += Move_OnPieceCaptured;
                            res.Add(move);
                        }
                    }
                }
            }

            return(res);
        }
示例#5
0
        public async Task ApplyMove(BoardPosition squarePosition)
        {
            var possibleMoves = _board.GetPossibleMoves() as IEnumerable <ChessMove>;
            var selectedPiece = Squares.FirstOrDefault(s => s.IsSelected);

            if (possibleMoves == null || selectedPiece == null)
            {
                return;
            }

            foreach (var move in possibleMoves)
            {
                // Since pawns are special
                if (!move.StartPosition.Equals(selectedPiece.Position) ||
                    !move.EndPosition.Equals(squarePosition))
                {
                    continue;
                }

                // Make a move
                _board.ApplyMove(move);
                break;
            }

            if (_board.NeedToPawnPromote)
            {
                var dialog = new PawnPromotionDialog(this);
                dialog.ShowDialog();
                possibleMoves = _board.GetPossibleMoves() as IEnumerable <ChessMove>;
                _board.ApplyMove(possibleMoves?.FirstOrDefault(move => move.ToString().Contains(dialog.Piece)));
            }

            Debug.WriteLine($"have: {_board.MoveHistory.Count}");
            RebindState();
            Debug.WriteLine($"have: {_board.MoveHistory.Count}");
            Debug.WriteLine("player moved");

            if (Players == NumberOfPlayers.One && !_board.IsFinished)
            {
                Debug.WriteLine("making move");
                var test     = _board.CurrentPlayer;
                var bestMove = await Task.Run(() => _ai.FindBestMove(_board));

                Debug.WriteLine($"{test}-{_board.CurrentPlayer}");

                if (bestMove != null)
                {
                    Debug.WriteLine($"{bestMove}");
                    Debug.WriteLine("test");
                    _board.ApplyMove(bestMove);

                    if (_board.NeedToPawnPromote)
                    {
                        Debug.WriteLine("promoting pawn");
                        bestMove = await Task.Run(() => _ai.FindBestMove(_board));

                        if (bestMove != null)
                        {
                            _board.ApplyMove(bestMove);
                        }
                    }
                }
            }

            Debug.WriteLine($"have: {_board.MoveHistory.Count}");
            RebindState();
            Debug.WriteLine($"have: {_board.MoveHistory.Count}");
            Debug.WriteLine("ai moved");
        }
示例#6
0
 public Square SquareAt(int row, int col)
 {
     return(Squares.FirstOrDefault(s => s.Row == row && s.Col == col));
 }