Esempio n. 1
0
 public TekMoves(TekBoard board)
 {
     _moves                = new Stack <TekPlay>();
     _board                = board;
     _snapshots            = new TekSnapShot(Board);
     _snapshots.AutoRemove = true;
 }
Esempio n. 2
0
 public TekBoard(TekBoard board) : this(board.Rows, board.Cols)
 {
     CopyFields(board.Fields);
     Areas         = CopyAreas(board.Areas);
     EatExceptions = board.EatExceptions;
     AutoNotes     = board.AutoNotes;
 }
Esempio n. 3
0
 public void Export(TekBoard board, string filename)
 {
     using (StreamWriter wr = new StreamWriter(filename))
     {
         _Export(board, wr);
     }
 }
Esempio n. 4
0
 public override bool HeuristicApplies(TekBoard board, TekField field)
 {
     foreach (TekRegion region in TekRegion.GetCompactRegions(field))
     {
         foreach (TekField f in region.GetBorderFields())
         {
             try
             {
                 int[] tryValues = f.PossibleValues.ToArray(); // setting value will empty possiblevalues array
                 foreach (int value in tryValues)
                 {
                     f.Value = value;
                     if (region.GetTotalPossibleValues().Count < region.Fields.Count)
                     {
                         AddHeuristicFields(region.Fields);
                         AddAffectedFields(f);
                         AddValue(value);
                     }
                 }
                 if (AffectedFields.Count > 0)
                 {
                     return(true);
                 }
             }
             finally
             {
                 f.Value = 0;
             }
         }
     }
     return(false);
 }
Esempio n. 5
0
 private void UpdatePossibleValues(TekBoard board)
 {
     foreach (TekField field in board.Fields)
     {
         field.UpdatePossibleValues(false);
     }
 }
Esempio n. 6
0
        public override bool HeuristicApplies(TekBoard board, TekField field)
        {
            List <TekArea> AdjacentAreas = field.Area.GetAdjacentAreas();

            foreach (TekArea area in AdjacentAreas)
            {
                foreach (int value in field.PossibleValues)
                {
                    bool possible = false;
                    Region.Clear();
                    foreach (TekField f in area.Fields)
                    {
                        if (f.ValuePossible(value))
                        {
                            if (field.Influencers.Contains(f))
                            {
                                Region.AddField(f);
                            }
                            else
                            {
                                possible = true;
                                break;
                            }
                        }
                    }
                    if (!possible && Region.Fields.Count > 0)
                    {
                        AddAffectedField(field);
                        AddValue(value);
                        AddHeuristicFields(Region.Fields);
                    }
                }
            }
            return(AffectedFields.Count > 0 && HeuristicValues.Count > 0);
        }
Esempio n. 7
0
        private bool ParseValue(string input, TekBoard board)
        {
            int   row, col, value;
            Match match = valuePattern.Match(input);

            if (match.Success &&
                Int32.TryParse(match.Groups["row"].Value, out row) &&
                Int32.TryParse(match.Groups["col"].Value, out col) &&
                Int32.TryParse(match.Groups["value"].Value, out value)
                )
            {
                if (!board.IsInRange(row, col) || value <= 0 || value > Const.MAXTEK)
                {
                    ParseError("Invalid value line {0}: ({1},{2}", input, row, col);
                }
                TekField field = board.Fields[row, col];
                field.Value   = value;
                field.Initial = match.Groups["initial"].Value == "i";
                return(true);
            }
            else
            {
                return(false);
            }
        }
