Commits

ChrisLowe  committed 198dbf1

added guessing game

  • Participants
  • Parent commits ba7afb8

Comments (0)

Files changed (1)

File guessing_game.dasm16

+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+;;
+;;    Guessing game by @lowey2002
+;;
+;;
+;;
+;;
+
+:start
+JSR cls
+JSR ask_users_name_screen
+JSR generate_seed
+JSR cls
+
+:new_game
+JSR print_introduction
+JSR cls
+SET A, 1    ; minium random number
+SET B, 0x64 ; maximum random number
+JSR pseudorandom
+SET [random_number], A
+
+
+SET A, 0  ; first Guess
+
+:guess_loop
+ADD [guess_count], 1
+JSR cls
+JSR ask_guess
+SET A, guess_input
+SET B, 2
+JSR read_input
+SET A, guess_input
+JSR atoi
+SET [last_guess], A
+JSR evaluate_guess
+SET PC, guess_loop
+
+:evaluate_guess
+IFE [last_guess], [random_number]
+JSR game_over
+IFG [last_guess], [random_number]
+SET PC, higher_guess
+SET PC, lower_guess
+
+:lower_guess
+SET A, 1
+SET PC, POP
+
+:higher_guess
+SET A, 2
+SET PC, POP
+
+:game_over
+JSR cls
+SET A, correct_string
+SET B, 3
+JSR print_message
+
+
+
+SET A, number_of_guesses_string
+SET B, 5
+JSR print_message
+SET A, [guess_count]
+SET B, guess_count_string
+JSR itoa
+SET A, guess_count_string
+SET B, 5
+SET C, 6
+SET X, 0x6
+SET Y, 0x0
+SET Z, 0
+JSR print_message_color
+SET A, play_again_string
+SET B, 9
+JSR print_message
+JSR console
+JSR cls
+ADD [seed], [last_guess]   ;; update seed
+ADD [seed], [random_number]
+SET [guess_count], 0
+SET PC, new_game
+
+
+
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+;;
+;;      Ask the user for their next guess
+;;
+;; Parameters
+;;     A - Last guess result (0 == first, 1 == lower, 2 == higher)
+;; Return
+;;     none
+;;;        places the guess at [guess_input]
+;;         (3 words to catch 100)
+:ask_guess
+IFE A, 0
+SET PC, prompt_guess
+IFE A, 1
+SET PC, last_guess_higher
+IFE A, 2
+SET PC, last_guess_lower
+
+:last_guess_higher
+SET A, higher_string
+SET B, 0
+JSR print_message
+SET PC, prompt_guess
+
+:last_guess_lower
+SET A, lower_string
+SET B, 0
+JSR print_message
+SET PC, prompt_guess
+
+:prompt_guess
+SET A, guess_string
+SET B, 1
+JSR print_message
+SET A, 2
+JSR console
+SET A, guess_input
+SET PC, POP
+
+
+
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+;;
+;;       Ask the user for name and age
+;;
+;;   - used to generate the random seed
+;;   - if a user enters the same name/age
+;;     twice in a row, they will get the same
+;;     randomly generated numbers
+;;
+:ask_users_name_screen
+SET A, enter_your_name_string
+SET B, 0
+JSR print_message
+SET A, 1
+JSR console
+SET A, username_input
+SET B, 1
+JSR read_input
+SET A, enter_your_age_string
+SET B, 2
+JSR print_message
+SET A, 3
+JSR console
+SET A, age_input
+SET B, 3
+JSR read_input
+SET PC, POP
+
+
+
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+;;
+;;    Prints an introduction
+;;    to the game
+;;
+:print_introduction
+SET A, introduction_string1
+SET B, 0
+JSR print_message
+SET A, introduction_string2
+SET B, 1
+JSR print_message
+SET A, introduction_string3
+SET B, 2
+JSR print_message
+SET A, introduction_string4
+SET B, 4
+JSR print_message
+JSR console
+SET PC, POP
+
+
+
+
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+;;
+;;           Generate a random number
+;;
+;;  Parameters
+;;      A - minimum
+;;      B - maximum
+;;
+;;  Returns
+;;      A - The random number
+;;
+;;  Notes
+;;      - Uses [seed] for the random seed and updates
+;;        it with the last un-modulized number generated.
+:pseudorandom
+SET X, [seed]
+MUL X, 0xE3D1
+ADD X, 0x2B69
+SHR X, 3
+SET [seed], X  ;; new seed
+SET I, B
+SUB I, A
+MOD X, I
+ADD X, A
+SET A, X    ;; random number
+SET PC, POP
+
+
+
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+;;
+;;            Generate_Seed
+;;
+;;   Generate a seed from the username and age
+;;      by simply adding the characters together
+;;      and ignoring the overflow
+;;
+;; Parameters
+;;     - none
+;; Returns
+;;     A - the seed
+:generate_seed
+SET I, 0   ;offset
+SET A, 0   ;seed
+:generate_seed_loop
+SET X, username_input
+ADD X, I   ;address
+ADD I, 1
+IFE [X], 0   ;end of name
+SET PC, generate_seed2
+ADD A, [X]
+SET PC, generate_seed_loop
+
+:generate_seed2
+SET I, 0   ;offset
+:generate_seed_loop2
+SET X, age_input
+ADD X, I   ;address
+ADD I, 1
+IFE [X], 0  ;end of age
+SET PC, POP
+ADD [seed], [X]
+SET PC, generate_seed_loop2
+
+
+
+
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+;;
+;;        Print_Message
+;;
+;; Parameters:
+;;    A - pointer to string
+;;    B - line number
+;;
+:print_message
+MUL B, 0x0020
+ADD B, 0x8000
+
+:print_message_loop
+IFE [A], 0x0000
+SET PC, POP
+SET [B], [A]
+ADD [B], 0x7000
+ADD A, 1
+ADD B, 1
+SET PC, print_message_loop
+
+
+
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+;;
+;;        Print_Message_Color
+;;
+;; Parameters:
+;;    A - pointer to string
+;;    B - line number  (x)
+;;    C - column       (y)
+;;    X - Letter color  (0x0 -> 0xf)
+;;    Y - Background color  (0x0 -> 0xf)
+;;    Z - Flashing (1 = on, 0 = off)
+;;
+;;  Notes
+;;      0x0 = black
+;;      0x1 = dark blue
+;;      0x2 = green
+;;      0x3 = teal
+;;      0x4 = dark red
+;;      0x5 = dark purple
+;;      0x6 = orange
+;;      0x7 = light grey
+;;      0x8 = dark grey
+;;      0x9 = light blue
+;;      0xA = light green
+;;      0xB = cyan
+;;      0xC = red
+;;      0xD = purple
+;;      0xE = yellow
+;;      0xF = white
+:print_message_color
+MUL B, 0x0020
+ADD B, 0x8000
+ADD B, C
+SET I, X   ;mask the letter color
+AND I, 0xf
+SHL I, 12
+SET J, Y   ;mask the background color
+AND J, 0xf
+SHL J, 8
+ADD I, J   ;mask for letter+background color
+IFE Z, 1
+BOR I, 0x80   ; I is now the full mast
+              ; b(4 bits letter color)(4 bits background)(1 bit blinking)0000000
+
+
+:print_message_color_loop
+IFE [A], 0x0000
+SET PC, POP    ; end of string
+SET [B], [A]
+BOR [B], I     ;the color mask
+ADD A, 1       ; next character
+ADD B, 1       ; next position
+SET PC, print_message_color_loop
+
+
+
+
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+;;
+;;        Read_Input
+;;
+;; Parameters:
+;;    A - memory address to store input
+;;    B - line number input occurs on
+;;
+:read_input
+MUL B, 0x0020
+ADD B, 0x8000   ;video RAM location
+SET Y, 0        ;index
+:read_input_loop
+IFE [B], 0
+SET PC, POP
+SET X, [B]
+AND X, 0x00FF   ;strip the color
+SET [A], X    ;put character in memory
+ADD B, 1
+ADD A, 1       ;move to next character
+SET PC, read_input_loop
+
+
+
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+;;
+;;        Console
+;;
+;; Parameters:
+;;    A - Line number to echo input on
+;;
+:console
+SET [0x9000], 0
+SET I, 0x8000
+SET B, A
+MUL B, 0x0020
+ADD I, B                ;; the offset to print
+
+:console_loop
+IFE [0x9000], 0         ;; no input
+SET PC, console_loop
+
+IFE [0x9000], 0xD       ;; enter pressed
+SET PC, POP
+IFE [0x9000], 0xA
+SET PC, POP
+
+IFE [0x9000], 0x08      ;; backspace pressed
+JSR backspace
+
+BOR [0x9000], 0x7000
+IFN [0x9000], 0x7008
+SET [I], [0x9000]
+SET [0x9000], 0
+ADD I, 1
+SET PC, console_loop
+
+:backspace
+SUB I, 1
+SET [I], 0x20
+SUB I, 1
+SET PC, POP
+
+
+
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+;;
+;;        Clear screen (cls)  by GR8
+;;
+;;  Parameters
+;;      - none
+;;  Return
+;;      - none
+;;
+:cls
+SET J, POP    ;preserve the return return
+SET SP, 0x8200
+SET [putc_line], 0
+SET [putc_col], 0
+:clear_loop
+SET PUSH, 0
+SET PUSH, 0
+SET PUSH, 0
+SET PUSH, 0
+SET PUSH, 0
+SET PUSH, 0
+SET PUSH, 0
+SET PUSH, 0
+IFG 0x8000,SP
+SET PC, clear_end
+SET PC, clear_loop
+:clear_end
+SET PC, J
+
+
+
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+;;
+;;              atoi
+;;
+;;     Converts a memory address containing characters
+;;     to an integer.
+;;
+;;  Parameters:
+;;     A - address of the string
+;;  Returns:
+;;     A - the integer
+;;
+;;  Notes:
+;;     - does not handle overflow or invalid input (ie 'A' will act weird)
+;;
+:atoi
+SET I, A     ;preserve address to I
+SET B, 0     ;accumulator
+SET A, 0
+:atoi_loop
+IFE [I], 0   ;finished
+SET PC, POP
+SET B, [I]
+SUB B, 48    ;subtract 48 to make it a digit
+ADD A, B     ;accumulate the digit
+ADD I, 1
+IFE [I], 0
+SET PC, POP
+MUL A, 10    ;multiply by order of magnitude
+SET PC, atoi_loop
+
+
+
+
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+;;
+;;           Convert integer to string
+;;                    (itoa)
+;;
+;;
+;;  Paramters
+;;     A - the integer
+;;     B - the address of the destination string
+;;
+;;  Notes
+;;     - address string must be a DAT with 0x5 reserved
+;;       to prevent overflow
+;;     - no support for radix or sign
+;;     - 0 will evaluate '0' or 0x30
+;;
+
+:itoa
+IFE A, 0
+SET PC, itoa_zero_string
+SET J, 0        ; stack count
+
+:itoa_loop1
+IFE A, 0
+SET PC, itoa_loop1_end
+SET I, A
+MOD I, 0xa      ; 10
+DIV A, 0xa      ; 10
+SET PUSH, i     ; the integer at this magnitude
+ADD J, 1        ; next magnitude
+SET PC, itoa_loop1
+
+:itoa_loop1_end
+SET Z, 0        ; character count
+
+:itoa_loop2
+IFE J, 0        ; stack cleared, all values accumulated
+SET PC, itoa_loop2_end
+SET I, POP
+SET A, 0x30     ; add '0' to get the ASCII value
+ADD A, I
+SET C, B
+ADD C, Z
+SET [C], A
+ADD Z, 1        ; next character
+SUB J, 1        ; decrement stack count
+SET PC, itoa_loop2
+
+:itoa_loop2_end SET C, B
+ADD C, Z
+SET [C], 0x0 ; null terminate
+:itoaret SET A, Z
+SET PC, POP
+
+:itoa_zero_string
+SET [B], 0x30
+ADD B, 1
+SET [B], 0
+SET PC, POP
+
+
+
+
+
+:end
+SET PC, end
+
+:enter_your_name_string
+DAT "Enter your name", 0
+
+:enter_your_age_string
+DAT "Enter you age:", 0
+
+:introduction_string1
+DAT "I am thinking of a number", 0
+
+:introduction_string2
+DAT "between 1 and 100", 0
+
+:introduction_string3
+DAT "try and guess it.", 0
+
+:introduction_string4
+DAT "press [ENTER] to start", 0
+
+:guess_string
+DAT "Enter guess:", 0
+
+:higher_string
+DAT "Higher", 0
+
+:lower_string
+DAT "Lower", 0
+
+:correct_string
+DAT "Congradulations, you win!", 0
+
+:number_of_guesses_string
+DAT "After    turns", 0      ;does stop truely stupid players with > 99 guesses
+
+:play_again_string
+DAT "Press [ENTER] to play again.", 0
+
+
+:putc_line
+DAT " "       ; used by cls
+
+:putc_col
+DAT " "       ; used by cls
+
+:seed
+DAT 0
+
+:random_number     ;the random number
+DAT 0
+
+:guess_count
+DAT 0
+
+:guess_count_string  ;number of guesses as a string
+DAT 0, 0, 0
+
+:guess_input
+DAT 0, 0, 0
+
+:last_guess
+DAT 0
+
+:username_input
+DAT 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
+
+:age_input         ;max of 32 characters
+DAT 0, 0, 0, 0, 0, 0
+
+:guesses
+DAT 0
+