EXAMPLE SOURCE CODES


     This is a small collection of mostly game example source codes. These source codes are made available to help PlayBasic programmers kick start their game programming journey. Looking for more source code / tutorials & media, then remember to visit the PlayBasic Resource board on our forums.

Found #1 item in Searching category

 Word heap - Dictionary Searching

By: Kevin Picone Added: March 24th, 2022

Category: All,Searching,Heap

    WordHeap - Used to keep a Dictionary of words / strings


    The Example test code (bellow)  loads the PlayBasic keywords file and adds all the PB commands to a heap (a static dictionary) and then does a search for each word.  The idea head is that we can build a dictionary of known words and then search if they exist or not.    While the test assumes we're cataloging words (in this case PlayBasic command names), it also support none alphanumeric characters too.  


PlayBasic Code:
; PROJECT : PlayBasic - KeyWord - Bucket
; AUTHOR  : Kev PIcone - PlayBasic TUTOR - Http://PlayBasic.com
; CREATED : 24/03/2022
; EDITED  : 25/03/2022
; ---------------------------------------------------------------------


	// Compute the location of the PlayBasic command listing
	// so we have a big list of words for something to load and test
	file$=GetPlayBasicKeyWordsPath$()

	// Load the keywords textfile to a string
	All_KeyWords$=LoadFileToString(file$)
	

	// Scan this block of text looking for the section named [Commands]
	// and then return it
	Command_KEyWords$ = GetKeywordsUnderHeading(All_KeyWords$,"[Commands]")


	//  Test adding PlayBasic keywords to our WordHeap
	Test_KeyWord_Heap(Command_KeyWords$)
	
	
	// 
	print "Test Complete - Press Space To End"
	
	; refresh display and wait for a key press before ending
	sync
	waitkey
	end
	
	
	
Function Test_KeyWord_Heap(KeyWords$)
	
	
		; Dim a string array called WORDS with an initial size of 1000
		dim Words$(1000)

		Result$	=replace$(KeyWords$,Chr$(13)+Chr$(10),",")
		count		=splittoarray(Result$,",",Words$())


		// --------------------------------------------------
		//  Add each word to the Word Heap / Dictionary
		// --------------------------------------------------
		For lp=0 to count-1
				word$		=words$(lp)
			   WordHeap_Add(Word$)
		next


		// --------------------------------------------------
		// Do a search for all the added keywords
		// --------------------------------------------------
		print " Searching For #"+Str$(Count)+" Keywords"
		starttime=timer()
		For lp=0 to count-1
				word$		=words$(lp)
				Status=WordHeap_FIND(Word$)
				#print digits$(Status,2)+">>>"+word$
				Matches+=Status
		next
		StartTime=Timer()-StartTime	
		print "Found Keywords #"+Str$(Matches)
		print ""
		print "Search Time #"+Str$(StartTime)+"  milliseconds"
		print ""
		print ""


EndFunction





Function GetKeywordsUnderHeading(KeyWords$,Heading$)
	
	cr$=Chr$(13)+chr$(10)
	Tag$=Heading$+cr$

	// Lookgfor the TAG plus the linefeed / end of line within the string
	StartPOS=instring(keywords$,tag$)
	if StartPos
		
		// If the tag is found, we step the found position to the end
		// of the found location plus the tag size in characters
		StartPos+=Len(tag$)
		
		//  search for tbhe first empty line beyond where the starting tag
		// was founf
		EndPos 		=instring(KeyWords$,Cr$+cr$,StartPos)
		
		// Check if the closing tag position was indeed after the start?
		if EndPos>StartPOs
			//  use MID$() to return this block of text from the keyword string		
			Result$		=mid$(Keywords$,StartPos,EndPos-StartPOs)
		endif
	endif
	
EndFunction  Result$





Function LoadFileToString(file$)
	if FIleexist(file$)
		local size=filesize(file$)
		local f=readnewfile(file$)
			result$=readchr$(f,size)
		closefile f
	endif
EndFunction Result$




LinkDll "shell32"
   zPriv_SHGetFolderPath(hWndOwner,nFolder,hToken,dwFlags,pszPath) Alias "SHGetFolderPathA"  as integer
EndLinkDll




Function zPriv_Highlighter_GetSpecialFolderPath(nFolderID)
   // Alloc a bank of 1024 bytes for the function to return the path in
   local Size=1024
   local ThisBank=newbank(Size)
   local Ptr=GetBankPtr(thisBank)
   local Status=zPriv_SHGetFolderPath(0,nFolderID,0,0,ptr)
   if Status=0
         // if status is 0 then the function worked
         Path$=PeekString(ptr,0)   ; peek a null termed string         
   else
         #print "error polling GetSpeicalFolderpath"
   endif
   Deletebank ThisBank
EndFunction path$




PSUB GetPlayBasicKeyWordsPath$()
		KeyWordsFile$=""

		;	constant CSIDL_LOCAL_APPDATA = $1C          ;{user}\Local App Data Settings _
		local folder$=zPriv_Highlighter_GetSpecialFolderPath($1C)
		if folderexist(folder$)
				// Get the Absolute location of the PlayBasic keywords file
				 KeyWordsFile$=Folder$+"\PlayBasic\Info\KeyWords.txt"
				if fileexist(KeyWordsFile$)=false
						KeyWordsFile$=""
				endif	
		endif
EndPSUB KeyWordsFile$






 Download

      Attached to post bellow


Download: Login to Download




Viewing Page [1] of [0]



Want More Source Codes?:



Release Type: The source code & tutorials found on this site are released as license ware for PlayBasic Users. No Person or Company may redistribute any file (tutorial / source code or media files) from this site, without explicit written permission.

 

 
     
 
       

(c) Copyright 2002 / 2024 Kevin Picone , UnderwareDesign.com  - Privacy Policy   Site: V0.99a [Alpha]