Example #1
0
        public void BlackKingAttemptsToCastleThroughCheck_Fails()
        {
            var fen       = "r1bqk2r/ppp2ppp/8/3P4/1Q2P3/6P1/PPP2P1P/R1B1KB1R b KQkq - 0 9";
            var gameState = TestUtility.GetGameState(_gameStateService, fen);

            var isCastleThroughCheck = CastlingEngine.DetermineCastleThroughCheck(gameState, 60, 63);

            Assert.IsTrue(isCastleThroughCheck);

            var newGameStateResult = _gameStateService.MakeMove(gameState, 60, 62);

            Assert.IsFalse(newGameStateResult.Success);
        }
Example #2
0
        public void WhiteKingAttemptsToCastleThroughCheck_Fails()
        {
            var fen       = "r3k2r/pp3ppp/2pq4/7b/1Q2PB2/1P6/P1P1BPPP/R3K2R w KQkq - 3 13";
            var gameState = TestUtility.GetGameState(_gameStateService, fen);

            var isCastleThroughCheck = CastlingEngine.DetermineCastleThroughCheck(gameState, 4, 0);

            Assert.IsTrue(isCastleThroughCheck);

            var newGameStateResult = _gameStateService.MakeMove(gameState, 4, 2);

            Assert.IsFalse(newGameStateResult.Success);
        }
Example #3
0
        public void BlackKingCastles_Succeeds()
        {
            var fen       = "r1bqk2r/ppp2ppp/8/3P4/2Q1P3/6P1/PPP2P1P/R1B1KB1R b KQkq - 0 9";
            var gameState = TestUtility.GetGameState(_gameStateService, fen);

            var isCastleThroughCheck = CastlingEngine.DetermineCastleThroughCheck(gameState, 60, 63);

            Assert.IsFalse(isCastleThroughCheck);

            var newGameStateResult = _gameStateService.MakeMove(gameState, 60, 62);

            Assert.IsTrue(newGameStateResult.Success, $"Castle should have succeeded. { newGameStateResult.Message }");

            var newFEN = newGameStateResult.Result.ToString();

            Assert.AreEqual("r1bq1rk1/ppp2ppp/8/3P4/2Q1P3/6P1/PPP2P1P/R1B1KB1R w KQ - 1 10", newFEN);
        }
Example #4
0
        public void WhiteKingCastles_Succeeds()
        {
            var fen       = "r1bqk2r/ppp2ppp/8/3P4/2Q1P3/6P1/PPP2P1P/R3KB1R w KQkq - 0 9";
            var gameState = TestUtility.GetGameState(_gameStateService, fen);

            // This test is broken
            // I'm guessing that the code I wrote to fix the issue in WhiteKingIsInCheckmateAndHasNoValidMoves
            // is making pieces seem like they are attacking the king when they are not
            var isCastleThroughCheck = CastlingEngine.DetermineCastleThroughCheck(gameState, 4, 0);

            Assert.IsFalse(isCastleThroughCheck);

            var newGameStateResult = _gameStateService.MakeMove(gameState, 4, 2);

            Assert.IsTrue(newGameStateResult.Success);

            var newFEN = newGameStateResult.Result.ToString();

            Assert.AreEqual("r1bqk2r/ppp2ppp/8/3P4/2Q1P3/6P1/PPP2P1P/2KR1B1R b kq - 1 9", newFEN);
        }
Example #5
0
        //	var friendlyAttacks = (activeColor == Color.White ? whiteAttacks : blackAttacks);
        //	var opponentAttacks = (activeColor == Color.White ? blackAttacks : whiteAttacks);
        public OperationResult <bool> IsValidCastleAttempt(GameState gameState, Square square, int destination, IEnumerable <AttackedSquare> attackedSquares)
        {
            var piece = square.Piece;

            var isCastleAttempt =
                (square.Index == 4 || square.Index == 60) &&
                piece.PieceType == PieceType.King &&
                Math.Abs(square.Index - destination) == 2;

            //if not a castle, then no validation needed.
            if (!isCastleAttempt)
            {
                return(OperationResult <bool> .Ok(isCastleAttempt));
            }

            var castleInfo = checkCastleAvailability(gameState, destination, piece);

            if (!castleInfo.CastleAvailability)
            {
                return(OperationResult <bool> .Fail("Castling is not available."));
            }

            //validate the move
            if (gameState.StateInfo.IsCheck)
            {
                return(OperationResult <bool> .Fail("Can't castle out of check."));
            }

            var castleThroughCheck = CastlingEngine.DetermineCastleThroughCheck(gameState, square.Index, castleInfo.RookPosition);

            if (castleThroughCheck)
            {
                return(OperationResult <bool> .Fail("Can't castle through check."));
            }

            return(OperationResult <bool> .Ok(isCastleAttempt));
        }