ExcelHome技术论坛

 找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

搜索
EH技术汇-专业的职场技能充电站 妙哉!函数段子手趣味讲函数 Excel服务器-会Excel,做管理系统 效率神器,一键搞定繁琐工作
HR薪酬管理数字化实战 Excel 2021函数公式学习大典 Excel数据透视表实战秘技 打造核心竞争力的职场宝典
让更多数据处理,一键完成 数据工作者的案头书 免费直播课集锦 ExcelHome出品 - VBA代码宝免费下载
用ChatGPT与VBA一键搞定Excel WPS表格从入门到精通 Excel VBA经典代码实践指南
查看: 5173|回复: 19

[分享] 数独游戏

[复制链接]

TA的精华主题

TA的得分主题

发表于 2016-2-19 20:31 | 显示全部楼层 |阅读模式
本帖最后由 lingyuncelia 于 2016-2-21 19:27 编辑

游戏规则自行百度。隐含对数法只能实现部分功能,其它功能多人验证,暂未发现问题。
如果有人能计算出正确答案,请发表,请共享!我与我周边的同事暂时未有人能破解。
哪位大神能破解?

数独.zip

54.77 KB, 下载次数: 246

评分

1

查看全部评分

TA的精华主题

TA的得分主题

发表于 2016-2-19 21:20 | 显示全部楼层
以下所有代码放在模块1中:
  1. Private Const SUDOKU_TITLE = "Su Doku "
  2. Private Const SUDOKU_VERSION = " v1.1b"
  3. Private Const SUDOKU_DATE = "Feb 2005"
  4. Private Const SUDOKU_AUTHOR = "Andy Pope"
  5. Private Const SUDOKU_DISPLAYGRID = "Grid"
  6. Private Const SUDOKU_STORAGEGRID = "StorageGrid"
  7. Private Const SUDOKU_SHEET = "SuDoku"

  8. Private Const SUDOKU_MSG_BUILDPUZZLE = "Please wait whilst I generate a new puzzle"
  9. Private Const SUDOKU_MSG_REVEALPUZZLE = "Please wait whilst the solution is revealed"
  10. Private Const SUDOKU_MSG_SOLVEPUZZLE = "Please wait whilst I attempt to solve your puzzle"
  11. Private Const SUDOKU_MSG_USERPUZZLE = "The puzzle appears to be user entered. Or contains incorrect answers." _
  12.                     & vbLf & vbLf & "Press Yes to attempt to solve the puzzle" & _
  13.                     vbLf & "No to reveal answer." & vbLf & "Or Cancel"
  14. Private Const SUDOKU_MSG_CONTINUESOLVING = "This puzzle is taking a long while to solve. Do you wish to continue?"

  15. Dim m_intGrid(81, 9) As Integer '(3x3) x 3 grids upto 9 values
  16. Dim m_intSubGrid(9, 9) As Integer
  17. Private Sub m_AssignCellValue(Cell As Integer, Value As Integer)
  18. '
  19. ' Set specified cell to Value
  20. '
  21.     Dim intIndex As Integer
  22.    
  23.     For intIndex = 1 To 9
  24.         m_intGrid(Cell, intIndex) = -intIndex
  25.     Next
  26.     m_intGrid(Cell, 0) = Value
  27.     m_intSubGrid(m_GetSubGrid(Cell), Value) = Value
  28.    
  29. End Sub

  30. Private Function m_AssignDisplay() As Range
  31. '
  32. ' create pointer to Display grid
  33. '
  34.     Set m_AssignDisplay = ThisWorkbook.Worksheets(SUDOKU_SHEET).Range(SUDOKU_DISPLAYGRID)

  35. End Function
  36. Private Function m_AssignStorage() As Range
  37. '
  38. ' create pointer to Storage grid
  39. '
  40.     Set m_AssignStorage = ThisWorkbook.Worksheets(SUDOKU_SHEET).Range(SUDOKU_STORAGEGRID)

  41. End Function
复制代码

TA的精华主题

TA的得分主题

