Ejemplo n.º 1
0
        public IActionResult Solve([FromBody] SolveSudokuModel model)
        {
            if (model.Cells.Length != 9)
            {
                return(new BadRequestObjectResult(new { error = "The sudoku grid should have 9 rows." }));
            }

            if (model.Cells.Any(x => x.Length != 9))
            {
                return(new BadRequestObjectResult(new { error = "Each sudoku row should have 9 columns." }));
            }

            var matrix = new short[9, 9];

            for (var row = 0; row < 9; row++)
            {
                for (var col = 0; col < 9; col++)
                {
                    matrix[row, col] = (short)model.Cells[row][col];
                }
            }

            var grid   = Grid.FromSudokuMatrix(matrix);
            var result = _solverService.Solve(grid);

            return(new JsonResult(result.IsSuccess));
        }
Ejemplo n.º 2
0
        public bool Solve(out Bitmap output)
        {
            _logger.LogTrace("AppService.Solve() invoked.");

            IAtlas <ImageItem> atlasOutput = null;
            var result = _solverService.Solve <ImageItem>(512, 512,
                                                          _imageList.Select(il => _imageAdapter.Adapt(il)).ToList(),
                                                          out atlasOutput);


            if (result)
            {
                output = _imageDrawerService.DrawAtlas <ImageItem>(atlasOutput);
            }
            else
            {
                output = null;
            }

            _logger.LogTrace($"AppService.Solve() finished and returned '{result}'");
            return(result);
        }
        public async Task <ActionResult <Solution> > Post([FromBody] ProblemDefinition definition)
        {
            var problemConfiguration = _mapper.Map <ProblemConfiguration>(definition);

            var username = Request.HttpContext.User.Identity.Name;

            try
            {
                var problemSolution = _solverService.Solve(problemConfiguration);

                if (problemSolution.HasOptimalSolution)
                {
                    await _problemSolutionService.Save(problemSolution, username);
                }

                var res = _mapper.Map <Solution>(problemSolution);

                return(res);
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
        }