示例#1
0
 static void UpliftLevel(ref ComplexityLevel level, ComplexityLevel newValue)
 {
     if (level != ComplexityLevel.Samurai && (int)newValue > (int)level)
     {
         level = newValue;
     }
 }
示例#2
0
        public async Task <IHttpActionResult> PutComplexityLevel(Guid id, ComplexityLevel complexityLevel)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != complexityLevel.Id)
            {
                return(BadRequest());
            }

            db.Entry(complexityLevel).State = EntityState.Modified;

            try
            {
                await db.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!ComplexityLevelExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(StatusCode(HttpStatusCode.NoContent));
        }
示例#3
0
 public Question(string question, Answer answer, IList <Answer> answers, ComplexityLevel complexityLevel)
 {
     this.QuestionDescription = question;
     this.Answer          = answer;
     this.Answers         = answers;
     this.ComplexityLevel = complexityLevel;
 }
示例#4
0
 public Question(object contents, Range<int> maxSelectedAnswersRange, ComplexityLevel level = ComplexityLevel.Normal)
 {
     if (ReferenceEquals(contents, null))
         throw new ArgumentNullException("contents");
     if (maxSelectedAnswersRange.LowerBound < 1)
         throw new InvalidRangeException("Lower bound has to be greater than 0.");
     ContentsAsObject = contents;
     _maxSelectedAnswersRange = maxSelectedAnswersRange;
     _complexityLevel = level;
 }
示例#5
0
        private static bool Examination(Grid grid, Action <Grid> progressAction, int depth, ref int tryCount)
        {
            if (depth >= Settings.Default.MaxDepth)
            {
                return(false);
            }

            var cells = grid.Where(z => z.IsEmpty).OrderBy(z => z.Count())
                        .Select(z => new
            {
                Keys = z.ToSet(),
                z.Row,
                z.Column
            }).ToArray();

            foreach (var cell in cells)
            {
                foreach (var key in cell.Keys)
                {
                    tryCount++;

                    var newGrid = grid.Clone();
                    progressAction?.Invoke(newGrid);

                    newGrid[cell.Row, cell.Column].PromoteValue(key);

                    ComplexityLevel level = ComplexityLevel.Samurai;

                    try
                    {
                        if (UsePredictableRules(newGrid, ref level))
                        {
                            grid.Promote(newGrid);
                            progressAction?.Invoke(grid);
                            return(true);
                        }
                    }
                    catch (SudokuException)
                    {
                        continue;
                    }

                    if (Examination(newGrid, progressAction, depth + 1, ref tryCount))
                    {
                        grid.Promote(newGrid);
                        progressAction?.Invoke(grid);
                        return(true);
                    }
                }
            }

            progressAction?.Invoke(grid);

            return(false);
        }
示例#6
0
        public async Task <IHttpActionResult> GetComplexityLevel(Guid id)
        {
            ComplexityLevel complexityLevel = await db.complexityLevel.FindAsync(id);

            if (complexityLevel == null)
            {
                return(NotFound());
            }

            return(Ok(complexityLevel));
        }
示例#7
0
        public async Task <IHttpActionResult> PostComplexityLevel(ComplexityLevel complexityLevel)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            db.complexityLevel.Add(complexityLevel);
            await db.SaveChangesAsync();

            return(CreatedAtRoute("DefaultApi", new { id = complexityLevel.Id }, complexityLevel));
        }
示例#8
0
        public void TestSolver(string fileName, ComplexityLevel expectedLevel)
        {
            var grid = Grid.LoadFromFile(fileName);

            ComplexityLevel level;
            int             tryCount;
            var             rc = Processor.Solve(grid, null, out level, out tryCount);


            Assert.True(rc);
            Assert.True(grid.IsValid);
            Assert.Equal(expectedLevel, level);
        }
