コード例 #1
0
        // TODO: Make Functional
        public async Task <ResponseContext <IList <PuzzleSolution> > > SolvePuzzle()
        {
            var matrix = await GetMatrix();

            var wordData = await PuzzleWords.GetAllAsync(c => c.Id, 0, null);

            IList <PuzzleSolution> solutions = new List <PuzzleSolution>();

            foreach (var word in wordData.Result)
            {
                var solution = FindWord(word, matrix.Result);
                if (solution.IsSuccess)
                {
                    solutions.Add(solution);
                }
                else
                {
                    solutions.Add(new PuzzleSolution()
                    {
                        Word = word.Word, Breakdown = new List <PuzzleMatrix>()
                    });
                }
            }

            return(ResponseContext.Ok(solutions));
        }
コード例 #2
0
        public static ResponseContext <K> Return <T, K>(this ResponseContext <T> result, Func <K> func)
        {
            if (result.IsFailure)
            {
                return(result.HasException ? ResponseContext.Fail <K>(result.InnerException) : ResponseContext.Fail <K>(result.Error, result.ErrorType));
            }

            return(ResponseContext.Ok(func()));
        }
コード例 #3
0
        public static ResponseContext <K> Map <T, K>(this ResponseContext <T> response, Func <ResponseContext <T>, ResponseContext <K> > func)
        {
            if (response.IsFailure)
            {
                return(response.HasException ? ResponseContext.Fail <K>(response.InnerException) : ResponseContext.Fail <K>(response.Error, response.ErrorType));
            }

            return(ResponseContext.Ok(func(response)));
        }
コード例 #4
0
        public static async Task <ResponseContext <K> > ReturnAsync <T, K>(this Task <ResponseContext <T> > response, Func <K> func)
        {
            var @this = await response;

            if (@this.IsFailure)
            {
                return(@this.HasException ? ResponseContext.Fail <K>(@this.InnerException) : ResponseContext.Fail <K>(@this.Error, @this.ErrorType));
            }

            return(ResponseContext.Ok(func()));
        }
コード例 #5
0
        // TODO: Make Functional
        public async Task <ResponseContext <IList <PuzzleMatrix> > > GetMatrix()
        {
            var cypherData = await Cyphers.GetAllAsync(c => c.Id, 0, null);

            var solvedCyphers = new List <string>();

            foreach (var cypher in cypherData.Result)
            {
                var markov = await Cyphers.SolveMarkov(cypher.Id);

                if (markov.IsSuccess)
                {
                    solvedCyphers.Add(markov.Result);
                }
            }

            IList <PuzzleMatrix> matrix = new List <PuzzleMatrix>();
            var j = 0;

            foreach (var markov in solvedCyphers)
            {
                j++;
                var i = 0;

                foreach (var character in markov.ToCharArray())
                {
                    i++;
                    matrix.Add(new PuzzleMatrix()
                    {
                        Row = j, Column = i, Character = character
                    });
                }
            }

            return(ResponseContext.Ok(matrix));
        }
コード例 #6
0
        public async Task <ResponseContext <string> > SolveMarkov(int cypherId)
        {
            string cypher = "";

            return(await GetByIdAsync(c => c.Id == cypherId).MapAsync(cypherData =>
            {
                return ((WithId(cypherId).Schemes.GetAllReplacementRules(s => s.OrderId, 0, null).MapAsync(schemeData =>
                {
                    cypher = cypherData.CypherText;
                    var schemeLength = schemeData.Count;
                    var solved = false;

                    do
                    {
                        var i = 0;
                        var original = cypher;

                        foreach (var scheme in schemeData)
                        {
                            i++;
                            cypher = cypher.Replace(scheme.ReplacementRule.Source, scheme.ReplacementRule.Replacement);

                            if (cypher != original)
                            {
                                if (scheme.IsTermination)
                                {
                                    solved = true;
                                }

                                break;
                            }
                            else if (i == schemeLength)
                            {
                                solved = true;
                                break;
                            }
                        }
                    } while (!solved);

                    return cypher;
                })));
            })
                   .ReturnAsync(() => ResponseContext.Ok(cypher)));

            /// ****************************************************************************
            ///
            /// The Non-Functional way has the characteristic that should
            ///     any part of the process error out, the message would
            ///     NOT boil up all the way to the UI.
            ///
            /// The Functional way, on the other hand, allows for the processes
            ///     to continue their normal course down their happy paths,
            ///     but catches all failures on any of the unhappy paths
            ///     and bubbles them up to the UI effortlessly.
            ///
            /// As an exercise, comment the code above and uncomment the code bellow,
            ///     then try to make a request for Cypher #15 (which does not exist).
            ///     Check the outputs.
            ///
            /// ****************************************************************************
            #region Non-Functional Way
            //var cypherData = await GetByIdAsync(c => c.Id == cypherId);
            //var schemeData = await WithId(cypherId).Schemes.GetAllReplacementRules(s => s.OrderId, 0, null);

            //var cypher = cypherData.Result.CypherText;
            //var schemeLength = schemeData.RecordCount;
            //var solved = false;

            //do
            //{
            //    var i = 0;
            //    var original = cypher;

            //    foreach (var scheme in schemeData.Result)
            //    {
            //        i++;
            //        cypher = cypher.Replace(scheme.ReplacementRule.Source, scheme.ReplacementRule.Replacement);

            //        if (cypher != original)
            //        {
            //            if (scheme.IsTermination)
            //                solved = true;

            //            break;
            //        }
            //        else if (i == schemeLength)
            //        {
            //            solved = true;
            //            break;
            //        }
            //    }
            //} while (!solved);

            //return ResponseContext.Ok(cypher);
            #endregion
        }