Esempio n. 8
0
        private bool ParseArea(string input, TekBoard board)
        {
            List <TekField> fields = new List <TekField>();
            Match           match  = areaPattern1.Match(input);

            if (match.Success)
            {
                if (!ParseAreaField(match.Groups["row"].Value, match.Groups["col"].Value, board, fields))
                {
                    ParseError("Invalid field in area line {0}: ({1},{2})", input, match.Groups["row"].Value, match.Groups["col"].Value);
                }
                match = areaPattern2.Match(match.Groups["rest"].Value);
                while (match.Success)
                {
                    if (!ParseAreaField(match.Groups["row"].Value, match.Groups["col"].Value, board, fields))
                    {
                        ParseError("Invalid field in area line {0}: ({1},{2})", input, match.Groups["row"].Value, match.Groups["col"].Value);
                    }
                    match = match.NextMatch();
                }
            }
            if (fields.Count > 0)
            {
                board.DefineArea(fields);
                return(true);
            }
            else
            {
                return(false);
            }
        }
Esempio n. 9
0
 private HeuristicAction _tryHeuristic(TekHeuristic heuristic, TekBoard board)
 {
     if (heuristic != null)
     {
         try
         {
             temStoredResults.Add(new TekHeuristicResult(heuristic));
             heuristic.ExecuteAction(temMoves);
             return(HeuristicAction.haNone);
         }
         catch (ETekFieldInvalid)
         {
             return(HeuristicAction.haExcludeValue);
         }
     }
     else if (board.IsSolved())
     {
         foreach (TekHeuristicResult result in temStoredResults)
         {
             heuristics.PrecomputedResults.Add(new TekHeuristicResult(result));
         }
         return(HeuristicAction.haSetValue);
     }
     else
     {
         return(HeuristicAction.haImpossible);
     }
 }
Esempio n. 10
0
        private void button11_Click(object sender, EventArgs e)
        {
            TekBoard board;

            board      = new TekBoard(6, 6);
            View.Board = board;
            Refresh();
        }
Esempio n. 11
0
 private void SetBoard(TekBoard value)
 {
     board = value;
     SetAreaColors(board);
     Selector.Reset();
     initializePanels();
     SetBorders();
 }
Esempio n. 12
0
        // prevent circular routing when computing distances

        public TekChains(TekBoard board)
        {
            Chains    = new List <List <TekField> >();
            Distances = new List <int[, ]>();
            InitializeChains(board);
            NormalizeChains();
            SortChains();
            ComputeDistances();
        }
Esempio n. 13
0
 public TekSnapShot(TekBoard board)
 {
     _ssValues         = new List <int[, ]>();
     _ssNotes          = new List <List <int> [, ]>();
     _ssExcludedValues = new List <List <int> [, ]>();
     _snapshots        = new List <string>();
     _board            = board;
     AutoRemove        = false;
 }
Esempio n. 14
0
        private void AddField(TekBoard board, TekField field)
        {
            List <TekField> chain = FindChain(field);

            if (chain == null)
            {
                chain = new List <TekField>();
                Chains.Add(chain);
            }
            chain.Add(field);
        }
Esempio n. 15
0
 public override bool HeuristicApplies(TekBoard board, TekField field)
 {
     if (field.PossibleValues.Count == 1)
     {
         AddHeuristicField(field);
         AddAffectedField(field);
         AddValue(field.PossibleValues[0]);
         return(true);
     }
     return(false);
 }
Esempio n. 16
0
        public override bool HeuristicApplies(TekBoard board, TekField field)
        {
            Stopwatch s = Stopwatch.StartNew();

            if (BruteForceSolve())
            {
                s.Stop();
                timeElapsed = s.Elapsed;
                board.LoadValues(Board.CopyValues());
                return(true);
            }
            return(false); // can't be solved
        }
Esempio n. 17
0
        public TekBoard Import(string filename)
        {
            TekBoard board = null;

            using (StreamReader sr = new StreamReader(filename))
            {
                if ((board = ParseStream(sr)) == null)
                {
                    ParseError("invalid file {0}", filename);
                }
            }
            return(board);
        }
Esempio n. 18
0
 protected override void BeforeProcessingBoard(TekBoard board)
 {
     Board = new TekBoard(board);
     SortedCandidates.Clear();
     foreach (TekField field in Board.Fields)
     {
         if (field.Value == 0)
         {
             SortedCandidates.Add(field);
         }
     }
     SortFields();
 }
Esempio n. 19
0
 private void InitializeChains(TekBoard board)
 {
     for (int r = 0; r < board.Rows; r++)
     {
         for (int c = 0; c < board.Cols; c++)
         {
             if (board.Fields[r, c].Value == 0 &&
                 board.Fields[r, c].PossibleValues.Count == 2)
             {
                 AddField(board, board.Fields[r, c]);
             }
         }
     }
 }
Esempio n. 20
0
        private bool ParseAreaField(string rowS, string colS, TekBoard board, List <TekField> fields)
        {
            int row, col;

            if (Int32.TryParse(rowS, out row) && Int32.TryParse(colS, out col))
            {
                if (board.IsInRange(row, col))
                {
                    fields.Add(board.Fields[row, col]);
                    return(true);
                }
            }
            return(false);
        }
Esempio n. 21
0
        public override bool HeuristicApplies(TekBoard board, TekField field)
        {
            if (!Chains.HasChains())
            {
                return(false);
            }

            List <List <TekField> > localChains = new List <List <TekField> >();
            List <TekField>         localFields = new List <TekField>();

            foreach (TekField f in field.Influencers)
            {
                List <TekField> chain = Chains.FindChain(f);
                if (chain != null && Chains.FindChain(field) != chain)
                {
                    localChains.Add(chain);
                    localFields.Add(f);
                }
            }
            for (int i = 0; i < localChains.Count; i++)
            {
                int j = localChains.IndexOf(localChains[i], i + 1);
                if (j != -1)
                {
                    if (Chains.ComputeDistance(localFields[i], localFields[j]) % 2 == 1)
                    {
                        bool noInfluence = true;
                        foreach (int value in Chains.ChainValues(localChains[i]))
                        {
                            if (field.ValuePossible(value))
                            {
                                noInfluence = false;
                            }
                        }
                        if (!noInfluence)
                        {
                            AddHeuristicFields(Chains.ShortestRoute(localFields[i], localFields[j]));
                            AddAffectedField(field);
                            AddValues(Chains.ChainValues(localChains[i]));
                            return(true);
                        }
                    }
                }
            }
            return(false);
        }
Esempio n. 22
0
        public override bool HeuristicApplies(TekBoard board, TekField field)
        {
            Dictionary <int, List <TekField> > FieldsPerValueInArea = field.Area.GetFieldsForValues();
            List <int>      CandidateValues = new List <int>();
            List <TekField> CandidateFields = new List <TekField>();

            foreach (int value in field.PossibleValues)
            {
                FieldsPerValueInArea[value].Remove(field);
                if (FieldsPerValueInArea[value].Count == 1)
                {
                    CandidateValues.Add(value);
                    CandidateFields.Add(FieldsPerValueInArea[value][0]);
                }
            }
            TekField field2 = null;
            int      value1 = 0, value2 = 0;

            for (int i = 0; i < CandidateFields.Count && field2 == null; i++)
            {
                for (int j = i + 1; j < CandidateFields.Count && field2 == null; j++)
                {
                    if (CandidateFields[j] == CandidateFields[i])
                    {
                        field2 = CandidateFields[i];
                        value1 = CandidateValues[i];
                        value2 = CandidateValues[j];
                    }
                }
            }
            if (field2 == null)
            {
                return(false);
            }
            AddHeuristicFields(field, field2);
            if (field.PossibleValues.Count > 2)
            {
                AddAffectedField(field);
            }
            if (field2.PossibleValues.Count > 2)
            {
                AddAffectedField(field2);
            }
            AddValues(value1, value2);
            return(AffectedFields.Count > 0);
        }
