示例#1
0
 public Board(Board b)
 {
     for (int i = 0; i < 9; i++)
     {
         for (int j = 0; j < 9; j++)
         {
             board[i,j] = new List<int>();
             for (int k = 0; k < b.board[i,j].Count; k++)
             {
                 board[i, j].Add(b.board[i, j][k]);
             }
         }
     }
 }
示例#2
0
 public RuleBasedSolver(string puzzle)
 {
     board = new Board();
     board.SetBoard(puzzle);
 }
示例#3
0
 /// <summary>
 /// Constructor for copying a board to the new RuleBasedSolver object. 
 /// </summary>
 /// <param name="b">The board to be copied.</param>
 public RuleBasedSolver(Board b)
 {
     board = new Board(b);
 }
示例#4
0
 /// <summary>
 /// Constructor which initialises the puzzle with the given grid.
 /// </summary>
 /// <param name="grid">A 9 by 9 int array which describes a puzzle grid. It has to be 9 by 9.</param>
 public RuleBasedSolver(int[,] grid)
 {
     board = new Board();
     board.SetBoard(grid);
 }
示例#5
0
 public RuleBasedSolver()
 {
     board = new Board();
 }
示例#6
0
        /// <summary>
        /// Guesses the content of one cell by using a brute force algorithm and continues with standard solving.
        /// </summary>
        /// <returns>Retuns the number of valid solutions.</returns>
        public int Guess()
        {
            //Find square with least possibilities.
            int[] min = { 100, 0, 0 }; // [min , i , j ];
            int solutions = 0;
            for (int i = 0; i < 9; i++)
            {
                for (int j = 0; j < 9; j++)
                {
                    if (board.board[i, j][0] == 0 && min[0] > board.board[i, j].Count)
                    {
                        min[0] = board.board[i, j].Count;
                        min[1] = i; min[2] = j;
                    }
                }
            }
            if (min[0] == 100)
            {
                return 1;
            }
            if (board.board[min[1], min[2]].Count == 1)
            {
                return 0;
            }
            List<Board> CorrectGuesses = new List<Board>();
            for (int g_index = 1; g_index < board.board[min[1], min[2]].Count; g_index++)
            {
                int g = board.board[min[1], min[2]][g_index];
                Board tmp = new Board(board);
                List<int> tmpList = tmp.board[min[1], min[2]];
                tmpList.Clear();
                tmpList.Add(g);
                tmp.UpdateCellPossibilities(min[1], min[2]);
                RuleBasedSolver solver = new RuleBasedSolver(tmp);
                if (StopOnMultipleSolutions)
                {
                    solver.StopOnMultipleSolutions = true;
                }
                solutions = solver.RunStep(EndTime);
                this.SingleCount += solver.SingleCount;
                this.NakedCount += solver.SingleCount;
                this.GuessCount += solver.GuessCount;
                if (StopOnMultipleSolutions && solutions > 1)
                {
                    return solutions;
                }
                while (solutions > 0)
                {
                    CorrectGuesses.Add(solver.board);
                    solutions--;
                }

            }

            if (CorrectGuesses.Count == 0)
            {
                return 0;
            }
            else
            {
                GuessCount++;
                board = new Board(CorrectGuesses[0]);
                return CorrectGuesses.Count;
            }
        }