Exemplo n.º 1
0
        /// <summary>
        /// Gets a randomly shuffled DNA EvoString prey.
        /// </summary>
        /// <param name="size"></param>
        /// <returns></returns>
        /// <remarks>
        /// There's a Typing Monkey inside this factory method! Cool!
        /// </remarks>
        public static IPrey CreateRandomEvoStringPrey(int size)
        {
            TypingMonkey monkey     = new TypingMonkey();
            string       chromosome = monkey.TypeAway(size);

            return(new EvoString(chromosome));
        }
Exemplo n.º 2
0
        /// <summary>
        /// In Nature there's a pseudo-random chance the newborn chromosomes will mutate...
        /// </summary>
        private void Mutate()
        {
            int marker = Dice.Roll(0, this.chromosome.Length - 1);
            int delta  = Dice.Roll(0, this.chromosome.Length - marker);

            TypingMonkey monkey       = new TypingMonkey();
            string       mutatedGenes = monkey.TypeAway(delta);

            char[] genes = this.chromosome.ToCharArray();

            // Override mutated genes.
            for (int i = 0; i < delta; i++)
            {
                genes[marker++] = mutatedGenes[i];
            }

            this.chromosome = new string(genes);
        }
Exemplo n.º 3
0
        public SaveUserFavoriteResponse SaveUserFavorite(SaveUserFavoriteRequest request)
        {
            var response = new SaveUserFavoriteResponse()
            {
                Success = false, Message = "Unable to save user favorite"
            };

            try
            {
                //Check to see if we have already created the 5 digit unique user code.
                //Not going to check to see if we get a duplicate code for this exercise
                var userCodeExists = request.WeathermanEntities.UserCodes.Any(w => w.UserId == request.UserId);

                //Will add the code if it does not exist for the logged in user.
                if (!userCodeExists)
                {
                    var userCode = new UserCode {
                        UserId = request.UserId
                    };
                    var typingMonkey = new TypingMonkey();
                    userCode.Code = typingMonkey.TypeAway(5);
                    request.WeathermanEntities.UserCodes.Add(userCode);
                }

                var savedLocation = new SavedLocation
                {
                    //Did not get a chance to implement saving the city yet.
                    City    = string.Empty,
                    ZipCode = request.PostalCode,
                    UserId  = request.UserId
                };
                request.WeathermanEntities.SavedLocations.Add(savedLocation);
                request.WeathermanEntities.SaveChanges();
            }
            catch (Exception exception)
            {
                response.Success   = false;
                response.Exception = exception;
                return(response);
            }
            response.Success = true;
            response.Message = string.Empty;
            return(response);
        }