Esempio n. 23
0
        public override bool HeuristicApplies(TekBoard board, TekField field)
        {
            if (field.PossibleValues.Count != 2 || !Chains.HasChains())
            {
                return(false);
            }
            List <TekField> chain = Chains.FindChain(field);

            if (chain == null)
            {
                return(false);
            }
            foreach (TekField f in field.Influencers)
            {
                List <TekField> chain2 = Chains.FindChain(f);
                if (chain2 == null || chain2 == chain || Chains.CommonValues(chain, chain2).Count != 1)
                {
                    continue;
                }
                List <TekField> touchPoints1 = Chains.Intersection(chain2, chain);
                List <TekField> touchPoints2 = Chains.Intersection(chain, chain2);
                if (touchPoints1.Count == 2 && touchPoints2.Count == 2)
                {
                    List <TekField> chainFields1 = Chains.ShortestRoute(touchPoints1[0], touchPoints1[1]);
                    List <TekField> chainFields2 = Chains.ShortestRoute(touchPoints2[0], touchPoints2[1]);
                    if ((chainFields1.Count % 2 == 0) != (chainFields2.Count % 2 == 0))
                    {
                        AddValue(Chains.CommonValues(chain, chain2)[0]);
                        if (chainFields1.Count % 2 == 0)
                        {
                            AddAffectedFields(touchPoints2);
                            AddHeuristicFields(chainFields1);
                        }
                        else
                        {
                            AddAffectedFields(touchPoints1);
                            AddHeuristicFields(chainFields2);
                        }
                        return(true);
                    }
                }
            }
            return(false);
        }
Esempio n. 24
0
 public override bool HeuristicApplies(TekBoard board, TekField field)
 {
     if (field.PossibleValues.Count == 0)
     {
         return(false);
     }
     for (int i = 0; i < field.PossibleValues.Count; i++) // note: foreach can not work since we modify during processing!
     {
         try
         {
             field.Value = field.PossibleValues[i];         // trying this value
             foreach (TekField field1 in field.Influencers) // field must at least influence two other fields (which could now be pairs)
             {
                 Region.AddField(field1);
                 foreach (TekField field2 in field.Influencers)
                 {
                     if (field1 != field2)
                     {
                         Region.AddField(field2);
                         foreach (TekField field3 in field1.CommonInfluencers(field2))
                         {
                             // and if there is a third field as well we might have the invalid configuration
                             if (Region.IsInvalidThreePairs(field3))
                             {
                                 Region.AddField(field3);
                                 AddHeuristicFields(Region.Fields);
                                 AddAffectedField(field);
                                 AddValue(field.Value);
                                 return(true);
                             }
                         }
                         Region.RemoveField(field2);
                     }
                 }
             }
         }
         finally
         {
             field.Value = 0;
         }
     }
     Region.RemoveField(field);
     return(false);
 }
Esempio n. 25
0
 public override bool HeuristicApplies(TekBoard board, TekField field)
 {
     Region.Clear();
     Region.AddField(field);
     foreach (TekField field2 in field.Influencers)
     {
         Region.AddField(field2);
         foreach (TekField field3 in field.Influencers)
         {
             if (field != field2 && field != field3 && field2 != field3)
             {
                 if (Region.IsTriplet(field3))
                 {
                     Region.AddField(field3);
                     AddHeuristicFields(Region.Fields);
                     AddValues(Region.GetTotalPossibleValues());
                     // determine affected fields
                     foreach (TekField f in field.CommonInfluencers(field2, field3))
                     {
                         foreach (int value in f.PossibleValues)
                         {
                             if (HeuristicValues.Contains(value))
                             {
                                 AddAffectedField(f);
                                 break;
                             }
                         }
                     }
                     if (AffectedFields.Count > 0)
                     {
                         return(true);
                     }
                     else
                     {
                         Reset();
                     }
                 }
             }
         }
         Region.RemoveField(field2);
     }
     return(false);
 }
