Esempio n. 1
0
 bool SolveComplete(Board board)
 {
     List<Board> sucessors = new System.Collections.Generic.List<Board>();
     for (int i = 0; i < 9; i++)
     {
         for (int j = 0; j < 9; j++)
         {
             if (board.spaces[i][j] == 0)
             {
                 for (int r = 1; r < 10; r++)
                 {
                     board.SetSpace(r, i, j);
                     if (board.IsSolved(i, j))
                     {
                         watch.Stop();
                         Solved++;
                         return true;
                     }
                     if (board.IsValid(i,j))
                     {
                         sucessors.Add(board.DeepClone());
                     }
                 }
                 foreach (Board brd in sucessors)
                 {
                     SolveComplete(brd);
                 }
                 return false;
             }
         }
     }
     if (myboard.IsValid())
     {
         return true;
     }
     return false;
 }
Esempio n. 2
0
 private bool Generate(Board board)
 {
     List<Board> sucessors = new System.Collections.Generic.List<Board>();
     for (int i = 0; i < 9; i++)
     {
         for (int j = 0; j < 9; j++)
         {
             if (board.spaces[i][j] == 0)
             {
                 Random rand = new Random();
                 List<int> nums = new List<int>() { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
                 while(nums.Count!=0)
                 {
                     int r = nums[rand.Next(0,nums.Count)];
                     nums.Remove(r);
                     board.SetSpace(r, i, j);
                     if (board.IsValid())
                     {
                         sucessors.Add(board.DeepClone());
                         if (board.IsSolved(i, j))
                         {
                             watch.Stop();
                             myboard = board.DeepClone();
                             throw new System.ArgumentException("Solved");
                         }
                     }
                 }
                 foreach (Board brd in sucessors)
                 {
                     Generate(brd);
                 }
             }
         }
     }
     if (myboard.IsValid())
     {
         return true;
     }
     return false;
 }