SPO600 실습 3 - 수학 및 문자열 - 숫자 추측.
이전 블로그에서 처음 두 부분을 완료했습니다. 이제 다음 부분부터 시작하겠습니다.
난수를 생성하여 저장합니다.
따라서 프로그램은 사용자가 정답을 입력할 때까지 입력을 계속 받아야 하므로 입력 번호('start:' 부분)를 받기 전에 임의의 숫자(답)를 생성하여 저장합니다. 단, 한 게임(라운드) 변경해서는 안됩니다.
; variables
define answer $16
;-------------------------------------------------------
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"
난수를 생성한 후 플레이어의 입력을 난수와 비교합니다.
최종 코드는 다음과 같습니다.
; 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
Wiki 페이지의 저장소에서 Chris Tyler의 코드를 사용했습니다.
Reference
이 문제에 관하여(SPO600 실습 3 - 수학 및 문자열 - 숫자 추측.), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://dev.to/smitgabani/spo600-lab-3-math-and-strings-guessing-numbers-4gpk텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)