Re: making a anagram search on a wordlist

From: Larry Serflaten (serflaten_at_usinternet.com)
Date: 06/18/04


Date: Fri, 18 Jun 2004 11:14:51 -0500


"Wilhil" <Wilhil@discussions.microsoft.com> wrote
> Hi
>
> basically I am trying to make a game where it generates out of random 9 characters. You have to make a word as quickly as
possible, It successfully looks up the word in the list, but I want it after I have done this to show me the best possible
combination.

How are you looking up the words in your list? I would think you need to build your
list such that it can support both types of lookup you want, namely 'exists' and 'best
match'.

How you do that depends on how much work you want to spend doing it....

Since your game uses combinations of letters, you might group your list by
the words that fit a certain combination. Here is a simple example, not fully
developed, (mutli letters not accounted for, etc,) but it may give you some
ideas...

HTH
LFS

Option Explicit

Private Type ListItem
  hash As Long
  List As String
End Type

Private WordList() As ListItem

Private Sub Form_Load()

  ReDim WordList(2)

  ' Build list
  WordList(0).hash = GetHash("apt")
  WordList(0).List = ",apt,pat,tap,"

  WordList(1).hash = GetHash("bat")
  WordList(1).List = ",bat,tab,"

  WordList(2).hash = GetHash("rat")
  WordList(2).List = ",art,rat,tar,"

  ' Find Word
  Debug.Print "BART="; WordExists("bart")
  Debug.Print "ART="; WordExists("art")

  ' Show others
  Debug.Print "Words for A B & T = "; FindMatch("abt")

End Sub

Private Function FindMatch(ByVal letters As String) As String
Dim hash As Long
Dim i As Long
  hash = GetHash(letters)
  For i = 0 To UBound(WordList)
    If WordList(i).hash = hash Then
      FindMatch = WordList(i).List
      Exit Function
    End If
  Next
End Function

Private Function WordExists(ByVal word As String) As Boolean
Dim hash As Long
Dim i As Long
  hash = GetHash(word)
  word = "," & LCase$(word) & ","
  For i = 0 To UBound(WordList)
    If WordList(i).hash = hash Then
      WordExists = InStr(WordList(i).List, word)
      Exit Function
    End If
  Next
End Function

Private Function GetHash(ByVal word As String) As Long
Dim bit
   word = LCase$(word)
   For bit = 1 To Len(word)
     GetHash = GetHash Or (2 ^ (Asc(word) - 97))
     word = Mid$(word, 2)
   Next
End Function



Relevant Pages

  • Re: outlook status bar
    ... ByVal lpClassName As String, ByVal nMaxCount As Long) As Long ... Private Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, ... Dim hwnd As Long ... Private Function FindChildWindowText(ByVal lHwnd As Long, ...
    (microsoft.public.office.developer.outlook.vba)
  • Re: mkdir
    ... Private Function VBCreatePath(NewPath As String) As Boolean ... Dim vArr As Variant ... Dim iFor As Integer ...
    (microsoft.public.vb.bugs)
  • Re: mkdir
    ... > Private Function VBCreatePath(NewPath As String) As Boolean ... > Dim vArr As Variant ... > Dim iFor As Integer ...
    (microsoft.public.vb.bugs)
  • Re: current date and time object
    ... > 'Office97 VBE does not recognise the AddressOf operator; ... > Private Function vbaPassAs Long ... > ByVal lpWindowName As String) As Long ... > Dim CurrentTime As String ...
    (microsoft.public.excel)
  • Re: Obtain remote PC system datetime
    ... Private Function GetRemoteMachineTOD(sMachine As String) As String ... Dim objset As SWbemObjectSet ... Dim bias As Long ...
    (microsoft.public.vb.winapi.networks)