public async Task CreateSolutions()
        {
            // Arrange

            // Act
            var result = await sut.Create(newSolution);

            // Assert
            Assert.That(result.Success, Is.True);
            Assert.That((SudokuSolution)result.Object, Is.InstanceOf <SudokuSolution>());
        }
        public async Task <IBaseResult> CheckAnnonymousGame(List <int> intList)
        {
            if (intList == null)
            {
                throw new ArgumentNullException(nameof(intList));
            }

            var result = new BaseResult();

            if (intList.Count != 81 || intList.Contains(0))
            {
                result.Success = false;
                result.Message = GamesMessages.GameNotSolvedMessage;

                return(result);
            }

            var game = new Game(
                new Difficulty
            {
                DifficultyLevel = DifficultyLevel.TEST
            },
                intList);

            result.Success = game.IsSolved();

            if (result.Success)
            {
                // Add solution to the database
                var solutionsResponse = await solutionsRepository.GetSolvedSolutions();

                if (solutionsResponse.Success)
                {
                    var solutionInDB = false;

                    foreach (var solution in solutionsResponse.Objects.ConvertAll(s => (SudokuSolution)s))
                    {
                        if (solution.SolutionList.IsThisListEqual(game.SudokuSolution.SolutionList))
                        {
                            solutionInDB = true;
                        }
                    }

                    if (!solutionInDB)
                    {
                        _ = solutionsRepository.Create(game.SudokuSolution);
                    }
                }

                result.Message = GamesMessages.GameSolvedMessage;
            }
            else
            {
                result.Message = GamesMessages.GameNotSolvedMessage;
            }

            return(result);
        }
示例#3
0
        public async Task <ISolutionResult> Solve(
            ISolutionRequest request)
        {
            if (request == null)
            {
                throw new ArgumentNullException(nameof(request));
            }

            var result = new SolutionResult();

            try
            {
                var solvedSolutions = ((await solutionsRepository.GetSolvedSolutions()).Objects)
                                      .ConvertAll(s => (SudokuSolution)s);

                var intList = new List <int>();

                intList.AddRange(request.FirstRow);
                intList.AddRange(request.SecondRow);
                intList.AddRange(request.ThirdRow);
                intList.AddRange(request.FourthRow);
                intList.AddRange(request.FifthRow);
                intList.AddRange(request.SixthRow);
                intList.AddRange(request.SeventhRow);
                intList.AddRange(request.EighthRow);
                intList.AddRange(request.NinthRow);

                var sudokuSolver = new SudokuMatrix(intList);

                await sudokuSolver.Solve();

                if (sudokuSolver.IsValid())
                {
                    var solution = new SudokuSolution(sudokuSolver.ToIntList());

                    var addResultToDataContext = true;

                    if (solvedSolutions.Count > 0)
                    {
                        foreach (var solvedSolution in solvedSolutions)
                        {
                            if (solvedSolution.ToString().Equals(solution.ToString()))
                            {
                                addResultToDataContext = false;
                            }
                        }
                    }

                    if (addResultToDataContext)
                    {
                        solution = (SudokuSolution)(await solutionsRepository.Create(solution)).Object;
                    }
                    else
                    {
                        solution = solvedSolutions.Where(s => s.SolutionList.IsThisListEqual(solution.SolutionList)).FirstOrDefault();
                    }

                    result.Success  = true;
                    result.Solution = solution;
                    result.Message  = SolutionsMessages.SudokuSolutionFoundMessage;
                }
                else
                {
                    intList = sudokuSolver.ToIntList();

                    if (solvedSolutions.Count > 0)
                    {
                        var solutonInDB = false;

                        foreach (var solution in solvedSolutions)
                        {
                            var possibleSolution = true;

                            for (var i = 0; i < intList.Count - 1; i++)
                            {
                                if (intList[i] != 0 && intList[i] != solution.SolutionList[i])
                                {
                                    possibleSolution = false;
                                    break;
                                }
                            }

                            if (possibleSolution)
                            {
                                solutonInDB     = possibleSolution;
                                result.Success  = possibleSolution;
                                result.Solution = solution;
                                result.Message  = SolutionsMessages.SudokuSolutionFoundMessage;
                                break;
                            }
                        }

                        if (!solutonInDB)
                        {
                            result.Success  = false;
                            result.Solution = null;
                            result.Message  = SolutionsMessages.SudokuSolutionNotFoundMessage;
                        }
                    }
                    else
                    {
                        result.Success  = false;
                        result.Solution = null;
                        result.Message  = SolutionsMessages.SudokuSolutionNotFoundMessage;
                    }
                }

                return(result);
            }
            catch (Exception exp)
            {
                result.Success = false;
                result.Message = exp.Message;

                return(result);
            }
        }