Lab 2 - Guessing Number Game

In this lab, I was trying to make a Guessing number game.

The rule of game is very simple.

    - Enter number
    - Compare the answer number with user's input
    - If the input is lower than the answer then print "Too low". If the input is higher than the               answer then print "Too high".
    - If user find the answer then game is over.

By using generating random number 'lda $fe' in previous lab I generate random number and save it into answer variable. After than compare with user's input 


Bitmap Code

; A number guessing game

; ROM routine entry points
define        SCINIT        $ff81 ; initialize/clear screen
define        CHRIN        $ffcf ; input character from keyboard
define        CHROUT        $ffd2 ; output character to screen
define        SCREEN        $ffed ; get screen size
define        PLOT        $fff0 ; get/set cursor coordinates

; zeropage variables
define        PRINT_PTR    $10
define        PRINT_PTR_H    $11
define        value_h        $15
define        answer    $16

; absolute variables
define        GETNUM_1    $0080
define        GETNUM_2    $0081

; constants

; --------------------------------------------------------

        jsr SCINIT
        jsr CHRIN

        jsr PRINT

dcb "A",32,"n","u","m","b","e","r",32
dcb "g","u","e","s","s","i","n","g",32,"g","a","m","e",00

        lda $fe        ; generate random number
        and #$99       ; mask out low two bits (=numbers 0-99)
        sta answer     ; store the answer

start:        jsr PRINT

dcb $0d,$0d,"E","n","t","e","r",32,"a",32,"n","u","m","b","e","r"
dcb "(","0","-","9","9",")",":"
dcb 32,32,32,32,32,32,32,32,00

        lda #$00
        sta value_h

        jsr GETNUM

        sed        ; set decimal
        cmp answer ; compare user input to the answer
        cld        ; clear decimal flag (switches into binary math mode)

        bcc toolow    ; if input value is less than the answer, go to toolow
        beq identical    ; if input value is same as answer, go to identical

        ; otherwise (if input value is greater than the answer), go to toohigh
toohigh:    pha        ; push the accumulator
        jsr PRINT

dcb "T","o","o",32,"h","i","g","h",32
dcb 32,32,32,32,32,32,32
dcb 32,32,32,32,32,32,32
dcb 32,32,32,32,32,32,32
dcb 00

        jsr start

toolow:        pha        ; push the accumulator
        jsr PRINT

dcb "T","o","o",32,"l","o","w",32
dcb 32,32,32,32,32,32,32
dcb 32,32,32,32,32,32,32
dcb 32,32,32,32,32,32,32
dcb 00
        jsr start

identical:    pha        ; push the accumulator
        jsr PRINT

dcb "C","o","r","r","e","c","t",".",32
dcb "T","h","e",32,"a","n","s","w","e","r",32,"w","a","s",":"
dcb 32,32,32,32,32
dcb 00

        lda value_h
        beq low_digits
        lda #$31
        jsr CHROUT
        jmp draw_100s

low_digits:    lda answer
        and #$f0
        beq ones_digit

draw_100s:    lda answer
        lsr
        lsr
        lsr
        lsr
        ora #$30
        jsr CHROUT

ones_digit:    lda answer
        and #$0f
        ora #$30
        jsr CHROUT

        brk        ; if correct, break the program

; --------------------------------------------------------
; Print a message
; 
; Prints the message in memory immediately after the 
; JSR PRINT. The message must be null-terminated and
; 255 characters maximum in length.

PRINT:        pla    ; pull the accumulator
        clc    ; clear carry flag (C) - required before using ADC
        adc #$01    ; add with carry
        sta PRINT_PTR
        pla
        sta PRINT_PTR_H

        tya    ; transfer Y to A
        pha    ; push the accumulator

        ldy #$00
print_next:    lda (PRINT_PTR),y
        beq print_done

        jsr CHROUT
        iny
        jmp print_next

print_done:    tya
        clc
        adc PRINT_PTR
        sta PRINT_PTR

        lda PRINT_PTR_H
        adc #$00
        sta PRINT_PTR_H

        pla
        tay

        lda PRINT_PTR_H
        pha
        lda PRINT_PTR
        pha

        rts

; ---------------------------------------------------
; GETNUM - get a 2-digit decimal number
;
; Returns A containing 2-digit BCD value

GETNUM:        txa        ; transfer X to accumulator
        pha        ; push the accumulator
        tya        ; transfer Y to A
        pha

        ldx #$00    ; count of digits received
        stx GETNUM_1    ; store the X register
        stx GETNUM_2


getnum_cursor:    lda #$a0    ; black square
        jsr CHROUT
        lda #$83    ; left cursor
        jsr CHROUT

getnum_key:    jsr CHRIN
        cmp #$00
        beq getnum_key

        cmp #$08    ; BACKSPACE
        beq getnum_bs

        cmp #$0d    ; ENTER
        beq getnum_enter

        cmp #$30    ; "0"
        bmi getnum_key

        cmp #$3a    ; "9" + 1
        bmi getnum_digit

        jmp getnum_key

getnum_enter:    cpx #$00
        beq getnum_key

        lda #$20
        jsr CHROUT
        lda #$0d
        jsr CHROUT

        lda GETNUM_1

        cpx #$01
        beq getnum_done

        asl
        asl
        asl
        asl
        ora GETNUM_2

getnum_done:    sta GETNUM_1
        pla
        tay
        pla
        tax
        lda GETNUM_1

        rts

getnum_digit:    cpx #$02
        bpl getnum_key
        pha
        jsr CHROUT
        pla
        and #$0f
        sta GETNUM_1,x
        inx
        jmp getnum_cursor

getnum_bs:    cpx #$00
        beq getnum_key
        lda #$20
        jsr CHROUT
        lda #$83
        jsr CHROUT
        jsr CHROUT
        lda #$20
        jsr CHROUT
        lda #$83
        jsr CHROUT
        dex
        lda #$00
        sta GETNUM_1,x
        jmp getnum_cursor

Output

A number guessing game           

                                                                                                                               Enter a number(0-99):        30                                                 Too low                          

                                                                                                                               Enter a number(0-99):        60                                                 Too low                           

                                                                                                                              Enter a number(0-99):        70                                                 Too low                            

                                                                                                                             Enter a number(0-99):        80                                                 Correct. The answer was:     80                                                                                   

댓글

이 블로그의 인기 게시물

Project stage-3 (testing & reflection)

Understanding SIMD and SVE: Harnessing Parallelism for Enhanced Performance

Lab3 - AArch64