Пример #1
0
        /**
         * This method receives a Move and returns whether it is a valid move.
         * A valid move must be a diagonal move which doesn't overlap another soldier, it must move an actual soldier, it must not exceed the board's boundaries,
         * if it's a jump move then it must be jumping over the opponent's soldier and if the move isn't a jump move but the player has jump moves available, then
         * the move isn't valid as well.
         */
        public bool IsMoveValid(Move i_Move)
        {
            bool validMove = true;
            CheckersCoordinates targetLocation  = i_Move.TargetLocation;
            CheckersCoordinates currentLocation = i_Move.CurrentLocation;
            Player movePlayer = i_Move.Player != null ? i_Move.Player : GetCurrentPlayer();

            if (!targetLocation.IsValid(m_CheckersBoard.Size) || !currentLocation.IsValid(m_CheckersBoard.Size))
            {
                validMove = false;
            }
            else
            {
                Soldier soldier = m_CheckersBoard.GetSoldierAt(currentLocation);
                Soldier targetLocationSoldier = m_CheckersBoard.GetSoldierAt(targetLocation);

                // Checking that the soldier actually exists and that we're not trying to move the soldier to an existing soldier.
                if (soldier == null || targetLocationSoldier != null)
                {
                    validMove = false;
                }
                else if (!isValidDiagonalMove(i_Move))
                {
                    validMove = false;
                }
                else if (i_Move.IsJumpMove && !IsJumpableMove(i_Move))
                {
                    validMove = false;
                }
                else if (!i_Move.IsJumpMove && PlayerHasJumpMove(movePlayer))
                {
                    validMove = false;
                }
            }

            return(validMove);
        }