Sudoku generator level of dificulty

Cochera

New Member
Joined
Feb 20, 2022
Messages
3
Office Version
  1. 2016
Platform
  1. Windows
Hi there,

I have problem with my Sudoku Generator. It's about the difficulty level.

My macro always returns 39-43 numbers completed.

How to do to increase the difficulty level and the macro returned ≈27 for the intermediate level, ≈21 for the difficult level, and ≈37 for the easy level.

VBA Code:
Sub CreatPuzzle()

Sheets("sudoku1").Select

Dim num As Integer, LoopCount As Integer
Dim CellRow As Integer, CellCol As Integer
Dim uCell As Integer, Comp As Boolean

Do
    Randomize
    CellRow = Int((10 - 2 + 1) * Rnd + 2)
    CellCol = Int((10 - 2 + 1) * Rnd + 2)
    num = Val(Cells(CellRow, CellCol).Value)
    If num <> 0 Then
        Comp = False
        For uCell = 2 To 10
            'Rows
            If Cells(uCell, CellCol).Value = "" Then
                If WorksheetFunction.CountIf(Rows(uCell), num) = 0 And _
                WorksheetFunction.CountIf(Range(qRNG(uCell, CellCol)), num) = 0 Then Comp = True
            End If
              
            'Columns
             If Cells(CellRow, uCell).Value = "" Then
                If WorksheetFunction.CountIf(Columns(uCell), num) = 0 And _
                WorksheetFunction.CountIf(Range(qRNG(CellRow, uCell)), num) = 0 Then Comp = True
            End If
        Next uCell
        
        If Comp = False Then
            Cells(CellRow, CellCol).Value = ""
        End If
      End If
      
      LoopCount = LoopCount + 1
      If LoopCount > 299 Then Exit Do
    Loop
End Sub
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
I forgot to paste it:
VBA Code:
Function qRNG(r As Integer, C As Integer) As String
    If C < 5 Then
        If r < 5 Then
            qRNG = "B2:D4"
        ElseIf r < 8 Then
            qRNG = "B5:D7"
        Else
            qRNG = "B8:D10"
        End If
    ElseIf C < 8 Then
        If r < 5 Then
            qRNG = "E2:G4"
        ElseIf r < 8 Then
            qRNG = "E5:G7"
        Else
            qRNG = "E8:G10"
        End If
    Else
        If r < 4 Then
            qRNG = "H2:J4"
        ElseIf r < 7 Then
            qRNG = "H5:J7"
        Else
            qRNG = "H8:J10"
        End If
    End If
End Function
 
Upvote 0
Welcome to the Forum!

Your code applies only the simplest of checks, and therefore can only generate very easy puzzles.

To generate harder puzzles, you'll need more code with more sophisticated checks to remove more numbers from the grid. Is that the question - how to write a Sudoku generator? There are many resources on the web, including free workbooks and source code available for download.

Incidentally, qRNG can be written far more succinctly ...

VBA Code:
Function qRNG(i As Long, j As Long) As Range
    
    Set qRNG = Cells(2 + 3 * Int((i - 2) / 3), 2 + 3 * Int((j - 2) / 3)).Resize(3, 3)
    
End Function

... if you also replace Range(qRNG(uCell, CellCol)) with qRNG(uCell, CellCol), and Range(qRNG(CellRow, uCell)) with (qRNG(CellRow, uCell).
 
Upvote 0
Welcome to the Forum!

Your code applies only the simplest of checks, and therefore can only generate very easy puzzles.

To generate harder puzzles, you'll need more code with more sophisticated checks to remove more numbers from the grid. Is that the question - how to write a Sudoku generator? There are many resources on the web, including free workbooks and source code available for download.

Incidentally, qRNG can be written far more succinctly ...

VBA Code:
Function qRNG(i As Long, j As Long) As Range
   
    Set qRNG = Cells(2 + 3 * Int((i - 2) / 3), 2 + 3 * Int((j - 2) / 3)).Resize(3, 3)
   
End Function

... if you also replace Range(qRNG(uCell, CellCol)) with qRNG(uCell, CellCol), and Range(qRNG(CellRow, uCell)) with (qRNG(CellRow, uCell).
Hello Stephen thank you for your post.

I have already sudoku generator in excel and it work good. The problem is in the last macro (code paste in previous post). Its clearing from the sudoku grid numbers but after I run macro grid still have from 39 to 43 numbers which is a lot. I want to made macro for 3 difficulties (easy, medium and hard). Easy -37 numbers, Medium - 27 numbers and Hard 21 numbers, just don't know what I have change in code to have this results (clearing more numbers from grid).

Best regards.
 
Upvote 0

Forum statistics

Threads
1,223,909
Messages
6,175,313
Members
452,634
Latest member
cpostell

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top