Esempio n. 1
0
        protected void Enumerate()
        {
            Square current = new Square();
              bool takeFromSquaresFiguredOut = false;

              while (SquaresToTry.Count > 0 || BacktrackStack.Count > 0) {
            if (takeFromSquaresFiguredOut && SquaresFiguredOut.Count > 0) {
              current = SquaresFiguredOut.Pop();
            }
            else {
              if (BacktrackStack.Any()) {
            current = BacktrackStack.Pop();
            RefreshSquare(current);
              }
              else {
            current = SquaresToTry.OrderBy(x => x.PossibleValues.Count()).Take(1).First();
            SquaresToTry.Remove(current);
              }
            }

            if (IsValidValueFoundForSquare(current)) {
              SquaresFiguredOut.Push(current);
              takeFromSquaresFiguredOut = false;
            }
            else {
              BacktrackStack.Push(current);
              takeFromSquaresFiguredOut = true;
            }
            RefreshPossibleValues();
              }
        }
Esempio n. 2
0
 protected virtual bool DoesSquareInSameZoneShareValue(Square current)
 {
     return SquaresFiguredOut.Any(square =>
                                 (square.XCoordinate == current.XCoordinate
                               || square.YCoordinate == current.YCoordinate
                               || square.BlockNumber == current.BlockNumber) && square.Number == current.Number);
 }
Esempio n. 3
0
        public void SolvePartialSolution(string filename)
        {
            SquaresFiguredOut = new Stack<Square>();
              SquaresToTry = new List<Square>();
              PartialSolution = new Stack<Square>();
              BacktrackStack = new Stack<Square>();

              var digits = ParseFile(filename);
              int length = (int)Math.Sqrt(digits.Count);
              this.GridLength = length;
              this.GridLengthSqrt = (int)Math.Sqrt(this.GridLength);
              GeneratePossibleValues(length);

              for (int row = 1; row <= length; row++) {
            for (int col = 1; col <= length; col++) {
              var square = new Square();
              square.XCoordinate = col;
              square.YCoordinate = row;
              square.BlockNumber = GetBlockNumber(col, row);

              if (digits[(row - 1) * length + col - 1] != "_") {
            square.Number = Convert.ToInt32(digits[(row - 1) * length + col - 1]);
            PartialSolution.Push(square);
              }
              else {
            SquaresToTry.Add(square);
              }
            }
              }
              RefreshPossibleValues();
              Enumerate();
        }
Esempio n. 4
0
        protected override bool DoesSquareInSameZoneShareValue(Square current)
        {
            if (base.DoesSquareInSameZoneShareValue(current))
            return true;

              foreach (var square in PartialSolution) {
            if ((square.XCoordinate == current.XCoordinate
              || square.YCoordinate == current.YCoordinate
              || square.BlockNumber == current.BlockNumber)
              && square.Number == current.Number) {
              return true;
            }
              }
              return false;
        }
Esempio n. 5
0
        void init()
        {
            Console.WriteLine("Before start thread");

            Thread[] testThread = new Thread[11];
            Thread tid1 = new Thread(new ThreadStart(MyThread.Thread1));
            Thread tid2 = new Thread(new ThreadStart(MyThread.Thread2));

            testThread[0] = tid1;
            testThread[1] = tid2;

            for (int i = 2; i < 11; ++i)
            {
                Thread tid = new Thread(new ThreadStart(MyThread.Thread3));
                testThread[i] = tid;
               
            }
            foreach (Thread myThread in testThread)
            {
                update_label.Text = "Starting Thread " + (Array.IndexOf(testThread, myThread) + 1);
                update_label.Refresh();
                Thread.Sleep(1000);
                myThread.Start();
            }
            foreach (Thread myThread in testThread)
            {
                myThread.Join();
                update_label.Text = "Threads joined.";
                update_label.Refresh();
            }
            
            /* Cross check all problem rows with all problem columns, then use square
                to determine what numbers are missing and compare with those rows and columns
                to not make duplicates */

            if(MyThread.getProblemRows().Count != 0 && MyThread.getProblemColumns().Count != 0)
            {
                label1.Text = "Problem rows: \n" + string.Join(", ", MyThread.getProblemRows());
                label1.Refresh();

                label2.Text = "Problem columns: \n" + string.Join(", ", MyThread.getProblemColumns());
                label2.Refresh();

                foreach(int r in MyThread.getProblemRows())
                {
                    foreach(int c in MyThread.getProblemColumns())
                    {
                        Tuple<int, int> tuple = new Tuple<int, int>(r, c);
                        problems.Add(tuple);
                    }
                }

                label3.Text = "Fix: \n" + string.Join(", ", problems);
                label3.Refresh();

                //dataGridView1.Rows[rowIndex].Cells[columnIndex].Style.BackColor = Color.Red;

                //var list = new List<int>(new[] { 1, 2, 4, 7, 9 });
                // var result = Enumerable.Range(0, 10).Except(list);

                var missingSquareList = new List<IEnumerable<int>>();
                foreach (Square s in Square.getSquares())
                {
                    List<int> list = s.getSquare().Cast<int>().ToList();
                    var missing = Enumerable.Range(1, 9).Except(list);
                    missingSquareList.Add(missing);
                    //MessageBox.Show(string.Join(", ", result));
                }

                var missingRowList = new List<IEnumerable<int>>();
                foreach(int rowM in MyThread.getProblemRows())
                {
                    List<int> list2 = new List<int>();
                    for(int g = 0; g < 9; g++)
                    {
                        list2.Add(grid[rowM, g]);
                    }
                    var missing = Enumerable.Range(1, 9).Except(list2);
                    missingRowList.Add(missing);
                }

                var missingColList = new List<IEnumerable<int>>();
                foreach(int colM in MyThread.getProblemColumns())
                {
                    List<int> list3 = new List<int>();
                    for(int g = 0; g < 9; g++)
                    {
                        list3.Add(grid[g, colM]);
                    }
                    var missing = Enumerable.Range(1, 9).Except(list3);
                    missingColList.Add(missing);
                }



                foreach(Tuple<int,int> t in problems)
                {
                    
                }
            }
            else
            {
                label3.Text = "Valid solution";
                label3.Refresh();
            }
           
             
           


        }
Esempio n. 6
0
File: Group.cs Progetto: bjeanes/uni
 /// <summary>
 /// Adds a square to the group.
 /// </summary>
 /// <param name="square">square to add to group.</param>
 public void AddSquare(Square square)
 {
     this.squares[numSquares] = square;
     this.numSquares++;
 }
Esempio n. 7
0
 protected void InitializeSquaresToTry()
 {
     for (var row = 1; row <= GridLength; row++) {
     for (var column = 1; column <= GridLength; column++) {
       var newSquare = new Square {
         XCoordinate = column,
         YCoordinate = row,
         BlockNumber = GetBlockNumber(column, row),
         PossibleValues = new List<int>(PossibleValues)
       };
       SquaresToTry.Add(newSquare);
     }
       }
 }
Esempio n. 8
0
 private void RefreshSquare(Square current)
 {
     current.PossibleValues = new List<int>();
       foreach (var possibleValue in PossibleValues) {
     current.Number = possibleValue;
     if (!DoesSquareInSameZoneShareValue(current)) {
       current.PossibleValues.Add(possibleValue);
     }
       }
       current.Number = null;
 }
Esempio n. 9
0
        protected bool IsValidValueFoundForSquare(Square current)
        {
            var random = new Random();

            while (current.PossibleValues.Count > 0) {
            int valueToTry = current.PossibleValues.OrderBy(x => random.Next()).Take(1).First();
            current.Number = valueToTry;
            current.PossibleValues.Remove(valueToTry);

            if (!DoesSquareInSameZoneShareValue(current)) {
                return true;
            }
            }
              current.Number = null;
              return false;
        }