Worm - Human vs Computer
#2
I have an algorithm that lets the computer play against itself, to semi-randomly build on a word, towards a legitimate word. It's very basic at present (with lots of GoTo's etc), but this will improve, I hope. Later it will be a human v computer version of Worm. Here's the code: (Note: it needs the WordLists files)
Code: (Select All)
'Comp v Comp Worm
Screen 12
_FullScreen
Randomize Timer
Common Shared wrd$, firstfile$, srchfile$, start$, dictwrd$
' wrd$ is the word  being built,   firstlist is the reference file search for start of search,  srchlist is actual list being searched,   start$ is the list's location
'dictwrd$ is word input from wordlist,   po is position of wrd$ in dictwrd$


Seed:
wrd$ = Chr$(Int(Rnd * 26) + 65)
Locate 10, 25: Print "Start-letter: ";: Print wrd$ '                         We have the first letter

StartNewWrdSearch:
firstlist = Int(Rnd * 26) + 65 '                                             firstlist is refence point for first wordlist to be searched, so that we know when all have been searched
srchlist = firstlist

FindAWord:
srchlist = srchlist + 1: If srchlist > 90 Then srchlist = 65 '               srchlist is ascii of list about to be searched

NextList: '                                                                  point to return to after each list has been searched
start$ = "WordLists/" + Chr$(srchlist) '                                     formulate path to srchlist as "WordLists/A" etc.
Locate 12, 18: Print "List being searched: "; start$
Open start$ For Input As #1 '                                                open the selected wordlist

DictLoop:
If Not EOF(1) Then
    Input #1, dictwrd$
    If dictwrd$ = wrd$ Then '                                                only used when dictwrd$ matches wrd$
        Matched
    End If
Else
    Close #1
    GoTo FindAWord
End If
po = InStr(dictwrd$, wrd$) '                                                 look for wrd$ in dictwrd$
If po = 0 Then GoTo DictLoop '                                               if not found, get next dictwrd$
If po = 1 Then '                                                             if wrd$ is at start of dictwrd$,
    wrd$ = wrd$ + Mid$(dictwrd$, po + Len(wrd$), 1) '                        add the letter after wrd$ to wrd$
Else
    wrd$ = Mid$(dictwrd$, po - 1, 1) + wrd$ '                                but if not at start, add the letter before wrd$ to wrd$
End If
Locate 16, 36: Print Space$(20)
Locate 16, 24: Print "Search-string: "; wrd$
Locate 14, 24: Print Space$(30)
Locate 14, 24: Print "Wordlist word: "; dictwrd$
_Delay .2
GoTo DictLoop

Sub Matched
    Locate 20, 30: Print wrd$; " Matched "; dictwrd$: Sleep
End Sub












'Comp v Comp Worm
Screen 12
_FullScreen
Randomize Timer
Common Shared wrd$, firstfile$, srchfile$, start$, dictwrd$
' wrd$ is the word  being built,   firstlist is the reference file search for start of search,  srchlist is actual list being searched,   start$ is the list's location
'dictwrd$ is word input from wordlist,   po is position of wrd$ in dictwrd$


Seed:
wrd$ = Chr$(Int(Rnd * 26) + 65)
Locate 10, 25: Print "Start-letter: ";: Print wrd$ '                         We have the first letter

StartNewWrdSearch:
firstlist = Int(Rnd * 26) + 65 '                                             firstlist is refence point for first wordlist to be searched, so that we know when all have been searched
srchlist = firstlist

FindAWord:
srchlist = srchlist + 1: If srchlist > 90 Then srchlist = 65 '               srchlist is ascii of list about to be searched

NextList: '                                                                  point to return to after each list has been searched
start$ = "WordLists/" + Chr$(srchlist) '                                     formulate path to srchlist as "WordLists/A" etc.
Locate 12, 18: Print "List being searched: "; start$
Open start$ For Input As #1 '                                                open the selected wordlist

DictLoop:
If Not EOF(1) Then
    Input #1, dictwrd$
    If dictwrd$ = wrd$ Then '                                                only used when dictwrd$ matches wrd$
        Matched
    End If
Else
    Close #1
    GoTo FindAWord
End If
po = InStr(dictwrd$, wrd$) '                                                 look for wrd$ in dictwrd$
If po = 0 Then GoTo DictLoop '                                               if not found, get next dictwrd$
If po = 1 Then '                                                             if wrd$ is at start of dictwrd$,
    wrd$ = wrd$ + Mid$(dictwrd$, po + Len(wrd$), 1) '                        add the letter after wrd$ to wrd$
Else
    wrd$ = Mid$(dictwrd$, po - 1, 1) + wrd$ '                                but if not at start, add the letter before wrd$ to wrd$
End If
Locate 16, 36: Print Space$(20)
Locate 16, 24: Print "Search-string: "; wrd$
Locate 14, 24: Print Space$(30)
Locate 14, 24: Print "Wordlist word: "; dictwrd$
_Delay .2
GoTo DictLoop

Sub Matched
    Locate 20, 30: Print wrd$; " Matched "; dictwrd$: Sleep
End Sub
Of all the places on Earth, and all the planets in the Universe, I'd rather live here (Perth, W.A.) Big Grin
Reply


Messages In This Thread
Worm - Human vs Computer - by PhilOfPerth - 01-29-2023, 05:03 AM
RE: Worm - Human vs Computer - by PhilOfPerth - 01-31-2023, 02:52 AM
RE: Worm - Human vs Computer - by bplus - 01-31-2023, 03:47 PM
RE: Worm - Human vs Computer - by PhilOfPerth - 02-01-2023, 02:14 AM
RE: Worm - Human vs Computer - by PhilOfPerth - 02-01-2023, 11:16 PM



Users browsing this thread: 1 Guest(s)