Exemplo n.º 1
0
        /// <summary>
        /// rate a grid
        /// returns it as int
        /// the lower the harder
        /// </summary>
        /// <param name="grid">Grid</param>
        /// <returns></returns>
        public static int Rate(Grid gridOrg)
        {
            Grid grid = (Grid)gridOrg.Clone();
            int  rate = 0;

            rateAll = 0;
            //			for(int trys = 0; trys<10;trys++)
            //			{
            //			grid = (Grid)gridOrg.Clone();
            //              rate = 0;
            while (grid.CountFilledCells() < (grid.CellsInRow * grid.CellsInRow))
            {
                int cand = FindUniqueCandidates(grid);

                rate += cand;
                if (cand > 0)
                {
                    int candidateIndex = random.Next(cand);
                    int m = -1, i = 0, j = 0, candidateRow = 0, candidateCol = 0;

                    for (i = 0; i < 9 && m < candidateIndex; i++)
                    {
                        for (j = 0; j < 9 && m < candidateIndex; j++)
                        {
                            if (grid.uniqueCandidates[i, j] != 0)
                            {
                                m++;
                                if (m == candidateIndex)
                                {
                                    candidateRow = i;
                                    candidateCol = j;
                                }
                            }
                        }
                    }
                    grid.cells[candidateRow, candidateCol] = grid.uniqueCandidates[candidateRow, candidateCol];
                }
                else
                {
                    //rate = rate - ((grid.BlocksAcross * grid.BlocksAcross) - grid.CountFilledCells());
                    //no more unique cells -> difficult grid
                    rate = grid.CountFilledCells();
                    break;
                }
            }
            //				rateAll += rate;
            //				MediaPortal.GUI.Library.Log.WriteFile(MediaPortal.GUI.Library.LogType.Log, "Soduko Rate end "+trys+" notes: {0}", rate);
            //			}
            return(rate);
        }
Exemplo n.º 2
0
 public static Grid FillOutCells(Grid grid, Grid solution, int number)
 {
     for (; number > 0; number--)
     {
         int candidateIndex = random.Next(81 - grid.CountFilledCells());
         int m = -1, i = 0, j = 0;
         for (i = 0; i < 9 && m < candidateIndex; i++)
         {
             for (j = 0; j < 9 && m < candidateIndex; j++)
             {
                 if (grid.cells[i, j] == 0)
                 {
                     m++;
                     if (m == candidateIndex)
                     {
                         grid.cells[i, j] = solution.cells[i, j];
                     }
                 }
             }
         }
     }
     return(grid);
 }
Exemplo n.º 3
0
 public static Grid FillOutCells(Grid grid, Grid solution, int number)
 {
   for (; number > 0; number--)
   {
     int candidateIndex = random.Next(81 - grid.CountFilledCells());
     int m = -1, i = 0, j = 0;
     for (i = 0; i < 9 && m < candidateIndex; i++)
     {
       for (j = 0; j < 9 && m < candidateIndex; j++)
       {
         if (grid.cells[i, j] == 0)
         {
           m++;
           if (m == candidateIndex)
           {
             grid.cells[i, j] = solution.cells[i, j];
           }
         }
       }
     }
   }
   return grid;
 }
Exemplo n.º 4
0
    /// <summary>
    /// Solve given sudoku grid and solutions upto maximum number specified
    /// </summary>
    /// <param name="g"></param>
    /// <returns></returns>
    public static IList Solve(Grid grid, int maxSolutions)
    {
      ArrayList solutions = new ArrayList();
      // If grid is solved then return as solution
      if (grid.CountFilledCells() == (grid.CellsInRow * grid.CellsInRow))
      {
        solutions.Add(grid.Clone());
        return solutions;
      }

      // Solve singles
      //Singles(grid);

      // Choose unsolved cell
      int leastCandidatesRow = -1;
      int leastCandidatesColumn = -1;
      IList leastCandidates = null;

      for (int row = 0; row < grid.CellsInRow; row++)
      {
        for (int column = 0; column < grid.CellsInRow; column++)
        {
          if (grid.cells[row, column] == 0)
          {
            IList candidates = grid.Possibilities(row, column);

            // If cell has no possible value then grid is not solvable so quit now
            if (candidates.Count == 0)
            {
              return solutions;
            }
            else if (leastCandidates == null || leastCandidates.Count > candidates.Count)
            {
              leastCandidatesRow = row;
              leastCandidatesColumn = column;
              leastCandidates = candidates;
            }
          }
        }
      }

      // For all candidates of unsolved cell
      if (leastCandidates != null)
      {
        while (leastCandidates.Count > 0)
        {
          // Set candidate
          int candidateIndex = random.Next(leastCandidates.Count);
          grid.cells[leastCandidatesRow, leastCandidatesColumn] = (int)leastCandidates[candidateIndex];
          leastCandidates.RemoveAt(candidateIndex);

          Grid nextLevelGrid = (Grid)grid.Clone();

          IList nextLevelSolutions = Solve(nextLevelGrid, maxSolutions);

          solutions.AddRange(nextLevelSolutions);

          // Trim number of solutions so we don't exceed maximum required
          if (solutions.Count > maxSolutions)
          {
            solutions.RemoveRange(0, solutions.Count - maxSolutions);
          }

          if (solutions.Count == maxSolutions)
          {
            return solutions;
          }
        }
      }
      return solutions;
    }
