Пример #1
0
 public Unit(Square[] members)
 {
     unitHasBeenChanged = false;
     myMembers = members;
     n2 = myMembers.Length;
     n = (int)Math.Sqrt(n2);
     n4 = n2 * n2;
     maxSum = (int)Math.Pow(2, n2 - 1);
     isComplete = checkCompletion();
     valueTable = new bool[n2][];
 }
Пример #2
0
 public UnitType(Square[] squares, int n, int type)
 {
     changesMade = false;
     this.n = n;
     n2 = n * n;
     n4 = n2 * n2;
     allSquares = squares;
     myType = type;
     groupedSquares = new Square[n2][];
     isComplete = checkCompletion();
     groupSquares();
 }
Пример #3
0
        static void Main(string[] args)
        {
            Board b = new Board(n);
            StreamReader inputPuzzles;
            Square[] firstThree = new Square[3];
            int sum = 0;
            inputPuzzles = new StreamReader("p096_sudoku.txt");
            String temp, puzzleString;
            for (int currentPuzzle = 0; !inputPuzzles.EndOfStream; currentPuzzle++)
            {
                puzzleString = "";
                temp = "";
                temp = inputPuzzles.ReadLine();
                while (!inputPuzzles.EndOfStream && !temp.StartsWith("G"))
                {
                    puzzleString = puzzleString + temp;
                    temp = inputPuzzles.ReadLine();
                }
                if (puzzleString.Length > n)
                {
                    b = new Board(3);
                    b.setupBoardFromString(puzzleString);

                    //b.showSquares();
                    b.completePuzzle();
                    Console.WriteLine(b.showSquares());
                    temp = "";
                    //Console.WriteLine(b.showSquares());
                    for (int index = 0; index < 3; index++)
                    {
                        if ((b.getSquares()[index].getValue() + 1) == 0)
                        {
                            Console.WriteLine(b.showSquares());
                            break;
                        }
                        temp = temp + (b.getSquares()[index].getValue() + 1);

                    }
                    //Console.WriteLine(temp);
                    sum += int.Parse(temp);
                    //Console.WriteLine(sum);
                    // Console.ReadLine();
                }

            }
            Console.WriteLine(sum);
            Console.ReadLine();
        }
Пример #4
0
        private int numberOfIterations = 0; //how many alternations between types have been made

        #endregion Fields

        #region Constructors

        //Constructors
        public Board(int n, Square[] squares)
        {
            initalize(n, squares);
        }
Пример #5
0
 private void initalize(int n, Square[] squares)
 {
     this.n = n;
     n2 = n * n;
     n4 = n2 * n2;
     allSquares = squares;
     complete = false;//has to be false in this case as the board is empty
 }
Пример #6
0
        private void Guess()
        {
            //find a square with two possible values
            Console.WriteLine("Guessing!");
            Square[] temp;
            Board branch;
            List<Square[]> permutations = new List<Square[]>();
            for (int i = 0; i < n4; i++)
            {
                if (allSquares[i].getValue() != Square.NULL_VALUE)
                {

                    foreach (int values in allSquares[i].GetPossibleValuesList())
                    {
                        temp = new Square[n4];
                        Array.Copy(allSquares, temp, n4);
                        temp[i].setValue(i);
                        permutations.Add(temp);
                    }
                }
            }
            foreach (Square[] sArr in permutations)
            {
                branch = new Board(n, sArr);
                branch.completePuzzle();
                if (branch.isLegal() && branch.isComplete())
                {
                    allSquares = branch.allSquares;
                    Console.WriteLine(showSquares() + "\n\n!!!!!!!");
                    Console.ReadLine();
                }

            }
        }
Пример #7
0
 /**
     Populate the squares array with null values and relevant positions
 */
 private void fillSquares()
 {
     for (int position = 0; position < n4; position++)//iterate through each element in the array
     {
         allSquares[position] = new Square(position / n2, position % n2, n);//create a new square with proper row and column values
     }
 }
Пример #8
0
 /**
     Go to the next iteration if the board is not complete
 */
 public bool nextStep()
 {
     Square[] comparatorSquares = new Square[allSquares.Length];
     Array.Copy(allSquares, comparatorSquares, allSquares.Length);
     complete = completionStatus();//figure out if the board is complete
     if (!complete && isLegal() && count < GUESS_MARK + 1)
     {
         UnitType current;
         current = new UnitType(allSquares, n, typesAvailable[numberOfIterations % typesAvailable.Length]);//take the next unit type and set current to that
         allSquares = current.operate();//set allSquares of the board equal to what it calculates
         numberOfIterations++;
         complete = completionStatus(); //update completion status
         if (!current.HaveChangesBeenMade())
         {
             count++;
             Console.WriteLine(count);
         }
         if (count >= GUESS_MARK)
         {
             //Guess();
             //count = 0;
         }
         return false;//not complete
     }
     else//the board is complete. Don't iterate anymore
     {
         return true;//complete
     }
 }
Пример #9
0
 private Square[] stitchJaggedArray(Square[][] sJaggedArray)
 {
     List<Square> sList = new List<Square>();
     foreach (Square[] sArr in sJaggedArray)
     {
         sList.AddRange(sArr);
     }
     sList.Sort();
     return sList.ToArray();
 }
Пример #10
0
        private void groupSquares()
        {
            int[] currentPositionInGroupArray = new int[n2];
            int tempGrouping;
            for (int i = 0; i < n2; i++)
            {
                groupedSquares[i] = new Square[n2];
            }
            for (int i = 0; i < n4; i++)
            {
                tempGrouping = allSquares[i].getGroupingValue(myType);

                groupedSquares[tempGrouping][currentPositionInGroupArray[tempGrouping]] = allSquares[i];
                currentPositionInGroupArray[tempGrouping]++;
            }
        }