Пример #1
0
        public void IsLegalMoveDiagonally_IsFalse_Test_001()
        {
            // Arrange diagonal move  where occupied by own
            GameController gc = new GameController();

            Location   origin           = gc.GetLocation("A7");
            Location   destination      = gc.GetLocation("B6");
            int        playerIndex      = 1;
            int        otherPlayerIndex = gc.GetOtherPlayerIndex(playerIndex);
            ChessPiece piece            = gc.Players[playerIndex].Pieces
                                          .SingleOrDefault(p => p.CurrentLocation.Coordinate == "A7");
            ChessPiece otherPiece = gc.Players[playerIndex].Pieces
                                    .SingleOrDefault(p => p.CurrentLocation.Coordinate == "B7");

            // Fake previous moved that places pieces in desired locations.
            piece.CurrentLocation      = origin;
            otherPiece.CurrentLocation = destination;

            // Act
            bool isLegalMoveDiagonally =
                MoveAssistant.IsLegalMoveDiagonally(
                    gc.Players[playerIndex], gc.Players[otherPlayerIndex], origin, destination, 1, false);

            Assert.IsFalse(isLegalMoveDiagonally);
        }
Пример #2
0
        public void IsLegalMoveAcrossColumns_IsFalse_Test_002()
        {
            // Arrange IsFalse because own piece encountered
            GameController gc = new GameController();

            Location   origin           = gc.GetLocation("A5");
            Location   destination      = gc.GetLocation("H5");
            int        playerIndex      = 1;
            int        otherPlayerIndex = gc.GetOtherPlayerIndex(playerIndex);
            ChessPiece piece            = gc.Players[playerIndex].Pieces
                                          .SingleOrDefault(p => p.CurrentLocation.Coordinate == "A8");
            ChessPiece secondPiece = gc.Players[playerIndex].Pieces
                                     .SingleOrDefault(p => p.CurrentLocation.Coordinate == "H8");

            // Fake previous moves that places pieces in desired locations.
            piece.CurrentLocation       = origin;
            secondPiece.CurrentLocation = gc.GetLocation("B5");

            // Act
            bool isLegalMoveAcrossColumns =
                MoveAssistant.IsLegalMoveAcrossColumns(
                    gc.Players[playerIndex], gc.Players[otherPlayerIndex], origin, destination);

            // Assert
            Assert.IsFalse(isLegalMoveAcrossColumns);
        }
Пример #3
0
        public void IsLegalMoveAcrossColumns_IsTrue_Test_002()
        {
            // Arrange across columns with capture
            GameController gc = new GameController();

            Location   origin           = gc.GetLocation("A5");
            Location   destination      = gc.GetLocation("H5");
            int        playerIndex      = 1;
            int        otherPlayerIndex = gc.GetOtherPlayerIndex(playerIndex);
            ChessPiece piece            = gc.Players[playerIndex].Pieces
                                          .SingleOrDefault(p => p.CurrentLocation.Coordinate == "A8");
            ChessPiece opponentPiece = gc.Players[otherPlayerIndex].Pieces
                                       .SingleOrDefault(p => p.CurrentLocation.Coordinate == "A1");

            // Fake previous moves that places pieces in desired locations.
            piece.CurrentLocation         = origin;
            opponentPiece.CurrentLocation = destination;

            // Act
            bool isLegalMoveAcrossColumns =
                MoveAssistant.IsLegalMoveAcrossColumns(
                    gc.Players[playerIndex], gc.Players[otherPlayerIndex], origin, destination);

            // Assert
            Assert.IsTrue(isLegalMoveAcrossColumns);
        }
Пример #4
0
        public void IsLegalMoveThroughRows_IsFalse_Test_004()
        {
            // Arrange decremented IsFalse because opponent piece encountered
            GameController gc = new GameController();

            Location   origin           = gc.GetLocation("A5");
            Location   destination      = gc.GetLocation("A1");
            int        playerIndex      = 1;
            int        otherPlayerIndex = gc.GetOtherPlayerIndex(playerIndex);
            ChessPiece piece            = gc.Players[playerIndex].Pieces
                                          .SingleOrDefault(p => p.CurrentLocation.Coordinate == "A8");
            ChessPiece opponentPiece = gc.Players[otherPlayerIndex].Pieces
                                       .SingleOrDefault(p => p.CurrentLocation.Coordinate == "A1");

            // Fake previous moves that places pieces in desired locations.
            piece.CurrentLocation         = origin;
            opponentPiece.CurrentLocation = gc.GetLocation("A4");

            // Act
            bool isLegalMoveThroughRows =
                MoveAssistant.IsLegalMoveThroughRows(
                    gc.Players[playerIndex], gc.Players[otherPlayerIndex], origin, destination);

            // Assert
            Assert.IsFalse(isLegalMoveThroughRows);
        }
Пример #5
0
        public override bool DidMove(
            Player player, Player opponent, Location origin, Location destination)
        {
            bool isLegalMoveDiagonally = MoveAssistant.IsLegalMoveDiagonally(
                player, opponent, origin, destination, 7, false);

            return(isLegalMoveDiagonally);
        }
Пример #6
0
        public override bool DidMove(
            Player player, Player opponent, Location origin, Location destination)
        {
            // Can move through L-Shaped Knight's Move
            bool isLegalMoveForKnight =
                MoveAssistant.IsLegalMoveForKnight(player, opponent, origin, destination, 2);

            if (isLegalMoveForKnight)
            {
                return(true);
            }
            else
            {
                return(false);
            }
        }
