Exemplo n.º 1
0
        private bool CanMoveIntoAttackersPath(PlayerTypeEnum player, string lastMoveNotation, ChessBoard board, List<string> attackers, Tuple<int, int> opponentKingTuple)
        {
            if (attackers.Count > 1)
                return false;

            bool canBlockPathOfAttackers = false;

            string attackerNotation = attackers.First();
            Tuple<int, int> attackerTuple = NotationHelper.Translate(attackerNotation);
            ChessPiece attackerPiece = board.Get(attackerTuple);

            if (attackerPiece.Type == PieceTypeEnum.Knight)
                return false;

            // get star pattern of each attacker
            List<Tuple<int, int>[]> relativePaths = new QueenRelativePathProvider().GetRelativePaths(
                new ChessPiece() { Player = player });

            var absolutePaths = BoardLogic.ConvertToAbsolutePaths(attackerTuple, relativePaths);

            // get path that leads to king
            var pathOfAttacker = BoardLogic.GetPathContaining(opponentKingTuple, absolutePaths);
            var trimmedPathOfAttacker = BoardLogic.TrimToBeforeDestination(opponentKingTuple, pathOfAttacker);

            // can any opponents move into path and block?
            var opponentPlayerSquares = board.GetPiecesForPlayer(GetOppositePlayer(player));
            foreach (var opponentSquare in opponentPlayerSquares)
            {
                bool canMoveIntoAttackersPath =
                    trimmedPathOfAttacker.Any(location =>
                    {
                        string dest = NotationHelper.Translate(location);
                        return CanMove(board, opponentSquare + dest, GetOppositePlayer(player), lastMoveNotation).Success;
                    });

                if (canMoveIntoAttackersPath)
                {
                    canBlockPathOfAttackers = true;
                    break;
                }
            }

            return canBlockPathOfAttackers;
        }
Exemplo n.º 2
0
        private bool CanTakedownAttackers(PlayerTypeEnum player, string lastMoveNotation, ChessBoard board, List<string> attackers)
        {
            if (attackers.Count > 1)
                return false;

            bool canTakedownAttacker = false;
            PlayerTypeEnum opponentPlayerType = GetOppositePlayer(player);
            List<string> opponentSquares = board.GetPiecesForPlayer(opponentPlayerType);
            string attackerNotation = attackers.First();

            foreach (var square in opponentSquares)
            {
                bool canDefeatAllAttackers = CanMove(board, square + attackerNotation, opponentPlayerType, lastMoveNotation).Success;
                if (canDefeatAllAttackers)
                {
                    canTakedownAttacker = true;
                    break;
                }
            }

            return canTakedownAttacker;
        }
Exemplo n.º 3
0
        private Tuple<bool, List<string>> IsInCheck(PlayerTypeEnum playerType, ChessBoard board, string lastMoveNotation)
        {
            PlayerTypeEnum opponentPlayerType = GetOppositePlayer(playerType);
            List<string> opponentSquares = board.GetPiecesForPlayer(opponentPlayerType);
            string kingNotation = board.GetKing(playerType);

            if (kingNotation == null)
                return new Tuple<bool, List<string>>(false, null);

            bool isInCheck = false;
            List<string> attackers = new List<string>();
            foreach (var square in opponentSquares)
            {
                var opponentMoveResult = CanMove(board, square, kingNotation, opponentPlayerType, lastMoveNotation, false);
                if (opponentMoveResult.Success)
                {
                    isInCheck = true;
                    attackers.Add(square);
                }
            }

            return new Tuple<bool, List<string>>(isInCheck, attackers);
        }
Exemplo n.º 4
0
        static void RandomIntoCheck()
        {
            Random r = new Random(Guid.NewGuid().GetHashCode());

            ChessBoard chessBoard = new ChessBoard();
            (new TraditionalBoardStager()).Stage(chessBoard);

            PlayerTypeEnum currentPlayer = PlayerTypeEnum.White;
            ChessGame chessGame = new ChessGame(
                chessBoard,
                currentPlayer,
                new Dictionary<PlayerTypeEnum, List<ChessPiece>>());

            bool isCheckmate = false;
            while (true)
            {
                int movesWithoutSuccess = 0;

                while (true)
                {
                    List<string> notation = chessBoard.GetPiecesForPlayer(currentPlayer);
                    List<string> opponentNotation =
                        chessBoard.GetPiecesForPlayer(currentPlayer == PlayerTypeEnum.White
                            ? PlayerTypeEnum.Black
                            : PlayerTypeEnum.White);

                    if ((notation.Count == 1 && opponentNotation.Count == 1) || movesWithoutSuccess > 5000)
                    {
                        //Console.WriteLine("{0} on {1}, moves w/o success: {2}", notation.Count, notation.Count, movesWithoutSuccess);

                        chessBoard = new ChessBoard();
                        (new TraditionalBoardStager()).Stage(chessBoard);

                        currentPlayer = PlayerTypeEnum.White;
                        chessGame = new ChessGame(
                            chessBoard,
                            currentPlayer,
                            new Dictionary<PlayerTypeEnum, List<ChessPiece>>());
                    }

                    int randomPieceIndex = r.Next(0, notation.Count);
                    string randomSource = notation.ElementAt(randomPieceIndex);

                    int randomRank = r.Next(0, 8);
                    int randomFile = r.Next(0, 8);

                    string randomDestination = NotationHelper.Translate(randomRank, randomFile);
                    var move = chessGame.PlayerMove(currentPlayer, randomSource + randomDestination);

                    if (move.Success)
                    {
                        movesWithoutSuccess = 0;

                        if (move.MoveType == MoveTypeEnum.Checkmate)
                        {
                            (new ConsoleBoardPrinter()).PrintBoard(chessBoard.CreateStateCopy(), currentPlayer);
                            Console.WriteLine("Check: {0}, Moves: {1}", Enum.GetName(typeof(MoveTypeEnum), move.MoveType), string.Join(", ", move.Moves));
                        }

                        currentPlayer = move.Turn;

                        if (move.MoveType == MoveTypeEnum.Checkmate)
                        {
                            isCheckmate = true;
                        }

                        break;
                    }

                    ++movesWithoutSuccess;
                }

                if (isCheckmate)
                {
                    break;
                }
            }
        }