Exemple #1
0
        public void KingAttacks3()
        {
            var positions = CastlingEngine.GetKingPositionsDuringCastle(60, 63);

            Assert.AreEqual(2, positions.Count());
            Assert.AreEqual(61, positions[0]);
            Assert.AreEqual(62, positions[1]);

            var positions2 = CastlingEngine.GetKingPositionsDuringCastle(60, 56);

            Assert.AreEqual(2, positions2.Count());
            Assert.AreEqual(59, positions2[0]);
            Assert.AreEqual(58, positions2[1]);

            var positions3 = CastlingEngine.GetKingPositionsDuringCastle(4, 7);

            Assert.AreEqual(2, positions3.Count());
            Assert.AreEqual(5, positions3[0]);
            Assert.AreEqual(6, positions3[1]);

            var positions4 = CastlingEngine.GetKingPositionsDuringCastle(4, 0);

            Assert.AreEqual(2, positions4.Count());
            Assert.AreEqual(3, positions4[0]);
            Assert.AreEqual(2, positions4[1]);
        }
Exemple #2
0
        private GameState GetNewGameState(GameState gameState, int piecePosition, StateInfo stateInfo, int newPiecePosition)
        {
            var transitionGameState = manageSquares(gameState, stateInfo, piecePosition, newPiecePosition);

            if (stateInfo.IsCastle)
            {
                var rookPositions = CastlingEngine.GetRookPositionsForCastle(gameState.ActiveColor, piecePosition, newPiecePosition);
                transitionGameState = manageSquares(transitionGameState, stateInfo, rookPositions.RookPos, rookPositions.NewRookPos);
            }
            if (stateInfo.IsEnPassant)
            {
                //remove the attacked pawn
                var enPassantAttackedPawnPosition = gameState.ActiveColor == Color.White ? newPiecePosition - 8 : newPiecePosition + 8;
                var enPassantAttackedPawnSquare   = transitionGameState.Squares.GetSquare(enPassantAttackedPawnPosition);
                enPassantAttackedPawnSquare.Piece = null;
            }
            _notationService.SetGameStateSnapshot(gameState, transitionGameState, stateInfo, piecePosition, newPiecePosition);

            var fenRecords       = gameState.History.DeepCopy();
            var previousStateFEN = gameState.ToString();

            fenRecords.Add(FenFactory.Create(previousStateFEN));
            transitionGameState.History = fenRecords;

            return(transitionGameState);
        }
Exemple #3
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);
        }
Exemple #4
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);
        }
Exemple #5
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);
        }
Exemple #6
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);
        }
Exemple #7
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));
        }