示例#1
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;
 }
示例#2
0
 static void UpliftLevel(ref ComplexityLevel level, ComplexityLevel newValue)
 {
     if (level != ComplexityLevel.Samurai && (int)newValue > (int)level)
     {
         level = newValue;
     }
 }
示例#3
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;
        }
示例#4
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;
        }
示例#5
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);
        }
示例#6
0
 public Question(object contents, ComplexityLevel level = ComplexityLevel.Normal)
     : this(contents, new Range<int>(1, 1), level)
 {
 }
示例#7
0
 public StringQuestion(string contents, ComplexityLevel level = ComplexityLevel.Normal)
     : base(contents, level)
 {
 }
示例#8
0
 public StringQuestion(string contents, Range<int> maxSelectedAnswersRange, ComplexityLevel level = ComplexityLevel.Normal)
     : base(contents, maxSelectedAnswersRange, level)
 {
 }