prog8/examples/numbergame.p8

66 lines
1.8 KiB
Plaintext
Raw Permalink Normal View History

%import textio
%import conv
%import math
%zeropage basicsafe
2018-12-06 23:08:22 +00:00
; The classic number guessing game.
; Note: this program can be compiled for multiple target systems.
2019-07-29 21:11:13 +00:00
main {
2018-11-28 00:12:23 +00:00
sub start() {
2018-12-07 23:27:12 +00:00
str name = "????????????????????????????????????????"
str input = "??????????"
ubyte secretnumber = math.rnd() % 99 + 1 ; random number 1..100
ubyte attempts_left
2018-11-28 00:12:23 +00:00
txt.lowercase()
2020-08-27 16:10:22 +00:00
txt.print("Please introduce yourself: ")
void txt.input_chars(name)
txt.print("\n\nHello, ")
txt.print(name)
txt.print(".\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
2020-03-11 19:47:42 +00:00
for attempts_left in 10 downto 1 {
2018-12-10 08:25:38 +00:00
2020-08-27 16:10:22 +00:00
txt.print("\nYou have ")
txt.print_ub(attempts_left)
txt.print(" guess")
if attempts_left>1
2020-08-27 16:10:22 +00:00
txt.print("es")
txt.print(" left.\nWhat is your next guess? ")
void txt.input_chars(input)
2020-10-11 16:36:20 +00:00
ubyte guess = conv.str2ubyte(input)
2018-12-10 08:25:38 +00:00
if guess==secretnumber {
ending(true)
return
2018-11-28 00:12:23 +00:00
} else {
2020-08-27 16:10:22 +00:00
txt.print("\n\nThat is too ")
2019-01-15 23:28:30 +00:00
if guess<secretnumber
2020-08-27 16:10:22 +00:00
txt.print("low!\n")
2018-11-28 00:12:23 +00:00
else
2020-08-27 16:10:22 +00:00
txt.print("high!\n")
2018-11-28 00:12:23 +00:00
}
}
ending(false)
return
sub ending(bool success) {
if success
2020-08-27 16:10:22 +00:00
txt.print("\n\nYou guessed it, impressive!\n")
else {
2020-08-27 16:10:22 +00:00
txt.print("\nToo bad! My number was: ")
txt.print_ub(secretnumber)
txt.print(".\n")
}
2020-08-27 16:10:22 +00:00
txt.print("Thanks for playing, ")
txt.print(name)
txt.print(".\n")
}
2018-11-28 00:12:23 +00:00
}
}