发表于 2016-2-19 21:20 | 显示全部楼层
第二部分:
  1. Private Function m_CanUse(Cell As Integer, Value As Integer) As Boolean
  2. '
  3. ' Check whether using value in cell will allow related sub grids to place same value elsewhere
  4. '
  5.     Dim intRow As Integer
  6.     Dim intCol As Integer
  7.     Dim blnAlternative As Boolean
  8.    
  9.     intRow = m_WhichSubRow(Cell)
  10.     intCol = m_WhichSubCol(Cell)
  11.    
  12.     Select Case intRow
  13.     Case 1
  14.         blnAlternative = m_AlternativeRowAvailable(2, Value)
  15.         blnAlternative = blnAlternative Or m_AlternativeRowAvailable(3, Value)
  16.     Case 2
  17.         blnAlternative = m_AlternativeRowAvailable(1, Value)
  18.         blnAlternative = blnAlternative Or m_AlternativeRowAvailable(3, Value)
  19.     Case 3
  20.         blnAlternative = m_AlternativeRowAvailable(1, Value)
  21.         blnAlternative = blnAlternative Or m_AlternativeRowAvailable(2, Value)
  22.     Case 4
  23.         blnAlternative = m_AlternativeRowAvailable(5, Value)
  24.         blnAlternative = blnAlternative Or m_AlternativeRowAvailable(6, Value)
  25.     Case 5
  26.         blnAlternative = m_AlternativeRowAvailable(4, Value)
  27.         blnAlternative = blnAlternative Or m_AlternativeRowAvailable(6, Value)
  28.     Case 6
  29.         blnAlternative = m_AlternativeRowAvailable(4, Value)
  30.         blnAlternative = blnAlternative Or m_AlternativeRowAvailable(5, Value)
  31.     Case 7
  32.         blnAlternative = m_AlternativeRowAvailable(8, Value)
  33.         blnAlternative = blnAlternative Or m_AlternativeRowAvailable(9, Value)
  34.     Case 8
  35.         blnAlternative = m_AlternativeRowAvailable(7, Value)
  36.         blnAlternative = blnAlternative Or m_AlternativeRowAvailable(9, Value)
  37.     Case 9
  38.         blnAlternative = m_AlternativeRowAvailable(7, Value)
  39.         blnAlternative = blnAlternative Or m_AlternativeRowAvailable(8, Value)
  40.     End Select
  41.         
  42.     Select Case intCol
  43.     Case 1
  44.         blnAlternative = blnAlternative Or m_AlternativeColAvailable(2, Value)
  45.         blnAlternative = blnAlternative Or m_AlternativeColAvailable(3, Value)
  46.     Case 2
  47.         blnAlternative = blnAlternative Or m_AlternativeColAvailable(1, Value)
  48.         blnAlternative = blnAlternative Or m_AlternativeColAvailable(3, Value)
  49.     Case 3
  50.         blnAlternative = blnAlternative Or m_AlternativeColAvailable(1, Value)
  51.         blnAlternative = blnAlternative Or m_AlternativeColAvailable(2, Value)
  52.     Case 4
  53.         blnAlternative = blnAlternative Or m_AlternativeColAvailable(5, Value)
  54.         blnAlternative = blnAlternative Or m_AlternativeColAvailable(6, Value)
  55.     Case 5
  56.         blnAlternative = blnAlternative Or m_AlternativeColAvailable(4, Value)
  57.         blnAlternative = blnAlternative Or m_AlternativeColAvailable(6, Value)
  58.     Case 6
  59.         blnAlternative = blnAlternative Or m_AlternativeColAvailable(4, Value)
  60.         blnAlternative = blnAlternative Or m_AlternativeColAvailable(5, Value)
  61.     Case 7
  62.         blnAlternative = blnAlternative Or m_AlternativeColAvailable(8, Value)
  63.         blnAlternative = blnAlternative Or m_AlternativeColAvailable(9, Value)
  64.     Case 8
  65.         blnAlternative = blnAlternative Or m_AlternativeColAvailable(7, Value)
  66.         blnAlternative = blnAlternative Or m_AlternativeColAvailable(9, Value)
  67.     Case 9
  68.         blnAlternative = blnAlternative Or m_AlternativeColAvailable(7, Value)
  69.         blnAlternative = blnAlternative Or m_AlternativeColAvailable(8, Value)
  70.     End Select
  71.    
  72.     m_CanUse = blnAlternative
  73.    
  74. End Function
复制代码

TA的精华主题

TA的得分主题

发表于 2016-2-19 21:21 | 显示全部楼层
第三部分:
  1. Private Function m_CompleteGrid() As Boolean
  2. '
  3. ' Try and resolve grid
  4. '
  5.     Dim intRow As Integer
  6.     Dim intCol As Integer
  7.     Dim intIndex As Integer
  8.     Dim intSubGrid As Integer
  9.     Dim intIndexRow As Integer
  10.     Dim intIndexCol As Integer
  11.     Dim intCell As Integer
  12.     Dim intUnique As Integer
  13.     Dim intValue As Integer
  14.     Dim intCellValues() As Integer
  15.    
  16.     ' Mark cells with values that are not available
  17.     For intSubGrid = 1 To 9
  18.         For intRow = 1 To 3
  19.             For intCol = 1 To 3
  20.                 intCell = ((intRow - 1) * 3) + intCol
  21.                 intIndex = m_GetCellIndex(intSubGrid, intCell)
  22.                 If m_intGrid(intIndex, 0) <> 0 Then
  23.                     intIndexRow = ((m_GetSubGridRow(intSubGrid) - 1) * 3) + intRow
  24.                     intIndexCol = ((m_GetSubGridCol(intSubGrid) - 1) * 3) + intCol
  25.                     m_ExcludeRowValue intIndexRow, m_intGrid(intIndex, 0)
  26.                     m_ExcludeColValue intIndexCol, m_intGrid(intIndex, 0)
  27.                     m_ExcludeValue intSubGrid, m_intGrid(intIndex, 0)
  28.                 End If
  29.             Next
  30.         Next
  31.     Next
  32.    
  33.     Do While m_Unsolved()
  34.         intUnique = m_GetUniquePosition(intValue)
  35.         If intUnique > 0 Then
  36.             intIndexRow = m_WhichSubRow(intUnique)
  37.             intIndexCol = m_WhichSubCol(intUnique)
  38.             m_AssignCellValue intUnique, intValue
  39.             m_ExcludeRowValue intIndexRow, m_intGrid(intUnique, 0)
  40.             m_ExcludeColValue intIndexCol, m_intGrid(intUnique, 0)
  41.             m_ExcludeValue m_GetSubGrid(intUnique), intValue
  42.         Else
  43.             ' need to randomly pick a number
  44.             ' use cell with least amount of choice
  45.             intCell = m_GetTestCell()
  46.             If intCell > 0 Then
  47.                 m_GetTestValues intCell, intCellValues
  48.                 For intIndex = 1 To UBound(intCellValues)
  49.                     intValue = intCellValues(intIndex)
  50.                     ' test surrounding grids for alternatives
  51.                     ' if this value is used
  52.                     If m_CanUse(intCell, intValue) Then
  53.                         intIndexRow = m_WhichSubRow(intCell)
  54.                         intIndexCol = m_WhichSubCol(intCell)
  55.                         m_ExcludeRowValue intIndexRow, intValue
  56.                         m_ExcludeColValue intIndexCol, intValue
  57.                         m_AssignCellValue intCell, intValue
  58.                         m_ExcludeValue m_GetSubGrid(intCell), intValue
  59.                         Exit For
  60.                     End If
  61.                 Next
  62.             Else
  63.                 m_CompleteGrid = False
  64.                 Exit Function
  65.             End If
  66.         End If
  67.         
  68.     Loop
  69.     m_CompleteGrid = True
  70.    
  71. End Function
复制代码

TA的精华主题

TA的得分主题

发表于 2016-2-19 21:22 | 显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用  · 内置多项VBA编程加强工具       ★ 免费下载 ★      ★使用手册
第4部分:
  1. Private Sub m_ExcludeRowValue(Row As Integer, Value As Integer)
  2. '
  3. ' Exclude this value from complete row
  4. '
  5.     Dim intIndex As Integer
  6.     Dim intCell As Integer
  7.    
  8.     intIndex = ((Row - 1) * 9) + 1
  9.     For intCell = intIndex To intIndex + 9 - 1
  10.         m_intGrid(intCell, Value) = -Value
  11.     Next
  12. End Sub
  13. Private Function m_AlternativeRowAvailable(Row As Integer, Value As Integer) As Boolean
  14. '
  15. ' Check that the value has not yet been used
  16. '
  17.     Dim intIndex As Integer
  18.     Dim intCell As Integer
  19.     Dim intCount As Integer
  20.    
  21.     intIndex = ((Row - 1) * 9) + 1
  22.     For intCell = intIndex To intIndex + 9 - 1
  23.         If m_intGrid(intCell, Value) = Value Then
  24.             intCount = intCount + 1
  25.         End If
  26.     Next
  27.     m_AlternativeRowAvailable = (intCount > 1)
  28. End Function
  29. Private Function m_ValueInRow(Row As Integer, Value As Integer) As Integer
  30. '
  31. ' Return number of occurances of value in a complete row
  32. '
  33.     Dim intIndex As Integer
  34.     Dim intCell As Integer
  35.     Dim intCount As Integer
  36.    
  37.     intIndex = ((Row - 1) * 9) + 1
  38.     For intCell = intIndex To intIndex + 9 - 1
  39.         If m_intGrid(intCell, 0) = Value Then
  40.             intCount = intCount + 1
  41.         End If
  42.     Next
  43.     m_ValueInRow = intCount
  44.    
  45. End Function

  46. Private Sub m_ExcludeColValue(Col As Integer, Value As Integer)
  47. '
  48. ' Exclude this value from complete Col
  49. '
  50.     Dim intIndex As Integer
  51.     Dim intCell As Integer
  52.    
  53.     intCell = Col
  54.     For intIndex = 1 To 9
  55.         m_intGrid(intCell, Value) = -Value
  56.         intCell = intCell + 9
  57.     Next
  58.    
  59. End Sub
  60. Private Function m_AlternativeColAvailable(Col As Integer, Value As Integer) As Boolean
  61. '
  62. ' Check that the value has not yet been used
  63. '
  64.     Dim intIndex As Integer
  65.     Dim intCell As Integer
  66.     Dim intCount As Integer
  67.    
  68.     intCell = Col
  69.     For intIndex = 1 To 9
  70.         If m_intGrid(intCell, Value) = Value Then
  71.             intCount = intCount + 1
  72.         End If
  73.         intCell = intCell + 9
  74.     Next
  75.     m_AlternativeColAvailable = (intCount > 0)

  76. End Function
  77. Private Function m_ValueInCol(Col As Integer, Value As Integer) As Integer
  78. '
  79. ' Return occurances of value in complete column
  80. '
  81.     Dim intIndex As Integer
  82.     Dim intCell As Integer
  83.     Dim intCount As Integer
  84.    
  85.     intCell = Col
  86.     For intIndex = 1 To 9
  87.         If m_intGrid(intCell, 0) = Value Then
  88.             intCount = intCount + 1
  89.         End If
  90.         intCell = intCell + 9
  91.     Next
  92.     m_ValueInCol = intCount

  93. End Function

  94. Private Sub m_ExcludeValue(SubGrid As Integer, Value As Integer)
  95. '
  96. ' Exclude this value from a subgrid
  97. '
  98.     Dim intIndex As Integer
  99.     Dim intCell As Integer
  100.    
  101.     For intIndex = 1 To 9
  102.         intCell = m_GetCellIndex(SubGrid, intIndex)
  103.         m_intGrid(intCell, Value) = -Value
  104.     Next
  105.    
  106. End Sub


  107. Private Function m_GetAvailable(Cell As Integer) As Integer
  108. '
  109. ' Return number of valid numbers avaiable for specific cell
  110. '
  111.     Dim intIndex As Integer
  112.    
  113.     If m_intGrid(Cell, 0) > 0 Then
  114.         m_GetAvailable = 0
  115.         Exit Function
  116.     End If
  117.    
  118.     For intIndex = 1 To 9
  119.         If m_intGrid(Cell, intIndex) > 0 Then
  120.             m_GetAvailable = m_GetAvailable + 1
  121.         End If
  122.     Next
  123. End Function
复制代码

TA的精华主题

TA的得分主题

发表于 2016-2-19 21:22 | 显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助
第5部分:
  1. Private Sub m_GetStartingGrid()
  2. '
  3. ' Randomly select starting values
  4. '
  5.     Dim strStartValues As String
  6.     Dim intSubGrid As Integer
  7.     Dim intReveal As Integer
  8.     Dim intIndex As Integer
  9.     Dim intRow As Integer
  10.     Dim intCol As Integer
  11.     Dim intIndexRow As Integer
  12.     Dim intIndexCol As Integer
  13.     Dim intValue As Integer
  14.     Dim rngDisplay As Range
  15.     Dim rngStorage As Range
  16.    
  17.     Set rngDisplay = m_AssignDisplay()
  18.     Set rngStorage = m_AssignStorage()
  19.    
  20.     Dim intNValues As Integer
  21.     Dim intValueStack(81) As Integer
  22.     Dim intNGrids As Integer
  23.     Dim intGridStack(81) As Integer
  24.     Dim intUseSubGrid As Integer
  25.     Dim intAttemptCount As Integer
  26.    
  27.     ' create an array of 3 copies of each number
  28. Retry:
  29.     intValue = 1
  30.     intNValues = 36
  31.     intNGrids = 36
  32.     intValue = 1
  33.     For intIndex = 1 To intNValues
  34.         intValueStack(intIndex) = intValue
  35.         intGridStack(intIndex) = intValue
  36.         If intIndex Mod (intNValues / 9) = 0 Then intValue = intValue + 1
  37.     Next
  38.    
  39.     ReDim blnReveal(9, 9) As Boolean
  40.     ReDim intRevealCount(9) As Integer
  41.     Dim intRevealGrid As Integer
  42.     Dim intGameLevel As Integer
  43.    
  44.     intGameLevel = (intNValues / 9) + 1
  45.     ' get first 34 values
  46.     Do While intNGrids > 0
  47.         intUseSubGrid = m_GetRandom(1, intNGrids)
  48.         intSubGrid = intGridStack(intUseSubGrid)
  49.         If intRevealCount(intSubGrid) < intGameLevel Then
  50.             Do
  51.                 intReveal = m_GetRandom(1, intNValues)
  52.                 intValue = intValueStack(intReveal)
  53.                 If Not blnReveal(intSubGrid, intValue) Then
  54.                     blnReveal(intSubGrid, intValue) = True
  55.                     intRevealCount(intSubGrid) = intRevealCount(intSubGrid) + 1
  56.                     intValueStack(intReveal) = intValueStack(intNValues)
  57.                     intNValues = intNValues - 1
  58.                     intGridStack(intUseSubGrid) = intGridStack(intNGrids)
  59.                     intNGrids = intNGrids - 1
  60.                     intAttemptCount = 0
  61.                     Exit Do
  62.                 Else
  63.                     intAttemptCount = intAttemptCount + 1
  64.                     If intAttemptCount = 100 Then
  65.                         intAttemptCount = 0
  66.                         GoTo Retry
  67.                     End If
  68.                 End If
  69.             Loop
  70.         Else
  71.             intAttemptCount = intAttemptCount + 1
  72.         End If
  73.         If intAttemptCount = 100 Then
  74.             intAttemptCount = 0
  75.             GoTo Retry
  76.         End If
  77.     Loop
  78.    
  79.     ' get a set for remaining 5 values
  80.     intNValues = 18
  81.     intNGrids = 18
  82.     intValue = 1
  83.     For intIndex = 1 To intNValues
  84.         intValueStack(intIndex) = intValue
  85.         intGridStack(intIndex) = intValue
  86.         If intIndex Mod (intNValues / 9) = 0 Then intValue = intValue + 1
  87.     Next
  88.     ' pick subgrids and values for remaining 5 places
  89.     intGameLevel = m_GetRandom(5, 9)
  90.     Do While intNGrids > (27 - intGameLevel)
  91.         intUseSubGrid = m_GetRandom(1, intNGrids)
  92.         intSubGrid = intGridStack(intUseSubGrid)
  93.         If intRevealCount(intSubGrid) < 7 Then  ' max 6 values in single grid
  94.             Do
  95.                 intReveal = m_GetRandom(1, intNValues)
  96.                 intValue = intValueStack(intReveal)
  97.                 If Not blnReveal(intSubGrid, intValue) Then
  98.                     blnReveal(intSubGrid, intValue) = True
  99.                     intRevealCount(intSubGrid) = intRevealCount(intSubGrid) + 1
  100.                     intValueStack(intReveal) = intValueStack(intNValues)
  101.                     intNValues = intNValues - 1
  102.                     intGridStack(intUseSubGrid) = intGridStack(intNGrids)
  103.                     intNGrids = intNGrids - 1
  104.                     Exit Do
  105.                 End If
  106.             Loop
  107.         End If
  108.     Loop
  109.    
  110.     m_ProtectPuzzle False
  111.     For intSubGrid = 1 To 9
  112.         ' update storage grid by making start values BOLD
  113.         ' update display grid by making start values visisble/bold and locked
  114.         For intRow = 1 To 3
  115.             For intCol = 1 To 3
  116.                 intIndexRow = ((m_GetSubGridRow(intSubGrid) - 1) * 3) + intRow
  117.                 intIndexCol = ((m_GetSubGridCol(intSubGrid) - 1) * 3) + intCol
  118.                 intValue = rngStorage.Cells(intIndexRow, intIndexCol).Value
  119.                 If blnReveal(intSubGrid, intValue) Then
  120.                     rngStorage.Cells(intIndexRow, intIndexCol).Font.Bold = True
  121.                     With rngDisplay.Cells(intIndexRow, intIndexCol)
  122.                         .Font.Bold = True
  123.                         .Locked = True
  124.                         .Value = rngStorage.Cells(intIndexRow, intIndexCol)
  125.                     End With
  126.                 Else
  127.                     rngStorage.Cells(intIndexRow, intIndexCol).Font.Bold = False
  128.                     With rngDisplay.Cells(intIndexRow, intIndexCol)
  129.                         .Font.Bold = False
  130.                         .Locked = False
  131.                     End With
  132.                 End If
  133.             Next
  134.         Next
  135.     Next
  136.    
  137.     'release
  138.     Set rngStorage = Nothing
  139.     Set rngDisplay = Nothing
  140.    
  141. End Sub
复制代码

TA的精华主题

TA的得分主题

发表于 2016-2-19 21:23 | 显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助
第6部分:
  1. Private Function m_GetSubGrid(Cell As Integer) As Integer
  2. '
  3. ' Return SubGrid number for cell
  4. '
  5.     Dim intRow As Integer
  6.     Dim intCol As Integer
  7.    
  8.     intRow = (m_WhichSubRow(Cell) + 2) \ 3
  9.     intCol = (m_WhichSubCol(Cell) + 2) \ 3
  10.    
  11.     m_GetSubGrid = ((intRow - 1) * 3) + intCol
  12. End Function

  13. Function m_GetSubGridCol(SubGrid As Integer) As Integer
  14. ' return col number 1 to 3 of subgrid
  15.     m_GetSubGridCol = ((SubGrid - 1) Mod 3) + 1
  16. End Function

  17. Function m_GetSubGridRow(SubGrid As Integer) As Integer
  18. ' return row number 1 to 3 of subgrid
  19.     m_GetSubGridRow = (SubGrid + 2) \ 3
  20. End Function

  21. Private Function m_GetTestCell() As Integer
  22. '
  23. ' Scan grid for cell with lowest number of values available
  24. ' Check use of value in cell will not cause failure in related cells
  25. '
  26.     Dim intCell As Integer
  27.     Dim intIndex As Integer
  28.     Dim intCount As Integer
  29.     Dim intTemp As Integer
  30.     Dim intStack() As Integer
  31.     Dim intStackCount As Integer
  32.    
  33.     intCount = 9
  34.     For intCell = 1 To 81
  35.         intTemp = m_GetAvailable(intCell)
  36.         If intTemp > 0 Then
  37.             If intTemp < intCount Then
  38.                 intStackCount = 1
  39.                 ReDim intStack(intStackCount) As Integer
  40.                 intStack(intStackCount) = intCell
  41.                 intCount = intTemp
  42.             ElseIf intTemp = intCount Then
  43.                 intStackCount = intStackCount + 1
  44.                 ReDim Preserve intStack(intStackCount) As Integer
  45.                 intStack(intStackCount) = intCell
  46.             End If
  47.         End If
  48.     Next
  49.     If intStackCount > 0 Then
  50.         m_GetTestCell = intStack(m_GetRandom(1, intStackCount))
  51.     Else
  52.         m_GetTestCell = 0
  53.     End If
  54.    
  55. End Function
复制代码

TA的精华主题

TA的得分主题

发表于 2016-2-19 21:23 | 显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助
第7部分:
  1. Private Sub m_GetTestValues(Cell As Integer, Value() As Integer)
  2. '
  3. ' Return a random list of values that could be used
  4. '
  5.     Dim intCount As Integer
  6.     Dim intIndex As Integer
  7.     Dim intResort As Integer
  8.     ReDim Value(0) As Integer
  9.     Dim intValue As Integer
  10.    
  11.     For intValue = 1 To 9
  12.         If m_intGrid(Cell, intValue) > 0 Then
  13.             intCount = intCount + 1
  14.             ReDim Preserve Value(intCount) As Integer
  15.             Value(intCount) = intValue
  16.         End If
  17.     Next
  18.     If intCount = 0 Then Exit Sub
  19.    
  20.     ReDim intOrder(intCount) As Integer
  21.     For intValue = 1 To intCount
  22.         intIndex = m_GetRandom(1, intCount - intValue + 1)
  23.         intOrder(intValue) = Value(intIndex)
  24.         For intResort = 1 To intCount - intValue
  25.             If intResort >= intValue Then
  26.                 Value(intResort) = Value(intResort + 1)
  27.             Else
  28.                 Value(intResort) = Value(intResort)
  29.             End If
  30.         Next
  31.     Next
  32.     For intValue = 1 To intCount
  33.         Value(intValue) = intOrder(intValue)
  34.     Next
  35.    
  36. End Sub

  37. Private Function m_GetUniquePosition(Value As Integer) As Integer
  38. '
  39. ' Search for a cell that contains a single location for a value within a subgrid
  40. '
  41.     Dim intSubGrid As Integer
  42.     Dim intIndex As Integer
  43.     Dim intCell As Integer
  44.     Dim intCount As Integer
  45.     Dim intValue As Integer
  46.     Dim intFoundValue As Integer
  47.     Dim intFoundCell As Integer
  48.    
  49.     ' single value in cell
  50.     For intCell = 1 To 81
  51.         If m_intGrid(intCell, 0) = 0 Then
  52.             intCount = 0
  53.             For intValue = 1 To 9
  54.                 If m_intGrid(intCell, intValue) > 0 Then
  55.                     intCount = intCount + 1
  56.                     intFoundValue = intValue
  57.                 End If
  58.             Next
  59.             If intCount = 1 Then
  60.                 Value = intFoundValue
  61.                 m_GetUniquePosition = intCell
  62.                 Exit Function
  63.             End If
  64.         End If
  65.     Next
  66.    
  67.     ' single occurance of value in subgrid
  68.     For intSubGrid = 1 To 9
  69.         For intValue = 1 To 9
  70.             intCount = 0
  71.             For intIndex = 1 To 9
  72.                 intCell = m_GetCellIndex(intSubGrid, intIndex)
  73.                 If m_intGrid(intCell, intValue) = intValue Then
  74.                     intCount = intCount + 1
  75.                     intFoundCell = intCell
  76.                 End If
  77.             Next
  78.             If intCount = 1 Then
  79.                 ' found
  80.                 Value = intValue
  81.                 m_GetUniquePosition = intFoundCell
  82.                 Exit Function
  83.             End If
  84.         Next
  85.     Next
  86.     m_GetUniquePosition = 0 ' Nothing Unique
  87. End Function
复制代码

TA的精华主题

TA的得分主题

发表于 2016-2-19 21:24 | 显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助
第8部分:
  1. Private Sub m_ProtectPuzzle(LockDown As Boolean)
  2. '
  3. ' Lock sheet so starting values can not be removed
  4. '
  5.     With ThisWorkbook.Worksheets(SUDOKU_SHEET)
  6.         If LockDown Then
  7.             .Protect
  8.         Else
  9.             .Unprotect
  10.         End If
  11.     End With
  12.    
  13. End Sub
  14. Private Sub m_RevealPuzzle()
  15. '
  16. ' Transfer solution to display
  17. '
  18.     Dim rngTemp As Range
  19.     Dim rngStorage As Range
  20.     Dim rngDisplay As Range
  21.     Dim lngRow As Long
  22.     Dim intCol As Integer
  23.    
  24.     Application.StatusBar = SUDOKU_MSG_REVEALPUZZLE

  25.     Set rngStorage = m_AssignStorage()
  26.     Set rngDisplay = m_AssignDisplay()
  27.    
  28.     m_ProtectPuzzle False
  29.     For lngRow = 1 To 9
  30.         For intCol = 1 To 9
  31.             rngDisplay.Cells(lngRow, intCol) = rngStorage.Cells(lngRow, intCol)
  32.         Next
  33.     Next
  34.     m_ProtectPuzzle True
  35.    
  36.     ' release
  37.     Set rngStorage = Nothing
  38.     Set rngDisplay = Nothing
  39.         
  40. End Sub
  41. Private Function m_IsUserPuzzle() As Boolean
  42. '
  43. ' Check numbers in current display grid match
  44. ' those in storage grid. If so then the solution
  45. ' to the grid is already know otherwise
  46. ' assume numbers are entered by user for solving
  47. '
  48.     Dim rngTemp As Range
  49.     Dim rngStorage As Range
  50.     Dim rngDisplay As Range
  51.     Dim lngRow As Long
  52.     Dim intCol As Integer
  53.    
  54.     Set rngStorage = m_AssignStorage()
  55.     Set rngDisplay = m_AssignDisplay()
  56.    
  57.     For lngRow = 1 To 9
  58.         For intCol = 1 To 9
  59.             If rngDisplay.Cells(lngRow, intCol) <> "" Then
  60.                 If rngDisplay.Cells(lngRow, intCol) <> rngStorage.Cells(lngRow, intCol) Then
  61.                     m_IsUserPuzzle = True
  62.                     Exit For
  63.                 End If
  64.             End If
  65.             If rngStorage.Cells(lngRow, intCol) = "" Then
  66.                 m_IsUserPuzzle = True
  67.                 Exit For
  68.             End If
  69.         Next
  70.     Next

  71.     ' release
  72.     Set rngStorage = Nothing
  73.     Set rngDisplay = Nothing
  74.    
  75. End Function
复制代码

TA的精华主题

TA的得分主题

发表于 2016-2-19 21:25 | 显示全部楼层
第9部分:
  1. Private Sub m_ReadPuzzle()
  2. '
  3. ' Load number from storage grid
  4. '
  5.     Dim intCell As Integer
  6.     Dim rngGrid As Range
  7.     Dim rngTemp As Range
  8.    
  9.     Set rngGrid = m_AssignStorage()
  10.    
  11.     For Each rngTemp In rngGrid
  12.         intCell = intCell + 1
  13.         If rngTemp <> "" Then
  14.             If rngTemp.Font.Bold Then
  15.                 m_AssignCellValue intCell, rngTemp.Value
  16.             End If
  17.         End If
  18.     Next
  19.    
  20.     ' release
  21.     Set rngGrid = Nothing
  22.    
  23. End Sub

  24. Private Sub m_ResetPuzzleArrays()
  25. '
  26. ' Initialize variables
  27. '
  28.     Dim intCell As Integer
  29.     Dim intIndex As Integer
  30.    
  31.     For intCell = 1 To 81
  32.         For intIndex = 0 To 9
  33.             m_intGrid(intCell, intIndex) = intIndex
  34.         Next
  35.     Next
  36.    
  37.     ' Which values have been used in each subgrid
  38.     For intCell = 1 To 9
  39.         For intIndex = 1 To 9
  40.             m_intSubGrid(intCell, intIndex) = -intIndex
  41.         Next
  42.     Next
  43.    
  44. End Sub

  45. Private Function m_SanityCheck() As Boolean
  46. '
  47. ' Check puzzle for possible errors when user entered values
  48. '
  49.     Dim blnOk As Boolean
  50.     Dim intCell As Integer
  51.     Dim intRow As Integer
  52.     Dim intCol As Integer
  53.     Dim intIndex As Integer
  54.     Dim intSubGrid As Integer
  55.     Dim intValue As Integer
  56.     Dim intGridCell As Integer
  57.     Dim intCellIndex As Integer
  58.     Dim intCount As Integer
  59.     Dim intCheckCell As Integer
  60.    
  61.     ' check for duplicate values across rows and columns
  62.     For intCell = 1 To 81
  63.         If m_intGrid(intCell, 0) > 0 Then
  64.             If m_ValueInUse(intCell, m_intGrid(intCell, 0)) Then
  65.                 intRow = m_WhichSubRow(intCell)
  66.                 intCol = m_WhichSubCol(intCell)
  67.                 MsgBox "Duplicate entry of Value " & m_intGrid(intCell, 0) & " in Row " & intRow & ", Column " & intCol, vbExclamation
  68.                 m_SanityCheck = False
  69.                 Exit Function
  70.             End If
  71.         End If
  72.     Next
  73.    
  74.     'check for duplicate values within subgrids
  75.     For intSubGrid = 1 To 9
  76.         For intGridCell = 1 To 9
  77.             intIndex = m_GetCellIndex(intSubGrid, intGridCell)
  78.             intValue = m_intGrid(intIndex, 0)
  79.             If intValue > 0 Then
  80.                 intCount = 0
  81.                 For intCheckCell = 1 To 9
  82.                     intCellIndex = m_GetCellIndex(intSubGrid, intCheckCell)
  83.                     If m_intGrid(intCellIndex, 0) = intValue Then intCount = intCount + 1
  84.                 Next
  85.                 If intCount > 1 Then
  86.                     intRow = m_WhichSubRow(intIndex)
  87.                     intCol = m_WhichSubCol(intIndex)
  88.                     MsgBox "Duplicate entry of Value " & m_intGrid(intIndex, 0) & " in Row " & intRow & ", Column " & intCol, vbExclamation
  89.                     Exit Function
  90.                 End If
  91.             End If
  92.         Next
  93.     Next
  94.    
  95.     m_SanityCheck = True
  96.    
  97. End Function
复制代码
您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

手机版|关于我们|联系我们|ExcelHome

GMT+8, 2024-11-17 00:17 , Processed in 0.045367 second(s), 12 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

沪公网安备 31011702000001号 沪ICP备11019229号-2

本论坛言论纯属发表者个人意见,任何违反国家相关法律的言论,本站将协助国家相关部门追究发言者责任!     本站特聘法律顾问:李志群律师

快速回复 返回顶部 返回列表