dim strFinalCode as string
dim strFinalGuess as string
dim strThisGuess as string
dim caseMatters as boolean 'case sensitivity flag
dim intLevel as integer
dim iCount as integer

dim intRandomCount as integer
dim strRandomString as string
dim strRandomChar as string

dim intRandom as integer
dim intPrevious as integer	

dim strRound as byte

function AreStringsEqual(byval str1 as string, byval str2 as string,byval caseMatters as boolean) as boolean
	'byVals added after Patrick Dwyer points out a side affect of passing by reference & ucase()
     
	dim i as integer
	dim s1 as string'our own copies of the strings
	dim s2 as string

	s1 = str1 'make our own copies to edit.
	s2 = str2

	'if they aren't even the same length then give up early.
	if len(s1)<>len(s2) then
		AreStringsEqual = false       'set return value to false
		exit function                 'get out of here
	end if

	'being not case sensitive is a bit more work.
	if caseMatters=false then
		s1=ucase(s1)
		s2=ucase(s2)
	end if

      ' here is the ascii comparison loop.
      dim c1 as string
      dim c2 as string
      for i = 1 to len(s1) 'proceed to loop through all the chars.
            c1 = mid(s1,i,1) 'c1 catches the current character in one string
            c2 = mid(s2,i,1) 'c2 catches the current character in the other
            if asc(c1)<>asc(c2) then 'ah HA, i found one!
                  AreStringsEqual = false       'so set return value to false
                  exit function                       'and then leave
            end if
      next

      AreStringsEqual=true 'finally, give in.
end function

Sub Main()
	call delay(0.5)	
	caseMatters = FALSE 'make this TRUE if 'A' is not 'a'

'********  CRASH STARTS HERE

	if strRound = 1 then
		debug.print "start level " ; cstr(intLevel)
	else
		intLevel = 1 'this is the starting level of difficulty (or, the number of flashes)
		debug.print "start level " ; cstr(intLevel)		
	end if

	intRandomCount = 0 
	strRandomString = "" 
	strRandomChar = "" 

	'generate random number, that is has X (or, intLevel) digits
	intPrevious = getADC(5)
	iCount = 0 
	do while iCount < intLevel
		iCount = iCount + 1
		intRandomCount = intRandomCount + 1
	
		intRandom = getADC(5) 
		if intPrevious = 0 then 
			intPrevious = getADC(5)
		end if
		intRandom = intRandom - intPrevious
		intPrevious = getADC(5)

		if intRandom = 0 then 
			intRandom = intRandom + iCount
		end if
		if intRandom > 4 then
			intRandom = 1
		end if
		do while intRandom < 0
			intRandom = intRandom * (-2)
		loop
		if intRandom = 0 then 
			intRandom = intRandom + iCount
		end if
		if intRandom > 4 then 
			'debug.print "meep"
			intRandom = 2
			'debug.print cstr(intRandom)
		end if 

		'debug.print cstr(intRandom)
		strRandomString = strRandomString & cstr(intRandom)
		debug.print "//" ; cstr(intRandom)
	loop

	strFinalCode = strRandomString
	
	debug.print "random code: " ; strFinalCode
	strFinalGuess = ""
	strThisGuess = ""

	'flash light 3x to tell user that game has started/reset
	if strRound <> 1 then

		iCount = 0 
		do while iCount < 3
			iCount = iCount + 1
			call putPin(8, 1)
			delay(0.2)
			call putPin(8, 0)
			delay(0.2)
		loop	
	end if

	dim str1 as string
	dim str2 as string
	dim str3 as string
	dim str4 as string
	str1 = "1"
	str2 = "2"
	str3 = "3"
	str4 = "4"
	dim char as string

	dim localCount as integer

	debug.print "length of code: " ; cstr(len(strFinalCode))
	for localCount  = 1 to len(strFinalCode) 'proceed to loop through all the digits in the code
     	char = mid(strFinalCode,localCount,1) 'c1 catches the current character in one string // string, which char, num of chars
		'debug.print char
		if AreStringsEqual (char , str1 , caseMatters ) = true then
			'debug.print "here."
			call putPin(12, 1)
			'call delay(0.5)
			call putPin(12, 0)	
		elseif AreStringsEqual (char , str2 , caseMatters ) = true then
			'debug.print "here."
			call putPin(11, 1)
			'call delay(0.5)	
			call putPin(11, 0)	
		elseif AreStringsEqual (char , str3 , caseMatters ) = true then 
			'debug.print "here."
			call putPin(10, 1)
			'call delay(0.5)	
			call putPin(10, 0)	
		elseif AreStringsEqual (char , str4 , caseMatters ) = true then
			'debug.print "here."
			call putPin(9, 1)
			'call delay(0.5)	
			call putPin(9, 0)	
		end if
	next

	debug.print "code = " ;  strFinalCode
	debug.print "level = " ;  cstr(intLevel)
	
	if Len(strFinalGuess) < intLevel then
		do while Len(strFinalGuess) < intLevel
			
			if getPin(13) = 0 then
				'flash light so user knows they pressed button
				call putPin(8, 1)
				delay(0.2)
				call putPin(8, 0)

				strThisGuess = "1"
				strFinalGuess = strFinalGuess & strThisGuess
				debug.print strFinalGuess
				'call delay(1.5)				
			end if

			if getPin(14) = 0 then
				'flash light so user knows they pressed button
				call putPin(8, 1)
				delay(0.2)
				call putPin(8, 0)

				strThisGuess = "2"
				strFinalGuess = strFinalGuess & strThisGuess
				debug.print strFinalGuess
				'call delay(1.5)
			end if
			
			if getPin(15) = 0 then
				'flash light so user knows they pressed button
				call putPin(8, 1)
				delay(0.2)
				call putPin(8, 0)

				strThisGuess = "3"
				strFinalGuess = strFinalGuess & strThisGuess
				debug.print strFinalGuess
				'call delay(1.5)
			end if

			if getPin(16) = 0 then
				'flash light so user knows they pressed button
				call putPin(8, 1)
				delay(0.2)
				call putPin(8, 0)

				strThisGuess = "4"
				strFinalGuess = strFinalGuess & strThisGuess
				debug.print strFinalGuess
				'call delay(1.5)
			end if

		loop
		call SubmitGuess()
	elseif Len(strFinalGuess) < intLevel then
		debug.print "crashing HERE!"
		'call Main()
	else
		debug.print "error:  the guess string is too long"
	end if

End Sub
Sub
SubmitGuess() debug.print "final code: " ; strFinalCode debug.print "final guess: " ; strFinalGuess 'APP CRASHES HERE SECOND TIME AROUND 'dim intNumWins as integer if AreStringsEqual (strFinalCode , strFinalGuess , caseMatters ) = true then debug.print "you win!" strRound = 1 intLevel = intLevel + 1 debug.print "(now onto level = " ; cstr(intLevel) ; "!)" do until iCount = 3 iCount = iCount + 1 call putPin(19, 1) call delay(0.5) call putPin(19, 0) loop elseif AreStringsEqual (strFinalCode , strFinalGuess , caseMatters ) = false then debug.print "you lose" debug.print "(back to level 1" debug.print "end of game" intLevel = 1 do until iCount = 3 iCount = iCount + 1 call putPin(20, 1) call delay(0.5) call putPin(20, 0) loop else debug.print "broken strings" end if call Reset() End Sub
Sub
Reset () debug.print "RESET" debug.print "*************" debug.print "final code=" ; strFinalCode debug.print "final guess=" ; strFinalGuess debug.print "level=" ; cstr(intLevel) debug.print "*************" call Main() End Sub