; *************************************
; **  THE GAME OF CHASE              **
; **      --FROM DR. DOBBS JOURN   **
; **             MAY 1977          **
; **      --MODIFIED FOR SOL         **
; *************************************
;
;
;
BEGIN:	XRA	A
	STA	YSCORE
	STA	RSCORE
ENTER:	IN	DATA
	LXI	SP,STACK
	CALL	CLEAR
	CALL	SCORE
;
	LXI	H,MS2
	LXI	B,MNAME
	CALL	MSGO
;
DIFF:	LXI	H,MS1
	LXI	B,MDIFF
	CALL	MSGO
	CALL	INP
	ANI	0FH
	STA	DFST
START:	CALL	CLEAR
	CALL	SCORE
	MVI	D,DCORN
	MVI	E,ECORN
TOP:	MVI	B,WIDTH
T1:	CALL	PLOT
	MVI	M,'X'
	INX	D
	CALL	PLOT
	MVI	M,' '
	INX	D
	DCR	B
	JNZ	T1
RSIDE:	MVI	B,HEIGHT
	DCR	E
	DCR	E
R1:	INR	D
	CALL	PLOT
	MVI	M,'X'
	DCR	B
	JNZ	R1
BOTH:	MVI	B,WIDTH-1
B1:	DCR	E
	CALL	PLOT
	MVI	M,' '
	DCR	E
	CALL	PLOT
	MVI	M,'X'
	DCR	B
	JNZ	B1
LSIDE:	MVI	B,HEIGHT
L1:	DCR	D
	CALL	PLOT
	MVI	M,'X'
	DCR	B
	JNZ	L1
;
SEED:	LDA	DFST
	RLC
	MOV	B,A
	MVI	A,1EH
	SUB	B
	MOV	B,A
LOOP1:	CALL	RND
	ANI	0FH
	MOV	D,A
	CALL	RND
	ANI	7FH
	MOV	E,A
	CALL	LIMIT
	JC	LOOP1
	CALL	PLOT
	MOV	A,L
	ANI	0FEH
	MOV	L,A
	MVI	M,'X'
	DCR	B
	JNZ	LOOP1
;
ROSE:	LXI	H,RCORN
	MVI	M,'T'
	LXI	H,RCORN+4
	MVI	M,'Y'
	LXI	H,RCORN+8
	MVI	M,'U'
	LXI	H,RCORN+42H
	MVI	M,5CH
	LXI	H,RCORN+44H
	MVI	M,5EH
	LXI	H,RCORN+46H
	MVI	M,2FH
	LXI	H,RCORN+80H
	M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0FFH
ENDB2:	DB	'YOU HAVE BEATEN THEM -  :'
	DB	0FFH
ENDB3:	DB	'HIT ANY KEY TO PLAY AGAIN.....'
	DB	0FFH
MDIFF:	DB	' LEVEL OF DIFFICULTY (0-9)?'
	DB	0FFH
;
;
STAT	EQU	0FAH
DATA	EQU	0FCH
RDA	EQU	1
STACK	EQU	0650H
RCORN	EQU	0CCF6H
DCORN	EQU	0
ECORN	EQU	2
HEIGHT	EQU	0DH
WIDTH	EQU	19H
LOWD	EQU	DCORN+1
LOWE	EQU	ECORN+1
HID	EQU	DCORN+HEIGHT-1
HIE	EQU	LOWE+WIDTH+WIDTH-4
MS1	EQU	0CFCCH
MS2	EQU	0CD10H
SCR	EQU	0CFBDH
SRE	EQU	0CF77H
MSY	EQU	0CFB8H
MT1	EQU	0CFF5H
ENDM1	EQU	0CD10H
ENDM3	EQU	0CD90H
SH:	DW	1234H
	DW	5678H
DFST:	DB	1
RSCORE	DS	1
YSCORE	DS	1
YOUP	DS	2
ROB1	DS	2
ROB2	DS	2
ROB3	DS	2
ROB4	DS	2
ROB5	DS	2
END