示例#9
0
        public async Task <IHttpActionResult> DeleteComplexityLevel(Guid id)
        {
            ComplexityLevel complexityLevel = await db.complexityLevel.FindAsync(id);

            if (complexityLevel == null)
            {
                return(NotFound());
            }

            db.complexityLevel.Remove(complexityLevel);
            await db.SaveChangesAsync();

            return(Ok(complexityLevel));
        }
示例#10
0
        public static bool Solve(Grid grid, Action <Grid> progressAction, out ComplexityLevel complexityLevel, out int tryCount)
        {
            complexityLevel = ComplexityLevel.Easy;

            if (UsePredictableRules(grid, ref complexityLevel))
            {
                tryCount = 0;
                return(true);
            }

            var ret = Examination(grid, progressAction, out tryCount);

            if (ret)
            {
                complexityLevel = tryCount < 2 ? ComplexityLevel.Hard : ComplexityLevel.Samurai;
            }
            else
            {
                complexityLevel = ComplexityLevel.Samurai;
            }

            return(ret);
        }
示例#11
0
 static void UpliftLevel(ref ComplexityLevel level, ComplexityLevel newValue)
 {
     if (level != ComplexityLevel.Samurai && (int)newValue > (int)level)
     {
         level = newValue;
     }
 }
示例#12
0
        public static bool UsePredictableRules(Grid grid, ref ComplexityLevel complexityLevel)
        {
            bool changed;

            do
            {
                // easy level
                changed = UseRules(grid, AdjustCells);

                if (changed)
                {
                    if (grid.IsCompleted)
                    {
                        return true;
                    }
                    continue;
                }

                grid.Disabled = false;

                Contract.Assert(grid.IsValid);

                // middle level
                UpliftLevel(ref complexityLevel, ComplexityLevel.Medium);

                changed = UseRules(grid, LastOneRule);
                if (changed)
                {
                    if (grid.IsCompleted)
                    {
                        return true;
                    }
                    continue;
                }

                Contract.Assert(grid.IsValid);

                changed = UseRules(grid, cells => SetRules(cells, FindPairs));
                if (changed)
                {
                    if (grid.IsCompleted)
                    {
                        return true;
                    }
                    continue;
                }

                Contract.Assert(grid.IsValid);

                // hard level
                UpliftLevel(ref complexityLevel, ComplexityLevel.Hard);

                changed = UseRules(grid, cells => SetRules(cells, FindTriads));
                if (changed)
                {
                    if (grid.IsCompleted)
                    {
                        return true;
                    }
                    continue;
                }

                Contract.Assert(grid.IsValid);

                changed = UseRules(grid, cells => SetRules(cells, FindQuads));
                if (changed)
                {
                    if (grid.IsCompleted)
                    {
                        return true;
                    }
                    continue;
                }

                Contract.Assert(grid.IsValid);

                changed = UseRules(grid, cells => HiddenSetRules(cells, FindHiddenPairs));
                if (changed)
                {
                    if (grid.IsCompleted)
                    {
                        return true;
                    }
                    continue;
                }

                Contract.Assert(grid.IsValid);

                changed = UseRules(grid, cells => HiddenSetRules(cells, FindHiddenTriads));
                if (changed)
                {
                    if (grid.IsCompleted)
                    {
                        return true;
                    }
                    continue;
                }

                Contract.Assert(grid.IsValid);

                changed = UseRules(grid, cells => HiddenSetRules(cells, FindHiddenQuads));
                if (changed)
                {
                    if (grid.IsCompleted)
                    {
                        return true;
                    }
                    continue;
                }

                Contract.Assert(grid.IsValid);

                // impossible level
                UpliftLevel(ref complexityLevel, ComplexityLevel.Samurai);

                changed = BlockJunctions(grid);
                if (changed)
                {
                    if (grid.IsCompleted)
                    {
                        return true;
                    }
                    continue;
                }

                Contract.Assert(grid.IsValid);

                changed = ColumnJunction(grid);
                if (changed)
                {
                    if (grid.IsCompleted)
                    {
                        return true;
                    }
                    continue;
                }

                changed = RowJunction(grid);
                if (changed)
                {
                    if (grid.IsCompleted)
                    {
                        return true;
                    }
                    continue;
                }

                Contract.Assert(grid.IsValid);

            } while (changed);

            return false;
        }
