コード例 #1
0
ファイル: Solver.cs プロジェクト: JurjenBiewenga/SudokuSolver
        /// <summary>
        /// Returns the first cell that has the lowest amount of options available
        /// </summary>
        /// <param name="possibleValues"></param>
        /// <param name="point"></param>
        /// <returns></returns>
        private bool FindBestCellToFill(List <int>[,] possibleValues, out Point point)
        {
            Point best       = new Point(-1, -1);
            int   bestAmount = 0;

            for (int x = 0; x < Board.Size; x++)
            {
                for (int y = 0; y < Board.Size; y++)
                {
                    if (_currentBoard.Get(new Point(x, y)) != 0)
                    {
                        continue;
                    }

                    if (possibleValues[y, x].Count == 1)
                    {
                        point = new Point(x, y);
                        return(true);
                    }

                    if (bestAmount == 0 || possibleValues[y, x].Count < bestAmount)
                    {
                        if (possibleValues[y, x].Count == 0)
                        {
                            continue;
                        }

                        best.X     = x;
                        best.Y     = y;
                        bestAmount = possibleValues[y, x].Count;
                    }
                }
            }

            if (bestAmount == 0)
            {
                point = new Point();
                return(false);
            }

            point = best;
            return(true);
        }
コード例 #2
0
ファイル: Solver.cs プロジェクト: JurjenBiewenga/SudokuSolver
        private List <int>[,] FindValidValues(Board board)
        {
            List <int>[,] possibleValues = new List <int> [Board.Size, Board.Size];

            Fill2DArrayWithRange(possibleValues, 1, Board.Size);

            for (int x = 0; x < Board.Size; x++)
            {
                for (int y = 0; y < Board.Size; y++)
                {
                    int value = board.Get(new Point(x, y));
                    if (value != 0)
                    {
                        possibleValues[y, x].Clear();
                        RemoveValueFromRow(possibleValues, y, value);
                        RemoveValueFromCollumn(possibleValues, x, value);
                        RemoveValueFromArea(possibleValues, board.GetAreaStartPointFromPointOnBoard(new Point(x, y)), value);
                    }
                }
            }

            return(possibleValues);
        }
コード例 #3
0
ファイル: SudokuGame.cs プロジェクト: Tilps/Stash
 private void button2_Click(object sender, EventArgs e)
 {
     if (boxes == null)
     {
         return;
     }
     Board board = new Board(sizey, sizex);
     for (int i = 0; i < boxes.Count; i++)
     {
         for (int j = 0; j < boxes[i].Count; j++)
         {
             try
             {
                 if (boxes[i][j].Text.Length != 0)
                 {
                     int value = int.Parse(boxes[i][j].Text);
                     if (value > 0 && value <= boxes.Count)
                         board.Set(i, j, value);
                 }
             }
             catch
             {
                 try
                 {
                     if (boxes[i][j].Text.Length != 0)
                     {
                         int value = char.ToUpper(boxes[i][j].Text[0])-'A'+10;
                         if (value > 0 && value <= boxes.Count)
                             board.Set(i, j, value);
                     }
                 }
                 catch
                 {
                 }
             }
         }
     }
     try
     {
         if (textBox2.Text.Length != 0)
         {
             string[] splits = textBox2.Text.Split('.');
             board.MaxLookahead = int.Parse(splits[0]);
         }
     }
     catch
     {
     }
     board.UseLogging = true;
     SolveState result = board.SolveWithRating();
     if (board.LastLookaheadUsed != 1)
         textBox2.Text = board.LastLookaheadUsed.ToString();
     else
         textBox2.Text = board.LastLookaheadUsed.ToString() + "." + board.Score.ToString() + "." + board.HighTuples.ToString();
     for (int i = 0; i < boxes.Count; i++)
     {
         for (int j = 0; j < boxes[i].Count; j++)
         {
             int value = board.Get(i, j);
             if (value != 0)
             {
                 boxes[i][j].Text = value.ToString();
             }
         }
     }
     textBox3.Text = "";
     if (result != SolveState.Solved)
         MessageBox.Show("Solving produced the following result: " + result.ToString());
     else
         textBox3.Lines = board.Log.Split('\n');
 }
コード例 #4
0
ファイル: SudokuGame.cs プロジェクト: Tilps/Stash
 private string[] DrawBoard(Board board2)
 {
     List<string> strs = new List<string>();
     for (int i = 0; i < boxes.Count; i++)
     {
         if (i % sizey == 0 && i != 0)
         {
             string str2 = "";
             for (int j = 0; j < boxes.Count ; j++)
             {
                 if (j % sizex == 0 && j != 0)
                     str2 += '+';
                 str2 += '-';
             }
             strs.Add(str2);
         }
         string str = "";
         for (int j = 0; j < boxes.Count; j++)
         {
             if (j % sizex == 0 && j != 0)
                 str += '|';
             if (board2.Get(i, j) == 0)
                 str += ".";
             else if (board2.Get(i, j) < 10)
                 str += board2.Get(i, j).ToString();
             else
                 str += ((char)('A' + board2.Get(i, j) - 10)).ToString();
         }
         strs.Add(str);
     }
     return strs.ToArray();
 }