Пример #7
0
        // ToDo: Handle promotion of pawns
        public override bool DidMove(
            Player player, Player opponent, Location origin, Location destination)
        {
            // Can move up to 2 rows toward opponent on 1st move
            bool isInitialMove = false;
            var  piece         = player.Pieces.Find(p => p.CurrentLocation == origin);

            if (piece.PreviousLocation == null)
            {
                isInitialMove = true;
            }

            int limit = 1;

            if (isInitialMove)
            {
                limit = 2;
            }

            bool isLegalMoveThroughRows =
                MoveAssistant.IsLegalMoveThroughRows(player, opponent, origin, destination, limit, false);

            // If is capture, can move 1 square diagonally toward opponent
            // ToDo: Handle case of en passant capture!!!
            bool isCapture             = MoveAssistant.IsCapture(opponent, destination);
            bool isLegalMoveDiagonally = false;

            if (isCapture)
            {
                isLegalMoveDiagonally =
                    MoveAssistant.IsLegalMoveDiagonally(player, opponent, origin, destination, 1, false);
            }

            // If legal through rows or diagonally, return True
            if (isLegalMoveThroughRows || isLegalMoveDiagonally)
            {
                return(true);
            }
            else
            {
                return(false);
            }
        }
Пример #8
0
        /// <summary>
        /// Tries to execute the move.
        /// </summary>
        /// <param name="playerIndex">Index of player making the move.</param>
        /// <param name="origin">The origin Location</param>
        /// <param name="destination">The destination Location</param>
        /// <param name="message">Either an empty string or a message containing UI feedback.</param>
        /// <returns>True, if successful; else false</returns>
        internal bool DidExecuteMove(
            int playerIndex, Location origin, Location destination, out string message)
        {
            int otherPlayerIndex = GetOtherPlayerIndex(playerIndex);

            Player     player   = Players[playerIndex];
            Player     opponent = Players[otherPlayerIndex];
            ChessPiece piece    = Players[playerIndex].Pieces
                                  .SingleOrDefault(p => p.CurrentLocation.Coordinate == origin.Coordinate);

            if (DidMovePiece(player, opponent, origin, destination, piece))
            {
                // Update state of captured piece
                bool isCaptured = MoveAssistant.IsCapture(opponent, destination);
                if (isCaptured)
                {
                    ChessPiece capturedPiece =
                        opponent.Pieces.SingleOrDefault(p => p.CurrentLocation == destination);
                    capturedPiece.PreviousLocation = capturedPiece.CurrentLocation;
                    capturedPiece.CurrentLocation  = null;
                    capturedPiece.IsCaptured       = true;

                    if (player.CapturedPieces == null)
                    {
                        player.CapturedPieces = new List <ChessPiece>();
                    }
                    player.CapturedPieces.Add(capturedPiece);
                    opponent.Pieces.Remove(capturedPiece);
                }

                piece.PreviousLocation = piece.CurrentLocation;
                piece.CurrentLocation  = destination;

                message = $"Moved {piece.Text} from {piece.PreviousLocation.Coordinate} to {piece.CurrentLocation.Coordinate}";
                return(true);
            }
            else
            {
                message = "Your move is not legal. Please try a different move.";
            }

            return(false);
        }
Пример #9
0
        public override bool DidMove(
            Player player, Player opponent, Location origin, Location destination)
        {
            bool isLegalMoveThroughRows =
                MoveAssistant.IsLegalMoveThroughRows(player, opponent, origin, destination);

            bool isLegalMoveAcrossColumns =
                MoveAssistant.IsLegalMoveAcrossColumns(player, opponent, origin, destination);

            // ToDo: Handle special case of Castling

            // If legal through rows or across columns, return True
            if (isLegalMoveThroughRows || isLegalMoveAcrossColumns)
            {
                return(true);
            }
            else
            {
                return(false);
            }
        }
Пример #10
0
        public void IsLegalMoveThroughRows_IsTrue_Test_001()
        {
            // Arrange through rows
            GameController gc = new GameController();

            Location   origin           = gc.GetLocation("A5");
            Location   destination      = gc.GetLocation("A1");
            int        playerIndex      = 1;
            int        otherPlayerIndex = gc.GetOtherPlayerIndex(playerIndex);
            ChessPiece piece            = gc.Players[playerIndex].Pieces
                                          .SingleOrDefault(p => p.CurrentLocation.Coordinate == "A8");

            // Fake a previous move that places piece in desired location.
            piece.CurrentLocation = origin;

            // Act
            bool isLegalMoveThroughRows =
                MoveAssistant.IsLegalMoveThroughRows(
                    gc.Players[playerIndex], gc.Players[otherPlayerIndex], origin, destination);

            // Assert
            Assert.IsTrue(isLegalMoveThroughRows);
        }
Пример #11
0
        public void IsLegalMoveThroughRows_IsFalse_Test_006()
        {
            // Arrange IsFalse because exceeded limit for subsequent moves of pawn
            GameController gc = new GameController();

            Location   origin           = gc.GetLocation("A5");
            Location   destination      = gc.GetLocation("A3");
            int        playerIndex      = 1;
            int        otherPlayerIndex = gc.GetOtherPlayerIndex(playerIndex);
            ChessPiece piece            = gc.Players[playerIndex].Pieces
                                          .SingleOrDefault(p => p.CurrentLocation.Coordinate == "A7");

            // Fake previous moves that places pieces in desired locations.
            piece.PreviousLocation = gc.GetLocation("A7");
            piece.CurrentLocation  = origin;

            // Act
            bool isLegalMoveThroughRows =
                MoveAssistant.IsLegalMoveThroughRows(
                    gc.Players[playerIndex], gc.Players[otherPlayerIndex], origin, destination, 1, false);

            // Assert
            Assert.IsFalse(isLegalMoveThroughRows);
        }