Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Private Sub cmdGo_Click()
- Dim bytFile() As Byte
- Dim lngResult As Long
- Dim lngSize As Long
- Dim Words() As Long
- Dim lngIdx As Long
- Dim lngWordCnt As Long
- Dim lngErr As Long
- Dim lngWordSize As Long
- Dim lngWordStart As Long
- Dim bytLetters() As Byte
- Dim lngLetterCnt As Long
- Dim lngIdx2 As Long
- Dim lngIdx3 As Long
- Dim lngUsedLetterFlags() As Long
- Dim lngUsedLetterCount As Long
- Dim lngPossibleWordCnt As Long
- Dim lngMaxWordSize As Long
- Dim strword As String
- ' Load the file
- If GetEntireFile(App.Path & "\Dict.txt", bytFile, lngSize, lngErr) = False Then
- MsgBox "Failed to load Dict.txt. Error " & lngErr & "occured. Process aborted."
- Exit Sub
- End If
- ' Check Input
- bytLetters = StrConv(txtLetters.Text, vbFromUnicode)
- lngLetterCnt = Len(txtLetters.Text)
- If lngLetterCnt <> 7 Then
- MsgBox "Enter exactaly 7 letters.": Exit Sub
- End If
- ' Count the number of words.
- ' -1 for zero based index, -1 to not search last character eliminating the need for checks.
- For lngIdx = 0 To lngSize - 2
- If bytFile(lngIdx) = CHAR_CR And bytFile(lngIdx + 1) = CHAR_LF Then
- lngWordCnt = lngWordCnt + 1
- lngIdx = lngIdx + 1
- End If
- Next
- 'Setup the array and populate the word table
- ReDim Words(0 To 3, 0 To lngWordCnt - 1)
- lngWordSize = 0
- lngWordCnt = 0
- lngWordStart = 0
- For lngIdx = 0 To lngSize - 2
- If lngWordSize = 0 Then lngWordStart = lngIdx
- lngWordSize = lngWordSize + 1
- If bytFile(lngIdx) = CHAR_CR And bytFile(lngIdx + 1) = CHAR_LF Then
- ' -1 because we have found the CR which isn't included in the word
- lngWordSize = lngWordSize - 1
- Words(WORD_SIZE, lngWordCnt) = lngWordSize
- Words(WORD_START, lngWordCnt) = lngWordStart
- lngWordCnt = lngWordCnt + 1
- If lngWordSize > lngMaxWordSize Then lngMaxWordSize = lngWordSize
- lngWordSize = 0
- lngIdx = lngIdx + 1
- End If
- Next
- ReDim lngUsedLetterFlags(0 To lngMaxWordSize - 1)
- For lngIdx = 0 To lngWordCnt - 1
- lngWordStart = Words(WORD_START, lngIdx)
- lngWordSize = Words(WORD_SIZE, lngIdx)
- For lngIdx2 = 0 To 6 ' Letters in scrabble hand
- For lngIdx3 = 0 To lngWordSize - 1 'Letters in word
- If bytFile(lngIdx3 + lngWordStart) = bytLetters(lngIdx2) And lngUsedLetterFlags(lngIdx3) <> LETTER_EXISTS Then
- lngUsedLetterFlags(lngIdx3) = LETTER_EXISTS
- ' get out once we've used this letter
- Exit For
- End If
- Next lngIdx3
- Next lngIdx2
- 'Count up how many letters are used vs count of matches
- For lngIdx2 = 0 To lngWordSize - 1
- If lngUsedLetterFlags(lngIdx2) = LETTER_EXISTS Then
- lngUsedLetterCount = lngUsedLetterCount + 1
- End If
- Next lngIdx2
- ' Were all the letters of this word used? its a match!
- If lngUsedLetterCount = lngWordSize Then
- Words(WORD_POSSIBLE, lngIdx) = WORD_POSSIBLE
- lngPossibleWordCnt = lngPossibleWordCnt + 1
- 'For lngIdx2 = lngWordStart To lngWordSize + lngWordStart
- ' strword = strword & Chr$(bytFile(lngIdx2))
- 'Next
- 'strword = strword & vbCrLf
- End If
- 'Clear the array
- ReDim lngUsedLetterFlags(0 To lngMaxWordSize - 1)
- lngUsedLetterCount = 0
- Next lngIdx
- MsgBox lngPossibleWordCnt & " possible words."
- 'Debug.Print strword
- End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement