ExcelHome技术论坛

 找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

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

[分享] 数独游戏

[复制链接]

TA的精华主题

TA的得分主题

发表于 2016-2-19 21:25 | 显示全部楼层
第10部分:
  1. Private Function m_ValueInUse(Cell As Integer, Value As Integer) As Boolean
  2. '
  3. ' Check whether value has already been used in same row or column
  4. ' use this before trying to solve a user puzzle
  5. '
  6.     Dim intRow As Integer
  7.     Dim intCol As Integer
  8.    
  9.     intRow = m_WhichSubRow(Cell)
  10.     intCol = m_WhichSubCol(Cell)
  11.         
  12.     m_ValueInUse = (m_ValueInRow(intRow, Value) + m_ValueInCol(intCol, Value)) > 2
  13.    
  14.    
  15. End Function

  16. Sub sudoku_About()
  17. '
  18. ' Display about info on Su Doku.
  19. '
  20.     Dim strMsg As String
  21.    
  22.     strMsg = SUDOKU_TITLE & SUDOKU_VERSION & " Written by " & SUDOKU_AUTHOR & vbLf & vbLf
  23.     strMsg = strMsg & "Rules:" & vbLf
  24.     strMsg = strMsg & "To solve a Su Doku puzzle every digit from 1 to 9 must appear in " & vbLf
  25.     strMsg = strMsg & "each of the 9 columns and 9 rows" & vbLf
  26.     strMsg = strMsg & "AND within each of the 9 boxes." & vbLf & vbLf
  27.     strMsg = strMsg & "You can generate random puzzles or solve user entered puzzles." & vbLf & vbLf
  28.     strMsg = strMsg & "Clear Grid - " & vbTab & vbTab & "clear the contents of the grid" & vbLf
  29.     strMsg = strMsg & "New Puzzle -" & vbTab & vbTab & "generates a unique puzzle" & vbLf
  30.     strMsg = strMsg & "Reveal Puzzle -" & vbTab & vbTab & "reveals solution to puzzle" & vbLf
  31.     strMsg = strMsg & "Solve YOUR Puzzle -" & vbTab & "attempts to solve puzzle entered by you" & vbLf

  32.     MsgBox strMsg, vbInformation, "About " & SUDOKU_TITLE
  33.    
  34. End Sub
  35. Private Sub m_ClearDisplay()
  36. '
  37. ' Clear display
  38. '
  39.     Dim rngGrid As Range
  40.    
  41.     m_ProtectPuzzle False
  42.    
  43.     ' clear display
  44.     Set rngGrid = m_AssignDisplay()
  45.     With rngGrid
  46.         .ClearContents
  47.         .Font.Bold = False
  48.         .Locked = False
  49.     End With
  50.    
  51.     m_ProtectPuzzle True
  52.         
  53. End Sub
  54. Sub sudoku_ClearPuzzle()
  55. '
  56. ' Clear puzzle grids and leave grid ready to receive puzzle
  57. '
  58.     m_ClearDisplay
  59.     m_ClearStorage
  60.     m_ProtectPuzzle True
  61.    
  62. End Sub
  63. Private Sub m_ClearStorage()
  64. '
  65. ' Clear storage grid
  66. '
  67.     Dim rngGrid As Range
  68.    
  69.     m_ProtectPuzzle False
  70.    
  71.     ' clear grid
  72.     Set rngGrid = m_AssignStorage()
  73.     With rngGrid
  74.         .ClearContents
  75.         .Font.Bold = False
  76.         .Locked = False
  77.     End With
  78.    
  79.     m_ProtectPuzzle True
  80.         
  81. End Sub
复制代码

TA的精华主题

TA的得分主题