示例#13
0
        public static bool Solve(Grid grid, Action<Grid> progressAction, out ComplexityLevel complexityLevel, out int tryCount)
        {
            complexityLevel = ComplexityLevel.Easy;

            if (UsePredictableRules(grid, ref complexityLevel))
            {
                tryCount = 0;
                return true;
            }

            var ret = Examination(grid, progressAction, out tryCount);
            if (ret)
            {
                complexityLevel = tryCount < 2 ? ComplexityLevel.Hard : ComplexityLevel.Samurai;
            }
            else
            {
                complexityLevel = ComplexityLevel.Samurai;
            }

            return ret;
        }
示例#14
0
        public void TestSolver(string fileName, ComplexityLevel expectedLevel)
        {
            var grid = Grid.LoadFromFile(fileName);

            ComplexityLevel level;
            int tryCount;
            var rc = Processor.Solve(grid, null, out level, out tryCount);

            Assert.True(rc);
            Assert.True(grid.IsValid);
            Assert.Equal(expectedLevel, level);
        }
示例#15
0
 public Question(object contents, ComplexityLevel level = ComplexityLevel.Normal)
     : this(contents, new Range<int>(1, 1), level)
 {
 }
示例#16
0
 public StringQuestion(string contents, ComplexityLevel level = ComplexityLevel.Normal)
     : base(contents, level)
 {
 }
示例#17
0
 public StringQuestion(string contents, Range<int> maxSelectedAnswersRange, ComplexityLevel level = ComplexityLevel.Normal)
     : base(contents, maxSelectedAnswersRange, level)
 {
 }
示例#18
0
        public static bool UsePredictableRules(Grid grid, ref ComplexityLevel complexityLevel)
        {
            bool changed;

            do
            {
                // easy level
                changed = UseRules(grid, AdjustCells);

                if (changed)
                {
                    if (grid.IsCompleted)
                    {
                        return(true);
                    }
                    continue;
                }

                grid.Disabled = false;

                Contract.Assert(grid.IsValid);

                // middle level
                UpliftLevel(ref complexityLevel, ComplexityLevel.Medium);

                changed = UseRules(grid, LastOneRule);
                if (changed)
                {
                    if (grid.IsCompleted)
                    {
                        return(true);
                    }
                    continue;
                }

                Contract.Assert(grid.IsValid);

                changed = UseRules(grid, cells => SetRules(cells, FindPairs));
                if (changed)
                {
                    if (grid.IsCompleted)
                    {
                        return(true);
                    }
                    continue;
                }

                Contract.Assert(grid.IsValid);

                // hard level
                UpliftLevel(ref complexityLevel, ComplexityLevel.Hard);

                changed = UseRules(grid, cells => SetRules(cells, FindTriads));
                if (changed)
                {
                    if (grid.IsCompleted)
                    {
                        return(true);
                    }
                    continue;
                }

                Contract.Assert(grid.IsValid);

                changed = UseRules(grid, cells => SetRules(cells, FindQuads));
                if (changed)
                {
                    if (grid.IsCompleted)
                    {
                        return(true);
                    }
                    continue;
                }

                Contract.Assert(grid.IsValid);

                changed = UseRules(grid, cells => HiddenSetRules(cells, FindHiddenPairs));
                if (changed)
                {
                    if (grid.IsCompleted)
                    {
                        return(true);
                    }
                    continue;
                }

                Contract.Assert(grid.IsValid);

                changed = UseRules(grid, cells => HiddenSetRules(cells, FindHiddenTriads));
                if (changed)
                {
                    if (grid.IsCompleted)
                    {
                        return(true);
                    }
                    continue;
                }

                Contract.Assert(grid.IsValid);

                changed = UseRules(grid, cells => HiddenSetRules(cells, FindHiddenQuads));
                if (changed)
                {
                    if (grid.IsCompleted)
                    {
                        return(true);
                    }
                    continue;
                }

                Contract.Assert(grid.IsValid);

                // impossible level
                UpliftLevel(ref complexityLevel, ComplexityLevel.Samurai);

                changed = BlockJunctions(grid);
                if (changed)
                {
                    if (grid.IsCompleted)
                    {
                        return(true);
                    }
                    continue;
                }

                Contract.Assert(grid.IsValid);

                changed = ColumnJunction(grid);
                if (changed)
                {
                    if (grid.IsCompleted)
                    {
                        return(true);
                    }
                    continue;
                }

                changed = RowJunction(grid);
                if (changed)
                {
                    if (grid.IsCompleted)
                    {
                        return(true);
                    }
                    continue;
                }

                Contract.Assert(grid.IsValid);
            } while (changed);

            return(false);
        }
