示例#1
0
        /// <summary>
        /// Calculate the solution to the game
        /// </summary>
        private static Solutions CalculateSolution(DownCountGame game)
        {
            Solutions solutions = DownCountSolver.Solve(game, false);

            if (solutions.Count == 0)
            {
                //Look for a 'close' soltion
                DownCountGame closeGame = new DownCountGame(game.TargetNumber, game.Numbers.ToArray());;
                for (int i = 1; i <= 10; ++i)
                {
                    closeGame.TargetNumber = game.TargetNumber - i;
                    solutions = DownCountSolver.Solve(closeGame, false);
                    if (solutions.Count > 0)
                    {
                        return(solutions);
                    }

                    closeGame.TargetNumber = game.TargetNumber + i;
                    solutions = DownCountSolver.Solve(closeGame, false);
                    if (solutions.Count > 0)
                    {
                        return(solutions);
                    }
                }
            }

            return(solutions);
        }
示例#2
0
        public void DownCountSolver_NoSolution()
        {
            DownCountGame game      = new DownCountGame(577, 9, 3, 4, 2, 1);
            Solutions     solutions = DownCountSolver.Solve(game);

            Assert.IsTrue(solutions.Count == 0);
        }
        public IActionResult Create([FromBody] DownCountGame game)
        {
            if (game == null)
            {
                return(BadRequest());
            }

            Entities.DownCountGame downCountGame = new Entities.DownCountGame(game.TargetNumber, game.Numbers.ToArray());
            var solutions = DownCountSolver.Solve(downCountGame, false);
            DownCountGameResult gameResult = new DownCountGameResult();

            if (solutions.Count == 0)
            {
                gameResult.ExactResult = false;
                gameResult.Result      = string.Empty;
            }
            else
            {
                var solution = solutions[0];
                gameResult.ResultValue = solution.Value;
                gameResult.Result      = solution.ToString();
                if (solution.Value == downCountGame.TargetNumber)
                {
                    gameResult.ExactResult = true;
                }
                else
                {
                    gameResult.ExactResult = false;
                }
            }

            return(new ObjectResult(gameResult));
        }
示例#4
0
        public void DownCountSolver_Test3()
        {
            DownCountGame game      = new DownCountGame(577, 75, 9, 3, 4, 2, 1);
            Solutions     solutions = DownCountSolver.Solve(game);

            Assert.IsTrue(solutions.Count > 0);
            solutions.ForEach(solution => Assert.AreEqual(577, solution.Value));

            solutions = DownCountSolver.Solve(game, false);
            Assert.IsTrue(solutions.Count == 1);
            solutions.ForEach(solution => Assert.AreEqual(577, solution.Value));
        }
示例#5
0
        public void DownCountSolver_Test4()
        {
            DownCountGame game      = new DownCountGame(952, 25, 50, 75, 100, 3, 6);
            Solutions     solutions = DownCountSolver.Solve(game);

            Assert.IsTrue(solutions.Count > 0);
            solutions.ForEach(solution => Assert.AreEqual(952, solution.Value));

            solutions = DownCountSolver.Solve(game, false);
            Assert.IsTrue(solutions.Count == 1);
            solutions.ForEach(solution => Assert.AreEqual(952, solution.Value));
        }