|
第13部分:
- Private Function m_Unsolved() As Boolean
- '
- ' Test for cells that contain more
- '
- Dim intCell As Integer
-
- For intCell = 1 To 81
- If m_intGrid(intCell, 0) = 0 Then
- m_Unsolved = True
- Exit For
- End If
- Next
-
- End Function
- Private Function m_WhichSubRow(Cell As Integer) As Integer
- ' return grid row from given index
- m_WhichSubRow = ((Cell - 1) \ 9) + 1
- End Function
- Private Function m_WhichSubCol(Cell As Integer) As Integer
- ' return grid col from given index
- Dim intIndex As Integer
-
- intIndex = Cell
- Do While intIndex > 9
- intIndex = intIndex - 9
- Loop
- m_WhichSubCol = intIndex
-
- End Function
- Private Function m_GetCellIndex(SubGrid As Integer, Cell As Integer) As Integer
- '
- ' Return Offset position of cell give subcell and cell position
- '
- Dim intRow As Integer
- Dim intCol As Integer
- Dim intSubRow As Integer
- Dim intSubCol As Integer
-
- intRow = (Cell + 2) \ 3
- intCol = ((Cell - 1) Mod 3) + 1
-
- intSubRow = m_GetSubGridRow(SubGrid)
- intSubCol = m_GetSubGridCol(SubGrid)
-
- m_GetCellIndex = ((intSubRow - 1) * 27) + ((intRow - 1) * 9) + ((intSubCol - 1) * 3) + intCol
-
- End Function
- Sub m_PopulateSubGrid(Index As Integer)
- '
- ' Randomly fill as 3x3 grid with
- '
- Dim intIndex As Integer
- Dim intOrder(9) As Integer
- Dim intNumber(9) As Integer
- Dim intCell As Integer
- Dim intLoop As Integer
- Dim strValue As String
- Dim intSlot As Integer
-
- strValue = "123456789"
- For intIndex = 1 To 9
- intSlot = m_GetRandom(1, 9 - intIndex + 1)
- intOrder(intIndex) = Int(Mid(strValue, intSlot, 1))
- If intSlot > 1 Then
- strValue = Left(strValue, intSlot - 1) & Mid(strValue, intSlot + 1)
- Else
- strValue = Mid(strValue, intSlot + 1)
- End If
- Next
- strValue = "123456789"
- For intIndex = 1 To 9
- intSlot = m_GetRandom(1, 9 - intIndex + 1)
- intNumber(intIndex) = Int(Mid(strValue, intSlot, 1))
- If intSlot > 1 Then
- strValue = Left(strValue, intSlot - 1) & Mid(strValue, intSlot + 1)
- Else
- strValue = Mid(strValue, intSlot + 1)
- End If
- Next
-
- For intIndex = 1 To 9
- intCell = m_GetCellIndex(Index, intIndex)
- For intLoop = 1 To 9
- m_intGrid(intCell, intLoop) = -intLoop
- Next
- m_AssignCellValue intCell, intNumber(intIndex)
- Next
-
- End Sub
复制代码 |
|