Post by tsh73 on Oct 8, 2020 17:36:21 GMT
Trying to make variable persistent in simplest possible way.
So, guess-a-number where secret number is saved to file.
If you close a program and then restart it'll pick saved number
(and if you guess it will clear file)
Now, if I have that number stored, I tried to make progam do single guess
But closing a window and resatring a program seems like too much hassle.
So, guess-a-number where secret number is saved to file.
I made a secret (but I will not tell you)
If you close a program and then restart it'll pick saved number
I gathered saved secret (and I will not tell you)
(and if you guess it will clear file)
---- Guess a number ----
I made up a number, (1..9)
- you guess it. That's all ;)
No! Not quite.
My number is saved.
If you close program and run it AGAIN,
you'll have SAME number to guess.
I made a secret (but I will not tell you)
Make your guess (1..9): 2
Nope, try again
Make your guess (1..9): 8
You did it!
-=* thanks for playing *=-
'guessANumber - example for
' save variable in a file
' reload variable
'(simplest way)
'logic: loop
' check if file exists
' if it is, read
' else generate and save
' ask
' say guessed or not
' !! if guessed delete saved file !!
print "---- Guess a number ----"
print "I made up a number, (1..9)"
print "- you guess it. That's all ;)"
print
print "No! Not quite."
print "My number is saved."
print "If you close program and run it AGAIN,"
print "you'll have SAME number to guess."
print
fname$="GuessANumber.cfg"
res$=readString$(fname$)
if res$="#NoSuchFile" then
secret = int(rnd(0)*9+1) '0..8 -> 1..9
call saveString fname$,str$(secret)
print "I made a secret (but I will not tell you)"
else
secret=val(res$)
print "I gathered saved secret (and I will not tell you)"
end if
while 1
Input "Make your guess (1..9): ";guess
if guess<>secret then
print "Nope, try again"
else
print "You did it!"
kill fname$
exit while
end if
wend
print "-=* thanks for playing *=-"
end
'==================================================
function readString$(fname$)
readString$="#NoSuchFile" 'default. If no file
on error goto [fail]
open fname$ for input as #inFile
readString$ = input$(#inFile, lof(#inFile))
close #inFile
exit function
[fail]
end function
sub saveString fname$,varVal$
open fname$ for output as #outFile
#outFile, varVal$; 'no extra CRLF
close #outFile
end sub
Now, if I have that number stored, I tried to make progam do single guess
But closing a window and resatring a program seems like too much hassle.
---- Guess a number ----
I made up a number, (1..9)
- you guess it. That's all ;)
No! Not quite.
My number is saved.
If you close program and run it AGAIN,
you'll have SAME number to guess.
I made a secret (but I will not tell you)
Make your guess (1..9): 3
Nope, try again (RUN that program AGAIN!)
I gathered saved secret (and I will not tell you)
Make your guess (1..9): 5
Nope, try again (RUN that program AGAIN!)
I gathered saved secret (and I will not tell you)
Make your guess (1..9): 2
You did it!
-=* thanks for playing *=-
'guessANumber - example for
' save variable in a file
' reload variable
'(simplest way)
'no loop variant - you RUN this program for next turn
'I admit, it is royal PITA (console to be closed each time)
' check if file exists
' if it is, read
' else
' show info (first time?)
' generate and save
' ask
' say guessed or not
' !! if guessed delete saved file !!
fname$="GuessANumber.cfg"
res$=readString$(fname$)
if res$="#NoSuchFile" then
gosub [help]
secret = int(rnd(0)*9+1) '0..8 -> 1..9
call saveString fname$,str$(secret)
print "I made a secret (but I will not tell you)"
else
secret=val(res$)
print "I gathered saved secret (and I will not tell you)"
end if
Input "Make your guess (1..9): ";guess
if guess<>secret then
print "Nope, try again (RUN that program AGAIN!)"
else
print "You did it!"
kill fname$
print "-=* thanks for playing *=-"
end if
end
[help]
print "---- Guess a number ----"
print "I made up a number, (1..9)"
print "- you guess it. That's all ;)"
print
print "No! Not quite."
print "My number is saved."
print "If you close program and run it AGAIN,"
print "you'll have SAME number to guess."
print
return
'==================================================
function readString$(fname$)
readString$="#NoSuchFile" 'default. If no file
on error goto [fail]
open fname$ for input as #inFile
readString$ = input$(#inFile, lof(#inFile))
close #inFile
exit function
[fail]
end function
sub saveString fname$,varVal$
open fname$ for output as #outFile
#outFile, varVal$; 'no extra CRLF
close #outFile
end sub