Statistics: Posted by Erik — Thu Mar 03, 2022 4:15 pm
Statistics: Posted by Ham62 — Sat Feb 26, 2022 9:33 pm
Statistics: Posted by Ham62 — Thu Feb 24, 2022 3:39 am
Code:
DEFINT A-ZDECLARE FUNCTION openlist% (filename AS STRING)DECLARE FUNCTION wordIndex% (myWord AS STRING)REDIM SHARED wordLst(0) AS STRING * 5DIM user AS STRING, id AS INTEGERPRINT "Loading list..."IF (NOT openlist%("words.dat")) THEN PRINT "Something went wrong": END' picking a random word from the list...PRINT "picking a random word: "; CHR$(34); wordLst(INT(RND * (UBOUND(wordLst) + 1))); CHR$(34)' testing user input (only accept existing words)DO INPUT "Guess a word:", user IF LEN(user) THEN id = wordIndex%(user) IF (id < 0) THEN PRINT "This word is not in the dictionary" ELSE PRINT "This word is index #"; id END IF ELSE EXIT DO END IFLOOP' Open word list. The list MUST be formatted and orderly; if not, the routine' will handle that for you and update the file so it loads faster next time.' Users can add (or remove) words from the list. In a perfect world, we should' make sure there's enough memory available to store the dictionary and also' verify data integrity (so only words with characters in range A-Z are ok'd.)FUNCTION openlist% (filename AS STRING) DIM ff AS INTEGER, update AS INTEGER, tmp as string * 5 DIM count AS INTEGER, offset AS INTEGER, max AS INTEGER, hiRef AS INTEGER ' open file ff = FREEFILE OPEN filename FOR INPUT AS #ff ' file size should be multiple of 7 (doesn't guarantee data integrity) IF (LOF(ff) MOD 7) THEN CLOSE #ff EXIT FUNCTION END IF ' resize word array REDIM wordLst(LOF(ff) \ 7 - 1) AS STRING * 5 ' store all in uppercase, then close file FOR i% = 0 TO UBOUND(wordLst) INPUT #ff, tmp wordLst(i%) = ucase$(tmp) NEXT i% CLOSE #ff ' now make sure they are alphabetically sorted, this can take a while ' depending on the number of words and how disorderly the list is. count = UBOUND(wordLst) + 1 offset = count \ 2 DO WHILE offset max = count - offset - 1 DO hiRef = 0 FOR i% = 0 TO max IF (wordLst(i%) > wordLst(i% + offset)) THEN SWAP wordLst(i%), wordLst(i% + offset) update = -1 hiRef = i% END IF NEXT i% max = hiRef - offset LOOP WHILE hiRef offset = offset \ 2 LOOP ' list had to be sorted, re-save so it loads faster next time IF update THEN OPEN filename FOR OUTPUT AS #ff FOR i% = 0 TO UBOUND(wordLst) PRINT #ff, wordLst(i%) NEXT i% CLOSE #ff END IF ' all done openlist% = -1END FUNCTION' Binary search. It's much faster than a linear search (one entry after the' other) but it requires the array to be alphabetically ordered. This function' returns the index of the word within the wordLst() array. If the word cannot' be found, this function returns -1.FUNCTION wordIndex% (myWord AS STRING) DIM search AS STRING, cueLo AS INTEGER, cueHi AS INTEGER, cueMid AS INTEGER search = UCASE$(myWord) ' make sure the input is formatted cueHi = UBOUND(wordLst) ' upper boundary wordIndex% = -1 ' assume the word doesn't exist DO WHILE (cueLo <= cueHi) cueMid = (cueLo + cueHi) \ 2 ' test central word SELECT CASE wordLst(cueMid) CASE IS < search cueLo = cueMid + 1 ' word must be in the 2nd half CASE IS > search cueHi = cueMid - 1 ' word must be in the 1st half CASE ELSE wordIndex% = cueMid ' found it! EXIT FUNCTION END SELECT LOOPEND FUNCTION
Statistics: Posted by MikeHawk — Wed Feb 23, 2022 10:17 am
Statistics: Posted by Ham62 — Wed Feb 23, 2022 4:05 am