Advertisement
Guest User

Untitled

a guest
Apr 25th, 2019
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Private Sub cmdGo_Click()
  2.  
  3.     Dim bytFile() As Byte
  4.     Dim lngResult As Long
  5.     Dim lngSize As Long
  6.     Dim Words() As Long
  7.     Dim lngIdx As Long
  8.     Dim lngWordCnt As Long
  9.     Dim lngErr As Long
  10.     Dim lngWordSize As Long
  11.     Dim lngWordStart As Long
  12.     Dim bytLetters() As Byte
  13.     Dim lngLetterCnt As Long
  14.     Dim lngIdx2 As Long
  15.     Dim lngIdx3 As Long
  16.     Dim lngUsedLetterFlags() As Long
  17.     Dim lngUsedLetterCount As Long
  18.     Dim lngPossibleWordCnt As Long
  19.     Dim lngMaxWordSize As Long
  20.     Dim strword As String
  21.    
  22.     ' Load the file
  23.    If GetEntireFile(App.Path & "\Dict.txt", bytFile, lngSize, lngErr) = False Then
  24.         MsgBox "Failed to load Dict.txt. Error " & lngErr & "occured. Process aborted."
  25.         Exit Sub
  26.     End If
  27.    
  28.     ' Check Input
  29.    bytLetters = StrConv(txtLetters.Text, vbFromUnicode)
  30.     lngLetterCnt = Len(txtLetters.Text)
  31.    
  32.     If lngLetterCnt <> 7 Then
  33.         MsgBox "Enter exactaly 7 letters.": Exit Sub
  34.     End If
  35.    
  36.     ' Count the number of words.
  37.    ' -1 for zero based index, -1 to not search last character eliminating the need for checks.
  38.    For lngIdx = 0 To lngSize - 2
  39.         If bytFile(lngIdx) = CHAR_CR And bytFile(lngIdx + 1) = CHAR_LF Then
  40.             lngWordCnt = lngWordCnt + 1
  41.             lngIdx = lngIdx + 1
  42.         End If
  43.     Next
  44.    
  45.     'Setup the array and populate the word table
  46.    ReDim Words(0 To 3, 0 To lngWordCnt - 1)
  47.    
  48.     lngWordSize = 0
  49.     lngWordCnt = 0
  50.     lngWordStart = 0
  51.    
  52.     For lngIdx = 0 To lngSize - 2
  53.                  
  54.         If lngWordSize = 0 Then lngWordStart = lngIdx
  55.         lngWordSize = lngWordSize + 1
  56.                
  57.         If bytFile(lngIdx) = CHAR_CR And bytFile(lngIdx + 1) = CHAR_LF Then
  58.            
  59.             ' -1 because we have found the CR which isn't included in the word
  60.            lngWordSize = lngWordSize - 1
  61.             Words(WORD_SIZE, lngWordCnt) = lngWordSize
  62.             Words(WORD_START, lngWordCnt) = lngWordStart
  63.             lngWordCnt = lngWordCnt + 1
  64.             If lngWordSize > lngMaxWordSize Then lngMaxWordSize = lngWordSize
  65.             lngWordSize = 0
  66.             lngIdx = lngIdx + 1
  67.            
  68.         End If
  69.     Next
  70.  
  71.    
  72.     ReDim lngUsedLetterFlags(0 To lngMaxWordSize - 1)
  73.    
  74.     For lngIdx = 0 To lngWordCnt - 1
  75.         lngWordStart = Words(WORD_START, lngIdx)
  76.         lngWordSize = Words(WORD_SIZE, lngIdx)
  77.        
  78.         For lngIdx2 = 0 To 6 ' Letters in scrabble hand
  79.            For lngIdx3 = 0 To lngWordSize - 1 'Letters in word
  80.                If bytFile(lngIdx3 + lngWordStart) = bytLetters(lngIdx2) And lngUsedLetterFlags(lngIdx3) <> LETTER_EXISTS Then
  81.                     lngUsedLetterFlags(lngIdx3) = LETTER_EXISTS
  82.                     ' get out once we've used this letter
  83.                    Exit For
  84.                 End If
  85.             Next lngIdx3
  86.         Next lngIdx2
  87.        
  88.         'Count up how many letters are used vs count of matches
  89.        For lngIdx2 = 0 To lngWordSize - 1
  90.             If lngUsedLetterFlags(lngIdx2) = LETTER_EXISTS Then
  91.                 lngUsedLetterCount = lngUsedLetterCount + 1
  92.             End If
  93.         Next lngIdx2
  94.        
  95.         ' Were all the letters of this word used? its a match!
  96.        If lngUsedLetterCount = lngWordSize Then
  97.             Words(WORD_POSSIBLE, lngIdx) = WORD_POSSIBLE
  98.             lngPossibleWordCnt = lngPossibleWordCnt + 1
  99.            
  100.             'For lngIdx2 = lngWordStart To lngWordSize + lngWordStart
  101.            '    strword = strword & Chr$(bytFile(lngIdx2))
  102.            'Next
  103.            
  104.             'strword = strword & vbCrLf
  105.        End If
  106.        
  107.         'Clear the array
  108.        ReDim lngUsedLetterFlags(0 To lngMaxWordSize - 1)
  109.         lngUsedLetterCount = 0
  110.    
  111.     Next lngIdx
  112.    
  113.     MsgBox lngPossibleWordCnt & " possible words."
  114.    
  115.     'Debug.Print strword
  116.    
  117. End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement