prog8/compiler/examples/numbergame.p8

77 lines
2.2 KiB
Plaintext
Raw Normal View History

2018-08-14 00:22:59 +00:00
%output prg
%import c64lib
%import mathlib
~ main {
sub start() -> () {
2018-08-14 00:22:59 +00:00
str name = "?" * 80
str guess = "?" * 80
2018-08-14 12:33:36 +00:00
byte secretnumber = 0
byte attempts_left = 10
2018-08-14 00:22:59 +00:00
memory word freadstr_arg = $22 ; argument for FREADSTR
c64.init_system()
c64.VMCSB |= 2 ; activate lowercase charset
2018-08-14 00:22:59 +00:00
; greeting
c64scr.print_string("Enter your name: ")
Y = c64scr.input_chars(name)
c64.CHROUT("\n")
c64.CHROUT("\n")
c64scr.print_string("Hello, ")
c64scr.print_string(name)
c64.CHROUT(".")
c64.CHROUT("\n")
; create a secret random number from 1-100
c64.RNDA(0) ; fac = rnd(0)
c64.MUL10() ; fac *= 10
c64.MUL10() ; .. and now *100
c64.FADDH() ; add 0.5..
c64.FADDH() ; and again, so +1 total
AY = c64flt.GETADRAY()
secretnumber = A
2018-08-14 00:22:59 +00:00
;A=math.randbyte()
;A+=c64.RASTER
;A-=c64.TIME_LO
;X,secretnumber=math.divmod_bytes(A, 99)
c64scr.print_string("I am thinking of a number from 1 to 100!You'll have to guess it!\n")
2018-08-14 12:33:36 +00:00
ask_guess:
2018-08-14 00:22:59 +00:00
c64scr.print_string("\nYou have ")
c64scr.print_byte_decimal(attempts_left)
c64scr.print_string(" guess")
if(attempts_left>0) c64scr.print_string("es")
2018-08-14 00:22:59 +00:00
c64scr.print_string(" left.\nWhat is your next guess? ")
Y = c64scr.input_chars(guess)
c64.CHROUT("\n")
freadstr_arg = guess
c64.FREADSTR(A)
AY = c64flt.GETADRAY()
2018-08-14 12:33:36 +00:00
if(A==secretnumber) {
c64scr.print_string("\nThat's my number, impressive!\n")
goto goodbye
2018-08-14 12:33:36 +00:00
}
2018-08-14 00:22:59 +00:00
c64scr.print_string("That is too ")
2018-08-14 12:33:36 +00:00
if(A > secretnumber)
c64scr.print_string("low!\n")
2018-08-14 12:33:36 +00:00
else
c64scr.print_string("high!\n")
2018-08-14 00:22:59 +00:00
attempts_left--
if(attempts_left>0) goto ask_guess
; more efficient: if_nz goto ask_guess
2018-08-14 00:22:59 +00:00
; game over.
2018-08-14 00:22:59 +00:00
c64scr.print_string("\nToo bad! It was: ")
c64scr.print_byte_decimal(secretnumber)
c64.CHROUT("\n")
2018-08-14 12:33:36 +00:00
goodbye:
2018-08-14 00:22:59 +00:00
c64scr.print_string("\nThanks for playing. Bye!\n")
return
}
2018-08-14 00:22:59 +00:00
}