Exemplo n.º 5
0
        protected override void OnClicked(int controlId, GUIControl control, Action.ActionType actionType)
        {
            if (control == btnSolve)
            {
                // Solve grid
                Grid solution = Solver.Solve(grid);
                if (solution != null)
                {
                    for (int row = 0; row < grid.CellsInRow; row++)
                    {
                        for (int column = 0; column < grid.CellsInRow; column++)
                        {
                            int         cellControlId = (1000 * (row + 1)) + column;
                            CellControl cntlFoc       = (CellControl)GetControl(cellControlId);
                            if (cntlFoc.editable)
                            {
                                cntlFoc.CellValue         = solution.cells[row, column];
                                cntlFoc.M_dwDisabledColor = m_dwCellIncorrectTextColor;
                                cntlFoc.editable          = false;
                            }
                        }
                    }
                }
            }
            else if (control == btnNewGame)
            {
                //new game
                GUIWaitCursor.Show();
                int minrating = 0;
                int maxrating = 0;
                ClearGrid();
                Grid puzzle = new Grid();
                switch ((LevelName)_Settings.Level)
                {
                case LevelName.Kids:
                    minrating = 550;
                    maxrating = 999;
                    break;

                case LevelName.Easy:
                    minrating = 450;
                    maxrating = 650;
                    break;

                case LevelName.Medium:
                    minrating = 250;
                    maxrating = 550;
                    break;

                case LevelName.Hard:
                    minrating = 0;
                    maxrating = 250;
                    break;
                }
                puzzle     = GenerateLevel(puzzle, minrating, maxrating);
                gameRating = Solver.Rate(puzzle);
                //puzzle = Solver.Generate(3);
                Grid solution = Solver.Solve(puzzle);
                if ((LevelName)_Settings.Level == LevelName.Easy)
                {
                    puzzle     = Solver.FillOutCells(puzzle, solution, 10);
                    gameRating = Solver.Rate(puzzle);
                }
                else if ((LevelName)_Settings.Level == LevelName.Kids)
                {
                    puzzle     = Solver.FillOutCells(puzzle, solution, 20);
                    gameRating = Solver.Rate(puzzle) * 2;
                }

                for (int row = 0; row < grid.CellsInRow; row++)
                {
                    for (int column = 0; column < grid.CellsInRow; column++)
                    {
                        int         cellControlId = (1000 * (row + 1)) + column;
                        CellControl cntlFoc       = (CellControl)GetControl(cellControlId);
                        cntlFoc.CellValue = puzzle.cells[row, column];
                        if (cntlFoc.CellValue > 0)
                        {
                            cntlFoc.editable = false;
                        }
                        else
                        {
                            cntlFoc.SolutionValue = solution.cells[row, column];
                        }
                    }
                }
                grid = puzzle;
                ResetCandidates();
                GUIWaitCursor.Hide();
                StartTimer();
                gameRunning = true;
                if (_Settings.Show || _Settings.Block)
                {
                    isScoreGame = false;
                }
                else
                {
                    isScoreGame = true;
                }
            }
            else if (control == btnBlockInvalidMoves)
            {
                _Settings.Block = btnBlockInvalidMoves.Selected;
                if (btnBlockInvalidMoves.Selected)
                {
                    if (btnShowInvalidMoves.Selected)
                    {
                        _Settings.Show = btnShowInvalidMoves.Selected = false;
                    }
                    isScoreGame = false;
                }
                _Settings.Save();
            }
            else if (control == btnClear)
            {
                ClearGrid();
                ResetCandidates();
            }
            else if (control == btnShowInvalidMoves)
            {
                _Settings.Show = btnShowInvalidMoves.Selected;
                if (btnShowInvalidMoves.Selected)
                {
                    if (btnBlockInvalidMoves.Selected)
                    {
                        _Settings.Block = btnBlockInvalidMoves.Selected = false;
                    }
                    isScoreGame = false;
                }
                ShowInvalid();
                _Settings.Save();
            }
            else if (control == btnLevel)
            {
                switch ((LevelName)_Settings.Level)
                {
                case LevelName.Kids:
                    _Settings.Level = (int)LevelName.Easy;
                    break;

                case LevelName.Easy:
                    _Settings.Level = (int)LevelName.Medium;
                    break;

                case LevelName.Medium:
                    _Settings.Level = (int)LevelName.Hard;
                    break;

                case LevelName.Hard:
                    _Settings.Level = (int)LevelName.Kids;
                    break;
                }
                UpdateButtonStates();
                _Settings.Save();
            }
            else if (control == btnHelpOnce)
            {
                int candidateIndex = random.Next(81 - grid.CountFilledCells());
                int m = -1, row = 0, column = 0;
                isScoreGame = false;

                for (row = 0; row < 9 && m < candidateIndex; row++)
                {
                    for (column = 0; column < 9 && m < candidateIndex; column++)
                    {
                        int         cellControlId = (1000 * (row + 1)) + column;
                        CellControl cntlFoc       = (CellControl)GetControl(cellControlId);
                        if (cntlFoc.editable == true && cntlFoc.CellValue == 0)
                        {
                            m++;
                            if (m == candidateIndex)
                            {
                                cntlFoc.CellValue       = cntlFoc.SolutionValue;
                                grid.cells[row, column] = cntlFoc.SolutionValue;
                            }
                        }
                    }
                    CheckCandidates();
                }
            }
            else if (control == btnResetGame)
            {
                ResetGame();
            }
            base.OnClicked(controlId, control, actionType);
        }