示例#19
0
        public QuestionsImportResultDTO ProccessFileForImport(string path)
        {
            var content        = System.IO.File.ReadAllText(path);
            var questionBlocks = content.Split(new string[] { new string('-', 5) }, StringSplitOptions.RemoveEmptyEntries);
            var questions      = new List <QuestionDTO>();
            var sb             = new StringBuilder();
            var i = 0;

            foreach (var questionBlock in questionBlocks)
            {
                try
                {
                    i++;
                    string patternForText = @"(?<=Текст:)([\s\S]*?)(?=Зображення:)";
                    var    test           = Regex.Match(questionBlock, patternForText);
                    string textOfQuestion = Regex.Match(questionBlock, patternForText).Value;
                    Debug.WriteLine(i);
                    textOfQuestion = textOfQuestion.Substring(0, textOfQuestion.LastIndexOf(';'));
                    string imageLink   = Regex.Match(questionBlock, patternForImagelink).Value;
                    string levelString = Regex.Match(questionBlock, patternForLevel).Value;

                    ValidateBaseInformation(ref sb, textOfQuestion, levelString, i);
                    ComplexityLevel level = levelString == "Легкий" ? level = ComplexityLevel.Easy :
                                                                              (levelString == "Середній" ? ComplexityLevel.Medium : ComplexityLevel.Hard);

                    var question = new QuestionDTO
                    {
                        Text      = textOfQuestion,
                        ImageLink = imageLink,
                        Level     = level,
                        Answers   = new List <AnswerDTO>()
                    };
                    var answerBlocks = Regex.Matches(questionBlock, patternForAnswers);
                    var j            = 0;
                    foreach (Match answerBlock in answerBlocks)
                    {
                        j++;
                        bool isCorrent  = answerBlock.Value.Contains("(вірна)");
                        var  answerText = answerBlock.Value.Substring(answerBlock.Value.IndexOf(":") + 1);

                        if (string.IsNullOrEmpty(answerText))
                        {
                            sb.AppendLine($"Помилка: на {i}-ому питанні текст {j}-ої відповіді пустий");
                        }
                        question.Answers.Add(new AnswerDTO
                        {
                            Text      = answerText,
                            IsCorrect = isCorrent
                        });
                    }
                    if (!question.Answers.Any(x => x.IsCorrect))
                    {
                        sb.AppendLine($"Помилка: на {i}-ому питанні немає жодної коректної відповіді");
                    }
                    questions.Add(question);
                }
                catch
                {
                    var a = i;
                    ;
                }
            }

            if (sb.Length != 0)
            {
                return(new QuestionsImportResultDTO
                {
                    IsSuccessful = false,
                    ErrorMessage = sb.ToString().Split(new string[] { Environment.NewLine }, StringSplitOptions.None),
                });
            }

            return(new QuestionsImportResultDTO
            {
                IsSuccessful = true,
                GottenQuestions = questions
            });
        }