Beispiel #1
0
        public static void Run([DefaultValue("Game1.txt")] string gameFile, [DefaultValue("18")] int depthPenalty,
                               [DefaultValue("50000")] int maxMoves)
        {
            string gameId = gameFile.Replace(".txt", "");

            Log.Logger = new LoggerConfiguration()
                         .WriteTo.LiterateConsole()
                         .WriteTo.File($"Log-{gameId}_Depth-{depthPenalty}")
                         .MinimumLevel.Debug()
                         .CreateLogger();

            ImmutableBubbleBurstGrid grid;

            using (var stream = File.OpenRead($"Games//{gameFile}"))
            {
                grid = BubbleGridBuilder.Create(new StreamReader(stream));
            }

            var solutionFileName = $"Solution_{gameId}_Depth-{depthPenalty}.json";

            File.Delete(solutionFileName);
            using (var fileStream = File.OpenWrite(solutionFileName))
                using (var writer = new StreamWriter(fileStream))
                {
                    var solver   = new GridSolver(grid, writer, depthPenalty);
                    var solution = solver.Solve(maxMoves);

                    writer.Write(JsonConvert.SerializeObject(solution.Moves));

                    solution.GridState.Display();
                }
        }
Beispiel #2
0
        public void AnEmptyGridIsSolvedWithOnlyMovingForward()
        {
            var grid   = Grid.OfSize(3);
            var solver = new GridSolver(grid);

            Assert.That(solver.Solve(), Is.True);
            Console.WriteLine(grid.ToString());
        }
Beispiel #3
0
        public void ForceASingleValue()
        {
            var grid = Grid.OfSize(3);

            grid.Cell(2, 1, 5);
            grid.Freeze();
            var solver = new GridSolver(grid);

            Assert.That(solver.Solve(), Is.True);
            Console.WriteLine(grid.ToString());
        }
Beispiel #4
0
        public void AnUnsolvableGrid()
        {
            var grid = Grid.OfSize(3);

            grid.Cell(1, 1, 5);
            grid.Cell(2, 1, 5);
            grid.Freeze();
            var solver = new GridSolver(grid);

            Assert.That(solver.Solve(), Is.False);
            Console.WriteLine(grid.ToString());
        }
        public ActionResult <string> Post([FromBody] string body, [FromQuery(Name = "rd")] string rowdelimiter)
        {
            if (String.IsNullOrWhiteSpace(body))
            {
                return(this.BadRequest("you must provide a body containing the 9x9 puzzle to be solved"));
            }

            Grid grid = null;

            try
            {
                var puzzle = Converter.From(body, rowdelimiter);
                grid = Grid.From(puzzle);
            }
            catch (FormatException fe)
            {
                return(this.BadRequest("the supplied puzzle contained non-numeric values"));
            }
            catch (ArgumentException ae)
            {
                return(this.BadRequest(ae.Message));
            }
            catch (Exception ex)
            {
                this._logger.LogError(ex.ToString());
                return(this.Problem("an error occurred while parsing the supplied puzzle", statusCode: 500));
            }

            try
            {
                var solver = new GridSolver(grid);
                var result = solver.Solve();
                if (result)
                {
                    return(this.Ok(grid.ToString()));
                }
                else
                {
                    return(this.UnprocessableEntity("the puzzle was unsolvable"));
                }
            }
            catch (ArgumentException ae)
            {
                return(this.BadRequest(ae.Message));
            }
            catch (Exception ex)
            {
                this._logger.LogError(ex.ToString());
                return(this.Problem("an error occurred while solving the supplied puzzle", statusCode: 500));
            }
        }
Beispiel #6
0
        public void SolveAFullPuzzle()
        {
            var values = new int[][]
            {
                new int[] { 0, 9, 0, 0, 0, 8, 6, 0, 4 },
                new int[] { 6, 0, 0, 0, 1, 9, 0, 0, 0 },
                new int[] { 0, 4, 8, 6, 2, 5, 0, 0, 0 },
                new int[] { 0, 2, 0, 0, 8, 0, 7, 1, 6 },
                new int[] { 0, 7, 0, 0, 0, 0, 0, 8, 0 },
                new int[] { 3, 8, 5, 0, 7, 0, 0, 2, 0 },
                new int[] { 0, 0, 0, 8, 6, 1, 5, 4, 0 },
                new int[] { 0, 0, 0, 3, 5, 0, 0, 0, 2 },
                new int[] { 5, 0, 7, 2, 0, 0, 0, 3, 0 },
            };

            var grid   = Grid.From(values);
            var solver = new GridSolver(grid);

            Assert.That(solver.Solve(), Is.True);
            Console.WriteLine(grid.ToString());
        }