#INCLUDE "TI-85.H"

RANDOM		=$39E8	; make a random number

;ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ
; Program Data in Text Memory (80DF)
;ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ

InputLength	=$80DF
InputStr	=$80E0	; 3 bytes + null
Number		=$80E4	; secret number
Score		=$80E6	; number of guesses
;=$80E8

;ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ
; Program Title
;ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ

.org 0
.db "Guess  1.1", 0

;ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ
; PROGRAM BEGINS HERE
;ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ

GameStart:

	CALL_ROM(CLEARLCD)

	CALL_ROM(RANDOM)
	ld a, ($8A45)
	ld (Number), a
	ld a, ($8A44)
	and 15
	ld (Number+1), a

	ld hl, 0
	ld (Score), hl

GuessLoop:

	set 3,(IY+5)	; display white on black
	ld hl, $0000
	ld (CURSOR_ROW), hl
	ld hl, (PROGRAM_ADDR)
	ld de, TitleStr
	add hl, de
	CALL_ROM(D_ZT_STR)
	res 3,(IY+5)	; display black on white

	ld a, 7		; set cursor for input loop
	ld (CURSOR_ROW), a

	sub a
	ld (InputLength), a

KeyLoop:
	call GET_KEY
	cp 2		; Left : backspace
	jr z, K_Left
	cp 9		; Enter : input done
	jr z, K_Enter
	cp $37		; Exit
	ret z	
	cp $12
	jr c, KeyLoop	; loop if key <= $11
	cp $25
	jr nc, KeyLoop	; loop if key >= $25
	ld hl, (PROGRAM_ADDR)
	ld de, XlateTable
	add hl, de
	sub $12
	ld e, a
	ld d, 0
	add hl,de
	ld a, (hl)	; a = ASCII value or 0
	cp 0
	jr z, KeyLoop
	push af	
	ld a, (InputLength)
	ld e, a		; d = 0 from above
	ld (CURSOR_COL), a
	inc a
	cp 4		; 3 character maximum
	jr nc, WordTooLong
	ld (InputLength), a
	ld hl, InputStr
	add hl, de
	pop af
	ld (hl),a
	CALL_ROM(TR_CHARPUT)	; print character
	jr KeyLoop
WordTooLong:
	pop af
	jr KeyLoop

K_Left:
	ld a, (InputLength)
	cp 0
	jr z, KeyLoop
	dec a
	ld (CURSOR_COL), a
	ld (InputLength), a
	ld a, $20	; space
	CALL_ROM(TR_CHARPUT)
	jr KeyLoop
	
K_Enter:
	ld a, (InputLength)
	cp 0
	jr z, KeyLoop

	ld hl, (Score)
	inc hl
	ld (Score), hl

	ld hl, $0F07
	ld (CURSOR_ROW), hl

	ld hl, InputStr
	ld de, 0
	ld a, (InputLength)
	ld b, a
Str2BCD:
	ld a, e
	rrca
	rrca
	rrca
	rrca
	and 15
	ld d, a
	ld a, e
	and 15
	rlca
	rlca
	rlca
	rlca
	ld e, a
	ld a, (hl)
	sub '0'
	or e
	ld e, a
	inc hl
	djnz Str2BCD	
	ld hl, (Number)
	call CP_HL_DE
	ld hl, (PROGRAM_ADDR)
	jr z, GameOver
	ld de, TooHigh
	jr c, GuessTooHigh
	ld de, TooLow
GuessTooHigh:
	add hl, de
	CALL_ROM(D_ZT_STR)
	JUMP_(GuessLoop)

GameOver:
	push hl
	ld de, Right
	add hl, de
	CALL_ROM(D_ZT_STR)

	ld hl, $0002
	ld (CURSOR_ROW), hl
	pop hl
	ld de, ScoreStr
	add hl, de
	CALL_ROM(D_ZT_STR)
	ld hl, $0E02
	ld (CURSOR_ROW), hl
	ld hl, (Score)
	CALL_ROM(D_HL_DECI)

NewGameLoop:
	call GET_KEY
	cp $21		; Y : Play again
	JUMP_Z(GameStart)	
	cp $14		; N : Exit
	jr nz, NewGameLoop
	ret	; END OF PRORGAM

;ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ
;  Strings
;ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ

TitleStr: .db " Guess          v1.1 "
	  .db " Copr. 1995 Dan Eble ",0

TooLow:  .db "Higher", 0
TooHigh: .db "Lower ", 0
Right:   .db "Right", 0

ScoreStr: .db "  Your score:        "
	  .db "  Play again? (Y/N)  ", 0

;ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ
; Scancode to ASCII translation table
;ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ

XlateTable:
; ASCII		; Scancode : Key
.db "3"		; 12 : 3 ('W')
.db "6"		; 13 : 6 ('S')
.db "9"		; 14 : 9 ('N')
.db 0		; 15 : ) ('I')
.db 0		; 16 : TAN ('D')
.db 0		; 17 : CUSTOM
.db 0		; 18 :
.db 0		; 19 : . ('Z')
.db "2"		; 1A : 2 ('V')
.db "5"		; 1B : 5 ('R')
.db "8"		; 1C : 8 ('M')
.db 0		; 1D : ( ('H')
.db 0		; 1E : COS ('C')
.db 0		; 1F : PRGM
.db 0		; 20 : DEL
.db "0"		; 21 : 0 ('Y')
.db "1"		; 22 : 1 ('U')
.db "4"		; 23 : 4 ('Q')
.db "7"		; 24 : 7 ('L')

.end
