prog8/examples/numbergame-lowlevel.p8

78 lines
2.4 KiB
Plaintext
Raw Normal View History

%import c64utils
%import c64flt
; The classic number guessing game.
; This version uses more low-level subroutines (calls directly into the C64's ROM routines)
; and instead of a loop (with the added behind the scenes processing), uses absolute jumps.
; It's less readable I think, but produces a smaller program.
~ main {
sub start() {
str name = "????????????????????????????????????????"
str input = "??????????"
2018-12-07 23:27:12 +00:00
ubyte guess
ubyte secretnumber = 0
ubyte attempts_left = 10
memory uword freadstr_arg = $22 ; argument for FREADSTR ($22/$23)
; greeting
2018-12-07 23:27:12 +00:00
c64.VMCSB |= 2 ; switch lowercase chars
2018-12-06 23:08:22 +00:00
c64.STROUT("Please introduce yourself: ")
2018-12-07 23:27:12 +00:00
c64scr.input_chars(name)
2018-11-28 00:12:23 +00:00
c64.CHROUT('\n')
c64.CHROUT('\n')
2018-12-06 23:08:22 +00:00
c64.STROUT("Hello, ")
c64.STROUT(name)
c64.STROUT(".\nLet's play a number guessing game.\nI am thinking of a number from 1 to 100!You'll have to guess it!\n")
; create a secret random number from 1-100
c64flt.RND() ; fac = random number between 0 and 1
c64flt.MUL10() ; fac *= 10
c64flt.MUL10() ; .. and now *100
c64flt.FADDH() ; add 0.5..
c64flt.FADDH() ; and again, so +1 total
A, Y = c64flt.GETADRAY()
2018-12-07 23:27:12 +00:00
secretnumber = A ; secret number = rnd()*100+1
ask_guess:
2018-12-06 23:08:22 +00:00
c64.STROUT("\nYou have ")
2018-12-21 00:06:01 +00:00
c64scr.print_ub(attempts_left)
2018-12-06 23:08:22 +00:00
c64.STROUT(" guess")
if(attempts_left>1)
c64.STROUT("es")
2018-12-06 23:08:22 +00:00
c64.STROUT(" left.\nWhat is your next guess? ")
Y=c64scr.input_chars(input)
2018-11-28 00:12:23 +00:00
c64.CHROUT('\n')
freadstr_arg = input
c64flt.FREADSTR(Y)
A, Y = c64flt.GETADRAY()
2018-12-07 23:27:12 +00:00
guess=A
2018-12-10 08:25:38 +00:00
if(guess==secretnumber) {
2018-12-06 23:08:22 +00:00
c64.STROUT("\nThat's my number, impressive!\n")
goto goodbye
}
2018-12-06 23:08:22 +00:00
c64.STROUT("\nThat is too ")
if(guess < secretnumber)
2018-12-06 23:08:22 +00:00
c64.STROUT("low!\n")
else
2018-12-06 23:08:22 +00:00
c64.STROUT("high!\n")
attempts_left--
if_nz goto ask_guess
; game over.
2018-12-06 23:08:22 +00:00
c64.STROUT("\nToo bad! It was: ")
2018-12-21 00:06:01 +00:00
c64scr.print_ub(secretnumber)
2018-11-28 00:12:23 +00:00
c64.CHROUT('\n')
goodbye:
2018-12-06 23:08:22 +00:00
c64.STROUT("\nThanks for playing, ")
c64.STROUT(name)
c64.STROUT(".\n")
return
}
}