2018-11-28 00:12:23 +00:00
|
|
|
%import c64utils
|
2018-12-06 23:08:22 +00:00
|
|
|
%import mathlib
|
|
|
|
|
2018-12-10 23:09:37 +00:00
|
|
|
; The classic number guessing game.
|
|
|
|
; This version uses mostly high level subroutine calls and loops.
|
|
|
|
; It's more readable than the low-level version, but produces a slightly larger program.
|
2018-11-28 00:12:23 +00:00
|
|
|
|
|
|
|
~ main {
|
2018-12-10 23:09:37 +00:00
|
|
|
|
2018-11-28 00:12:23 +00:00
|
|
|
sub start() {
|
2018-12-07 23:27:12 +00:00
|
|
|
str name = "????????????????????????????????????????"
|
2018-12-10 23:09:37 +00:00
|
|
|
str input = "??????????"
|
|
|
|
ubyte secretnumber = rnd() % 99 + 1 ; random number 1..100
|
2018-11-28 00:12:23 +00:00
|
|
|
|
2018-12-07 23:27:12 +00:00
|
|
|
c64.VMCSB |= 2 ; switch lowercase chars
|
2018-12-06 23:08:22 +00:00
|
|
|
c64scr.print_string("Please introduce yourself: ")
|
2018-12-07 23:27:12 +00:00
|
|
|
c64scr.input_chars(name)
|
2018-12-06 23:08:22 +00:00
|
|
|
c64scr.print_string("\n\nHello, ")
|
2018-11-28 00:12:23 +00:00
|
|
|
c64scr.print_string(name)
|
2018-12-06 23:08:22 +00:00
|
|
|
c64scr.print_string(".\nLet's play a number guessing game.\nI am thinking of a number from 1 to 100!You'll have to guess it!\n")
|
2018-11-28 00:12:23 +00:00
|
|
|
|
|
|
|
for ubyte attempts_left in 10 to 1 step -1 {
|
2018-12-10 08:25:38 +00:00
|
|
|
|
2018-12-10 23:09:37 +00:00
|
|
|
; stackptr debugging
|
|
|
|
; c64scr.print_byte_decimal(X)
|
|
|
|
; c64.CHROUT('\n')
|
2018-12-10 08:25:38 +00:00
|
|
|
|
2018-11-28 00:12:23 +00:00
|
|
|
c64scr.print_string("\nYou have ")
|
2018-12-04 21:30:35 +00:00
|
|
|
c64scr.print_byte_decimal(attempts_left)
|
2018-11-28 00:12:23 +00:00
|
|
|
c64scr.print_string(" guess")
|
2018-12-10 23:09:37 +00:00
|
|
|
if attempts_left>1
|
|
|
|
c64scr.print_string("es")
|
2018-12-06 23:08:22 +00:00
|
|
|
c64scr.print_string(" left.\nWhat is your next guess? ")
|
2018-12-10 23:09:37 +00:00
|
|
|
c64scr.input_chars(input)
|
|
|
|
ubyte guess = str2ubyte(input)
|
2018-12-10 08:25:38 +00:00
|
|
|
|
2018-12-10 23:09:37 +00:00
|
|
|
; debug info
|
|
|
|
; c64scr.print_string(" > attempts left=")
|
|
|
|
; c64scr.print_byte_decimal(attempts_left)
|
|
|
|
; c64scr.print_string("\n > secretnumber=")
|
|
|
|
; c64scr.print_byte_decimal(secretnumber)
|
|
|
|
; c64scr.print_string("\n > input=")
|
|
|
|
; c64scr.print_string(input)
|
|
|
|
; c64scr.print_string("\n > guess=")
|
|
|
|
; c64scr.print_byte_decimal(guess)
|
|
|
|
; c64.CHROUT('\n')
|
|
|
|
; c64scr.print_byte_decimal(X) ; stackptr debugging
|
|
|
|
; c64.CHROUT('\n')
|
2018-12-10 08:25:38 +00:00
|
|
|
|
2018-12-10 23:09:37 +00:00
|
|
|
if guess==secretnumber {
|
2018-12-11 23:15:05 +00:00
|
|
|
return ending(true)
|
2018-11-28 00:12:23 +00:00
|
|
|
} else {
|
2018-12-06 23:08:22 +00:00
|
|
|
c64scr.print_string("\n\nThat is too ")
|
2018-12-10 23:09:37 +00:00
|
|
|
if guess<secretnumber
|
2018-11-28 00:12:23 +00:00
|
|
|
c64scr.print_string("low!\n")
|
|
|
|
else
|
|
|
|
c64scr.print_string("high!\n")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-11 23:15:05 +00:00
|
|
|
return ending(false)
|
2018-12-10 23:09:37 +00:00
|
|
|
|
|
|
|
|
|
|
|
sub ending(success: ubyte) {
|
|
|
|
if success
|
|
|
|
c64scr.print_string("\n\nYou guessed it, impressive!\n")
|
|
|
|
else {
|
|
|
|
c64scr.print_string("\nToo bad! My number was: ")
|
|
|
|
c64scr.print_byte_decimal(secretnumber)
|
|
|
|
c64scr.print_string(".\n")
|
|
|
|
}
|
|
|
|
c64scr.print_string("Thanks for playing, ")
|
|
|
|
c64scr.print_string(name)
|
|
|
|
c64scr.print_string(".\n")
|
|
|
|
}
|
2018-11-28 00:12:23 +00:00
|
|
|
}
|
|
|
|
}
|