发表于 2016-2-19 21:26 | 显示全部楼层
第11部分:
  1. Sub sudoku_Puzzle()
  2. '
  3. ' Generate a puzzle
  4. '
  5.     Dim intRow As Integer
  6.     Dim intCol As Integer
  7.     Dim intIndex As Integer
  8.     Dim intCell As Integer
  9.     Dim lngRow As Long
  10.     Dim rngTemp As Range
  11.     Dim rngGrid As Range
  12.    
  13.     Application.ScreenUpdating = False
  14.     Application.StatusBar = SUDOKU_MSG_BUILDPUZZLE
  15.    
  16.     Do
  17.         sudoku_ClearPuzzle                  ' clear display and storage areas
  18.         m_ResetPuzzleArrays                 ' reset variables
  19.         m_ProtectPuzzle False
  20.         Randomize                           ' set random number generator
  21.         m_StartingGrid                      ' begin grid creation
  22.     Loop While Not m_CompleteGrid()         ' complete the solution to this grid
  23.    
  24.     ' output numbers to storage grid
  25.     Set rngGrid = m_AssignStorage
  26.     For Each rngTemp In rngGrid
  27.         intCell = intCell + 1
  28.         If m_intGrid(intCell, 0) <> 0 Then
  29.             rngTemp = m_intGrid(intCell, 0)
  30.         End If
  31.     Next
  32.    
  33.     m_GetUniqueSolution       ' random select starting values
  34.    
  35.     Set rngGrid = m_AssignDisplay
  36.     With rngGrid
  37.         .HorizontalAlignment = xlCenter
  38.         .VerticalAlignment = xlCenter
  39.     End With
  40.     m_ProtectPuzzle True        ' lock starting cells
  41.    
  42.     Application.ScreenUpdating = True
  43.     Application.StatusBar = False
  44.    
  45. End Sub
  46. Sub sudoku_RevealPuzzle()
  47. '
  48. ' show solution to puzzle or solve user supplied puzzle
  49. '
  50.     Dim lngChoice As Long
  51.    
  52.     Application.ScreenUpdating = False
  53.    
  54.     If Not m_IsUserPuzzle() Then
  55.         ' The grid matches current solution so simply
  56.         ' transfer solution
  57.         m_RevealPuzzle
  58.     Else
  59.         lngChoice = MsgBox(SUDOKU_MSG_USERPUZZLE, vbYesNoCancel Or vbQuestion)
  60.         If lngChoice = vbYes Then
  61.             ' confirm this is what they want
  62.             m_SolvePuzzle
  63.         ElseIf lngChoice = vbNo Then
  64.             m_RevealPuzzle
  65.         End If
  66.     End If

  67.     Application.ScreenUpdating = True
  68.     Application.StatusBar = False
  69.    
  70. End Sub
  71. Sub sudoku_SolvePuzzle()
  72. '
  73. ' solve user entered grid
  74. '
  75.     Application.ScreenUpdating = False
  76.     Application.StatusBar = SUDOKU_MSG_SOLVEPUZZLE
  77.    
  78.     m_SolvePuzzle
  79.    
  80.     Application.ScreenUpdating = True
  81.     Application.StatusBar = False
  82.    
  83. End Sub
复制代码

TA的精华主题

TA的得分主题

发表于 2016-2-19 21:27 | 显示全部楼层
第12部分:
  1. Private Sub m_SolvePuzzle()
  2. '
  3. ' Attempt 3 times to solve puzzle before prompting for more attempts
  4. '
  5.     Dim intAttempts As Integer
  6.     Dim rngTemp As Range
  7.     Dim rngStorage As Range
  8.     Dim rngDisplay As Range
  9.     Dim lngRow As Long
  10.     Dim intCol As Integer
  11.     Dim intCell As Integer
  12.    
  13.     m_ClearStorage          ' store puzzle so we can do multiple attempts
  14.     m_ProtectPuzzle False
  15.    
  16.     Set rngStorage = m_AssignStorage()
  17.     Set rngDisplay = m_AssignDisplay()
  18.    
  19.     ' store users puzzle
  20.     For lngRow = 1 To 9
  21.         For intCol = 1 To 9
  22.             If rngDisplay.Cells(lngRow, intCol).Value <> "" Then
  23.                 rngDisplay.Cells(lngRow, intCol).Font.Bold = True
  24.                 With rngStorage.Cells(lngRow, intCol)
  25.                     .Font.Bold = True
  26.                     .Value = rngDisplay.Cells(lngRow, intCol).Value
  27.                 End With
  28.             Else
  29.                 rngDisplay.Cells(lngRow, intCol).Font.Bold = False
  30.                 With rngStorage.Cells(lngRow, intCol)
  31.                     .Font.Bold = False
  32.                     .Value = rngDisplay.Cells(lngRow, intCol).Value
  33.                 End With
  34.             End If
  35.         Next
  36.     Next
  37.    
  38.     ' so try solving this puzzle
  39.     Do
  40.         intAttempts = intAttempts + 1
  41.         If intAttempts > 10 Then
  42.             ' check with user thats its ok to continue trying to solve this
  43.             If MsgBox(SUDOKU_MSG_CONTINUESOLVING, vbExclamation Or vbYesNo) = vbNo Then
  44.                 Exit Sub
  45.             End If
  46.             intAttempts = 1
  47.         End If
  48.         
  49.         m_ResetPuzzleArrays
  50.         Randomize
  51.         m_ReadPuzzle
  52.    
  53.         If Not m_SanityCheck() Then Exit Sub
  54.    
  55.     Loop While Not m_CompleteGrid()         ' complete the solution to this grid

  56.     ' output numbers to storage grid
  57.     For Each rngTemp In rngStorage
  58.         intCell = intCell + 1
  59.         If m_intGrid(intCell, 0) <> 0 Then
  60.             rngTemp = m_intGrid(intCell, 0)
  61.         End If
  62.     Next
  63.    
  64.     m_RevealPuzzle
  65.    
  66. End Sub
复制代码

TA的精华主题

TA的得分主题

发表于 2016-2-19 21:28 | 显示全部楼层
第13部分:
  1. Private Function m_Unsolved() As Boolean
  2. '
  3. ' Test for cells that contain more
  4. '
  5.     Dim intCell As Integer
  6.    
  7.     For intCell = 1 To 81
  8.         If m_intGrid(intCell, 0) = 0 Then
  9.             m_Unsolved = True
  10.             Exit For
  11.         End If
  12.     Next
  13.    
  14. End Function

  15. Private Function m_WhichSubRow(Cell As Integer) As Integer
  16. ' return grid row from given index
  17.     m_WhichSubRow = ((Cell - 1) \ 9) + 1
  18. End Function
  19. Private Function m_WhichSubCol(Cell As Integer) As Integer
  20. ' return grid col from given index
  21.     Dim intIndex As Integer
  22.    
  23.     intIndex = Cell
  24.     Do While intIndex > 9
  25.         intIndex = intIndex - 9
  26.     Loop
  27.     m_WhichSubCol = intIndex
  28.    
  29. End Function

  30. Private Function m_GetCellIndex(SubGrid As Integer, Cell As Integer) As Integer
  31. '
  32. ' Return Offset position of cell give subcell and cell position
  33. '
  34.     Dim intRow As Integer
  35.     Dim intCol As Integer
  36.     Dim intSubRow As Integer
  37.     Dim intSubCol As Integer
  38.    
  39.     intRow = (Cell + 2) \ 3
  40.     intCol = ((Cell - 1) Mod 3) + 1
  41.    
  42.     intSubRow = m_GetSubGridRow(SubGrid)
  43.     intSubCol = m_GetSubGridCol(SubGrid)
  44.    
  45.     m_GetCellIndex = ((intSubRow - 1) * 27) + ((intRow - 1) * 9) + ((intSubCol - 1) * 3) + intCol
  46.    
  47. End Function

  48. Sub m_PopulateSubGrid(Index As Integer)
  49. '
  50. ' Randomly fill as 3x3 grid with
  51. '
  52.     Dim intIndex As Integer
  53.     Dim intOrder(9) As Integer
  54.     Dim intNumber(9) As Integer
  55.     Dim intCell As Integer
  56.     Dim intLoop As Integer
  57.     Dim strValue As String
  58.     Dim intSlot As Integer
  59.    
  60.     strValue = "123456789"
  61.     For intIndex = 1 To 9
  62.         intSlot = m_GetRandom(1, 9 - intIndex + 1)
  63.         intOrder(intIndex) = Int(Mid(strValue, intSlot, 1))
  64.         If intSlot > 1 Then
  65.             strValue = Left(strValue, intSlot - 1) & Mid(strValue, intSlot + 1)
  66.         Else
  67.             strValue = Mid(strValue, intSlot + 1)
  68.         End If
  69.     Next
  70.     strValue = "123456789"
  71.     For intIndex = 1 To 9
  72.         intSlot = m_GetRandom(1, 9 - intIndex + 1)
  73.         intNumber(intIndex) = Int(Mid(strValue, intSlot, 1))
  74.         If intSlot > 1 Then
  75.             strValue = Left(strValue, intSlot - 1) & Mid(strValue, intSlot + 1)
  76.         Else
  77.             strValue = Mid(strValue, intSlot + 1)
  78.         End If
  79.     Next
  80.    
  81.     For intIndex = 1 To 9
  82.         intCell = m_GetCellIndex(Index, intIndex)
  83.         For intLoop = 1 To 9
  84.             m_intGrid(intCell, intLoop) = -intLoop
  85.         Next
  86.         m_AssignCellValue intCell, intNumber(intIndex)
  87.     Next
  88.    
  89. End Sub
复制代码

TA的精华主题

TA的得分主题

发表于 2016-2-19 21:29 | 显示全部楼层
第14部分:
  1. Private Sub m_StartingGrid()
  2. '
  3. ' Populate 3 sub grids on unique rows and columns
  4. ' this will give us a good starting point as NONE on the
  5. ' random values will be incorrect
  6. '
  7.     Dim intIndex As Integer
  8.     Dim intLoop As Integer
  9.     Dim intGrid As Integer
  10.     Dim strSubGrids As String
  11.    
  12.     intGrid = m_GetRandom(1, 6)
  13.     Select Case intGrid
  14.     Case 1
  15.         strSubGrids = "159"
  16.     Case 2
  17.         strSubGrids = "168"
  18.     Case 3
  19.         strSubGrids = "249"
  20.     Case 4
  21.         strSubGrids = "267"
  22.     Case 5
  23.         strSubGrids = "348"
  24.     Case 6
  25.         strSubGrids = "357"
  26.     End Select
  27.    
  28.     For intIndex = 1 To 3
  29.         intGrid = CInt(Mid(strSubGrids, intIndex, 1))
  30.         m_PopulateSubGrid intGrid
  31.     Next

  32. End Sub
  33. Private Function m_GetRandom(Low As Integer, High As Integer) As Integer
  34. '
  35. ' Return a random integer between and including Low and High
  36. '
  37.     If High - Low <= 0 Then
  38.         m_GetRandom = Low
  39.         Exit Function
  40.     End If
  41.     m_GetRandom = Int(Rnd * (High - Low + 1)) + Low
  42. End Function
复制代码

TA的精华主题

TA的得分主题

发表于 2016-2-19 21:29 | 显示全部楼层
第15部分:
  1. Private Sub m_GetUniqueSolution()

  2.     Dim rngDisplay As Range
  3.     Dim rngStorage As Range
  4.     Dim rngAnswer As Range
  5.     Dim intTestCount As Integer
  6.     Dim lngRow As Long
  7.     Dim intCol As Integer
  8.     Dim blnMatch As Boolean
  9.     Dim strProblem As String
  10.     Dim intTestPuzzle As Integer
  11.     Dim blnOk As Boolean
  12.     Dim blnUnique As Boolean
  13.     Dim intTested As Integer
  14.    
  15.     Application.ScreenUpdating = False
  16.    
  17.     Set rngDisplay = m_AssignDisplay()
  18.     Set rngAnswer = m_AssignStorage()
  19.     Set rngStorage = ThisWorkbook.Worksheets(SUDOKU_SHEET).Range(SUDOKU_STORAGEGRID).Offset(0, 11)
  20.    
  21.     intTested = 25 ' number of test solution tried before puzzle is deemed to be unique
  22.    
  23.     Do While Not blnUnique
  24.    
  25.         
  26.         blnUnique = True
  27.         m_GetStartingGrid       ' random select starting values
  28.         rngAnswer.Copy rngStorage
  29.         
  30.         For intTestCount = 1 To intTested
  31.             m_ProtectPuzzle False
  32.             rngDisplay.ClearContents
  33.             For lngRow = 1 To 9
  34.                 For intCol = 1 To 9
  35.                     If rngStorage.Cells(lngRow, intCol).Font.Bold Then
  36.                         rngDisplay.Cells(lngRow, intCol).Value = rngStorage.Cells(lngRow, intCol).Value
  37.                         rngDisplay.Cells(lngRow, intCol).Font.Bold = True
  38.                     End If
  39.                 Next
  40.             Next
  41.             
  42.             m_SolvePuzzle

  43.             blnMatch = True
  44.             strProblem = ""
  45.             For lngRow = 1 To 9
  46.                 For intCol = 1 To 9
  47.                     If rngStorage.Cells(lngRow, intCol).Value <> rngDisplay.Cells(lngRow, intCol) Then
  48.                         blnMatch = False
  49.                         blnUnique = False
  50.                         Exit For
  51.                     End If
  52.                 Next
  53.                 If Not blnMatch Then Exit For
  54.             Next
  55.             If Not blnMatch Then Exit For
  56.         Next
  57.    
  58.     Loop

  59.     ' Sort out Display with unique solution
  60.     m_ProtectPuzzle False
  61.     rngDisplay.ClearContents
  62.     For lngRow = 1 To 9
  63.         For intCol = 1 To 9
  64.             If rngStorage.Cells(lngRow, intCol).Font.Bold Then
  65.                 rngDisplay.Cells(lngRow, intCol).Value = rngStorage.Cells(lngRow, intCol).Value
  66.                 rngDisplay.Cells(lngRow, intCol).Font.Bold = True
  67.             End If
  68.         Next
  69.     Next
  70.     rngStorage.ClearContents
  71.     Application.ScreenUpdating = True

  72. End Sub
复制代码

TA的精华主题

TA的得分主题

 楼主| 发表于 2016-2-19 21:36 | 显示全部楼层
谢谢,我晕啦。能否直接一点。你直接发张破解后的数独答案,大家共享下。

TA的精华主题

TA的得分主题

发表于 2016-2-19 21:38 | 显示全部楼层
甩x附x件x:

13A 数独.rar

50.2 KB, 下载次数: 131

评分

2

查看全部评分

TA的精华主题

TA的得分主题

发表于 2018-10-10 22:37 | 显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用  · 内置多项VBA编程加强工具       ★ 免费下载 ★      ★使用手册
http://club.excelhome.net/thread-1433335-1-1.html
去下载一个VB版的可以逻辑解此题(功能更强),VBA版也可以

TA的精华主题

TA的得分主题

发表于 2018-10-10 22:38 | 显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用  · 内置多项VBA编程加强工具       ★ 免费下载 ★      ★使用手册
最终解如下
812753649943682175675491283154237896369845721287169534521974368438526917796318452
您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

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

GMT+8, 2025-1-16 20:17 , Processed in 0.044597 second(s), 9 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

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

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

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