Esempio n. 26
0
 public TekHeuristic FindHeuristic(TekBoard board)
 {
     board.AutoNotes = true;
     if (PrecomputedResults.Count > 0)
     {
         TekHeuristic result = PrecomputedResults[0].AsHeuristic();
         PrecomputedResults.RemoveAt(0);
         return(result);
     }
     for (int i = 0; i < HeuristicIndex.Count; i++)
     {
         TekHeuristic heuristic = Heuristics[HeuristicIndex[i]];
         if (heuristic.Enabled && heuristic.Applies(board))
         {
             return(heuristic);
         }
     }
     return(null);
 }
Esempio n. 27
0
        public bool HeuristicSolve(TekBoard board, TekMoves moves)
        {
            TekHeuristic heuristic = FindHeuristic(board);
            bool         Paused    = false;

            while (heuristic != null && !Paused)
            {
                AfterHeuristicFoundHandler?.Invoke(heuristic);
                StoreResult(heuristic);
                if (BeforeExecutionHandler != null && !BeforeExecutionHandler(heuristic))
                {
                    return(false);
                }
                heuristic.ExecuteAction(moves);
                AfterExecutionHandler?.Invoke(heuristic);
                heuristic = FindHeuristic(board);
            }
            return(board.IsSolved());
        }
Esempio n. 28
0
        private HeuristicAction TryValue(TekBoard board, TekField field, int value)
        {
            bool            prev   = board.EatExceptions;
            HeuristicAction result = HeuristicAction.haNone;

            try
            {
                board.EatExceptions = false;
                this.Enabled        = false; // make sure FindHeuristic doesnt call this recursively!
                if ((result = _tryValue(field, value)) == HeuristicAction.haNone)
                {
                    temStoredResults.Clear();
                    temMoves.TakeSnapshot(_ssDescription(field));
                    try {
                        do
                        {
                            try
                            {
                                result = _tryHeuristic(heuristics.FindHeuristic(board), board);
                            }
                            catch (ETekFieldInvalid)
                            {
                                result = HeuristicAction.haImpossible;
                            }
                        } while (result == HeuristicAction.haNone);
                    }
                    finally
                    {
                        temMoves.RestoreSnapshot(_ssDescription(field));
                        _ssIndex++;
                    }
                }
            }
            finally
            {
                board.EatExceptions = prev;
                this.Enabled        = true;
                field.Value         = 0; // backtracking
            }
            return(result);
        }
Esempio n. 29
0
 public bool Applies(TekBoard board)
 {
     Reset();
     BeforeProcessingBoard(board);
     foreach (TekField field in board.Fields)
     {
         if (field.Value > 0)
         {
             continue;
         }
         if (HeuristicApplies(board, field))
         {
             return(true);
         }
         else
         {
             Reset();
         }
     }
     return(false);
 }
Esempio n. 30
0
        public void SetAreaColors(TekBoard board)
        {
            if (board.Areas.Count == 0)
            {
                return;
            }
            int index0 = 0;

            AreaColorIndex = new int[board.Areas.Count];
            for (int i = 0; i < AreaColorIndex.Length; i++)
            {
                AreaColorIndex[i] = -1;
            }

            foreach (TekArea area in board.Areas)
            {
                List <TekArea> neighbours        = area.GetAdjacentAreas();
                List <int>     inUseByNeighbours = new List <int>();
                foreach (TekArea area2 in neighbours)
                {
                    if (AreaColorIndex[area2.AreaNum] != -1)
                    {
                        inUseByNeighbours.Add(AreaColorIndex[area2.AreaNum]);
                    }
                }
                int index = (index0 + 1) % MAXCOLOR;
                while (index != index0)
                {
                    if (inUseByNeighbours.Contains(index))
                    {
                        index = (index + 1) % MAXCOLOR;
                    }
                    else
                    {
                        break;
                    }
                }
                AreaColorIndex[area.AreaNum] = index;
            }
        }