Exemplo n.º 6
0
        /// <summary>
        /// Solve given sudoku grid and solutions upto maximum number specified
        /// </summary>
        /// <param name="g"></param>
        /// <returns></returns>
        public static IList Solve(Grid grid, int maxSolutions)
        {
            ArrayList solutions = new ArrayList();

            // If grid is solved then return as solution
            if (grid.CountFilledCells() == (grid.CellsInRow * grid.CellsInRow))
            {
                solutions.Add(grid.Clone());
                return(solutions);
            }

            // Solve singles
            //Singles(grid);

            // Choose unsolved cell
            int   leastCandidatesRow    = -1;
            int   leastCandidatesColumn = -1;
            IList leastCandidates       = null;

            for (int row = 0; row < grid.CellsInRow; row++)
            {
                for (int column = 0; column < grid.CellsInRow; column++)
                {
                    if (grid.cells[row, column] == 0)
                    {
                        IList candidates = grid.Possibilities(row, column);

                        // If cell has no possible value then grid is not solvable so quit now
                        if (candidates.Count == 0)
                        {
                            return(solutions);
                        }
                        else if (leastCandidates == null || leastCandidates.Count > candidates.Count)
                        {
                            leastCandidatesRow    = row;
                            leastCandidatesColumn = column;
                            leastCandidates       = candidates;
                        }
                    }
                }
            }

            // For all candidates of unsolved cell
            if (leastCandidates != null)
            {
                while (leastCandidates.Count > 0)
                {
                    // Set candidate
                    int candidateIndex = random.Next(leastCandidates.Count);
                    grid.cells[leastCandidatesRow, leastCandidatesColumn] = (int)leastCandidates[candidateIndex];
                    leastCandidates.RemoveAt(candidateIndex);

                    Grid nextLevelGrid = (Grid)grid.Clone();

                    IList nextLevelSolutions = Solve(nextLevelGrid, maxSolutions);

                    solutions.AddRange(nextLevelSolutions);

                    // Trim number of solutions so we don't exceed maximum required
                    if (solutions.Count > maxSolutions)
                    {
                        solutions.RemoveRange(0, solutions.Count - maxSolutions);
                    }

                    if (solutions.Count == maxSolutions)
                    {
                        return(solutions);
                    }
                }
            }
            return(solutions);
        }