	title	REVERSI for SOL
	name	('REVERSI')

; DASMed version of the SOL game REVERSI - Original version

_COMP	equ	07fh
_HUMAN	equ	005h

maxcol	equ	64		; Max columns

MSB	equ	10000000b

_EOT	equ	04h		; Indicator for end of line
_NL	equ	18h		; Indicator for new line

lc000	equ	0c000h		; Warmstart
;
lcc00	equ	0cc00h
lcc52	equ	lcc00+ 1*maxcol+18	;;0cc52h
lce40	equ	lcc00+ 9*maxcol+ 0	;;0ce40h
lcec0	equ	lcc00+11*maxcol+ 0	;;0cec0h
lcedc	equ	lcc00+11*maxcol+28	;;0cedch
lceec	equ	lcc00+11*maxcol+44	;;0ceech
lcf00	equ	lcc00+12*maxcol+ 0	;;0cf00h
lcf35	equ	lcc00+12*maxcol+53	;;0cf35h
lcf40	equ	lcc00+13*maxcol+ 0	;;0cf40h
lcf80	equ	lcc00+14*maxcol+ 0	;;0cf80h

; Game starts at 0x0000

	.phase	0000h

	nop
	nop
	nop
	nop
;
	jp	l065a
;
l0007:
	db	25
	db	'R'+MSB,' E ','V'+MSB,' E ','R'+MSB,' S ','I'+MSB,_NL
	db	24
	db	'BY RAY G. WHITE',_NL
	db	0
	db	'Reversi is a game that challenges your skill vs. the computer''s.',_NL
	db	0
	db	'The game is played on an 8 X 8 "board" on which the "pieces" are',_NL
	db	0
	db	'placed. In this game you are the white(',_HUMAN,') player and the',_NL
	db	0
	db	'computer is the black(',_COMP,') player. A (.) indicates locations where',_NL
	db	0
	db	'no piece has been placed. At the start of the game the four',_NL
	db	0
	db	'centre locations are filled with pieces. When it is your turn to',_NL
	db	0
	db	'place a piece, you must do so in such a way that at least one of',_NL
	db	0
	db	'the computer''s pieces lie between one of your pieces and the',_NL
	db	0
	db	'piece you just placed. All of the computer''s pieces that are',_NL
	db	0
	db	'positioned that way are then converted into your pieces. The',_NL
	db	0
	db	'game ends when the board is full, and the winner is the one',_NL
	db	0
	db	'with the most pieces on the board.',_NL
	db	0,_NL
	db	20
	db	'*TYPE SPACE TO BEGIN*'
	db	_NL,_EOT
l031d:
	db	18
	db	'MY SCORE:     YOUR SCORE:'
	db	_NL,_EOT
l0339:
	db	21
	db	'1  2  3  4  5  6  7  8'
	db	_NL,_EOT
l0352:
	db	27
	db	'TIE GAME!'
	db	_NL,_EOT
l035e:
	db	18
	db	'YOU LOSE!!! (HAR! HAR! HAR!)'
	db	_NL,_EOT
l037d:
	db	19
	db	'YOU WIN!!! (WAAAA! SNIFF!)'
	db	_NL,_EOT
;
; Hilited version of
; 'BOTH PLAYERS ARE UNABLE TO MOVE '
;
l039a:
	db	16
	db	0c2h,0cfh,0d4h,0c8h,0a0h,0d0h
	db	0cch,0c1h,0d9h,0c5h,0d2h,0d3h,0a0h
	db	0c1h,0d2h,0c5h,0a0h,0d5h,0ceh,0c1h
	db	0c2h,0cch,0c5h,0a0h,0d4h,0cfh,0a0h
	db	0cdh,0cfh,0d6h,0c5h,' '
	db	_NL,_EOT
;
; Hilited version of
; 'ILLEGAL ENTRY-TRY AGAIN'
;
l03bd:
	db	20
	db	0c9h,0cch,0cch,0c5h,0c7h,0c1h
	db	0cch,0a0h,0c5h,0ceh,0d4h,0d2h,0d9h
	db	0adh,0d4h,0d2h,0d9h,0a0h,0c1h,0c7h
	db	0c1h,0c9h,0ceh
	db	_NL,_EOT
l03d7:
	db	7
	db	'ENTER YOUR MOVE IN THE FORM(X,Y),(L TO LEAVE):'
	db	_NL,_EOT
l0408:
	db	17
	db	'THE COMPUTER IS UNABLE TO MOVE'
	db	_NL,_EOT
l0429:
	db	21
	db	'YOU ARE UNABLE TO MOVE'
	db	_NL,_EOT
l0442:
	db	20
	db	'DO YOU WANT TO GO FIRST?'
	db	_NL,_EOT
;
	db	0,0,0,0
l0461:
	db	'........'			; Line 1
	db	'........'			; Line 2
	db	'........'			; Line 3
	db	'...',_HUMAN,_COMP,'...'	; Line 4
	db	'...',_COMP,_HUMAN,'...'	; Line 5
	db	'........'			; Line 6
	db	'........'			; Line 7
	db	'........'			; Line 8
l04a1:
	ds	8*8
;
	ds	15
l04f0:
	db	0
l04f1:
	db	0
l04f2:
	db	0,0
;
	db	0,0,0,0,0,0,0,0,0,0,0,0
l0500:
	db	0
l0501:
	db	0
;
	db	0,0
l0504:
	db	0
l0505:
	db	0
l0506:
	db	0,0
l0508:
	db	0
l0509:
	db	0
;
	db	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
l0522:
	db	0		; Counterpart
l0523:
	db	0		; Base pointer to board
l0524:
	db	0		; Board column
l0525:
	db	0		; Board row
l0526:
	dw	0		; Board pointer
;
	db	0,0,0,0,0,0,0,0,0
;
; Clear the game board
;
l0531:
	ld	hl,lcc00	; Point to video board
l0534:
	ld	(hl),' '	; Clear board
	inc	hl
	ld	a,HIGH 0d000h -1
	sub	h		; Test top reached
	jp	p,l0534		; .. nope
	ret
;
	nop
	nop
	nop
;
; Put string ^DE to video memory starting with ^BC
;
; String is organized as:
;
;	DB	COL		; Start column
;
;	DB	<string>	; String
;	DB	_NL		; New line indicator
;
;	.... more strings
;
;	DB	_EOT		; End indicator
;
l0541:
	ld	h,b		; Copy address
	ld	l,c
	ld	a,(de)		; Get byte
	sub	_EOT		; Test end
	ret	z		; Yeap
	ld	a,(de)		; Get byte
	add	a,l		; Fix pointer
	jp	nc,l054d	; Skip
	inc	h		; Fix row
l054d:
	ld	l,a		; Set column
l054e:
	inc	de
	ld	a,(de)		; Get character
	sub	_NL		; Test new line
	jp	nz,l0561	; Nope
	inc	de
	ld	a,c		; Get column
	add	a,maxcol	; Advance columns
	jp	nc,l055d
	inc	b		; Next row
l055d:
	ld	c,a
	jp	l0541		; Print next
l0561:
	ld	a,(de)		; Get character
	ld	(hl),a		; Put to video ram
	inc	hl
	jp	l054e		; Test end
;
	nop
	nop
	nop
;
; Input character into Accu
;
l056a:
	call	l0dc2		; Test data available
	jp	z,l056a		; Nope, wait
	ret
;
; Output the game board
;
l0571:
	call	l0531		; Clear the game board
	ld	bc,lcc00	; Point to video board
	ld	de,l0339
	call	l0541		; Give upper column line
	ld	b,'1'		; Init left number
	ld	hl,lcc52	; Init board position
	ld	de,l0461	; Init board lines
l0585:
	ld	(hl),b		; Store number on the left
	ld	c,1		; Init column count
l0588:
	inc	hl
	inc	hl
	inc	hl
	ld	a,(de)		; Unpack lines
	ld	(hl),a
	inc	de
	inc	c		; Advance column count
	ld	a,8+1		; Test this line done
	sub	c
	jp	nz,l0588	; Nope
	inc	hl
	inc	hl
	inc	hl
	ld	(hl),b		; Store number on the right
	inc	b		; Advance line count
	ld	a,'8'+1		; Test all done
	sub	b
	jp	z,l05ab		; Yeap
	ld	a,l
	add	a,maxcol-8*3-3
	ld	l,a		; Advanve to next line
	ld	a,h
	adc	a,0
	ld	h,a
	jp	l0585
l05ab:
	ld	bc,lce40
	ld	de,l0339	; Give lower column line
	call	l0541
	ret
;
	nop
	nop
;
; Test any field free on the board
;
l05b7:
	ld	hl,l0461	; Set board line pointer
l05ba:
	ld	a,(hl)		; Get field
	sub	'.'		; Test empty
	ret	z		; Yeap
	inc	hl
	ld	a,LOW l04a1+1
	sub	l		; Test all scanned
	jp	nz,l05ba
;
; Game board filled
;
	call	l0571		; Output the game board
	ld	bc,lcec0
	ld	de,l031d
	call	l0541		; Give score message
l05d1:
	ld	b,0		; Clear human count
	ld	c,0		; Clear computer count
	ld	hl,l0461	; Set board line pointer
l05d8:
	ld	a,(hl)		; Get piece
	sub	_COMP		; Test computer
	jp	z,l05e2		; Yeap
	nop
	jp	l0d5d		; Check human
l05e2:
	inc	c		; Bump computer count
l05e3:
	inc	hl		; Bump board
	ld	a,LOW l04a1
	sub	l		; Test all scanned
	jp	nz,l05d8
;
; Got a winner, determine who
;
	ld	hl,lcedc
	ld	d,c		; Get computer pieces
	call	l0640		; Store as number
	ld	hl,lceec
	ld	d,b		; Get human pieces
	call	l0640		; Store as number
	ld	a,b		; Determine the winner
	sub	c
	jp	z,l0631		; It's a tie
	jp	m,l0637		; Computer wins
	ld	de,l037d	; Human wins
l0603:
	ld	bc,lcf40
	call	l0541		; Tell winner
	ld	de,l0442
	ld	bc,lcf80
	call	l0541		; Ask for going first
	ld	hl,l0461	; Set board line pointer
l0615:
	ld	(hl),'.'	; Reset board
	inc	hl
	ld	a,l
	sub	LOW l04a1
	jp	nz,l0615
	ld	hl,l0461+3*8+3
	ld	(hl),_HUMAN	; Init human
	inc	hl
	ld	(hl),_COMP	; And computer
	ld	hl,l0461+4*8+3
	ld	(hl),_COMP	; And vice versa in next line
	inc	hl
	ld	(hl),_HUMAN
	jp	l067e
l0631:
	ld	de,l0352	; Tie
	jp	l0603
l0637:
	ld	de,l035e	; Human loses
	jp	l0603
;
	nop
	nop
	nop
;
; Convert number in reg D to ASCII ^HL
;
l0640:
	ld	(hl),'0'	; Init number
	dec	hl
	jp	l0db6
l0646:
	inc	(hl)		; Advance count
	ld	a,(hl)
	sub	'9'+1		; Test overflow
	jp	nz,l0652
	ld	(hl),'0'	; Clear digit
	dec	hl
	inc	(hl)		; Advance tens
	inc	hl
l0652:
	dec	d		; Count down
	ret	z
	jp	l0646
;
	nop
	nop
	nop
;
; Init the game
;
l065a:
	ld	a,0
	out	(0feh),a	; Clear VDM port
	ld	bc,lcc00	; Point to video board
	ld	de,l0007
	call	l0531		; Clear the game board
	call	l0541		; Give rules
l066a:
	call	l056a		; Read character
	sub	' '		; Wait for blank
	jp	nz,l066a
	call	l0571		; Output the game board
	ld	bc,lcf00
	ld	de,l0442
	call	l0541		; Ask for going first
l067e:
	call	l056a		; Read character
	sub	'L'		; Test leave
	jp	z,l0693
	sub	'N'-'L'		; Test 'N'o
	jp	z,l09a8
	sub	'Y'-'N'		; Test 'Y'es
	jp	nz,l067e	; Wait if no valid selection
	jp	l07ca
l0693:
	call	l0531		; Clear the game board
	jp	lc000		; Enter OS
;
	nop
	nop
	nop
;
; Test direction ok
; ENTRY	Reg B holds column
;	Reg C holds row
;	Reg D holds counterpart of piece
;	Reg E holds start of board
; EXIT	Reg D holds result - 0fh indicates match
;
l069c:
	ld	hl,256 * HIGH l0461
	nop
	ld	a,e
	ld	(l0523),a	; Save base pointer
	ld	a,d
	ld	(l0522),a	; Save counterpart
	ld	d,0		; Init result
	nop
	ld	a,c
	ld	(l0525),a	; Save row
	ld	a,b
	ld	(l0524),a	; Save column
	ld	l,e		; Init base column
	ld	a,l		; Get column
	add	a,b		; Add column
l06b6:
	dec	c		; Count down position
	jp	z,l06bf		; Got it
	add	a,8		; Add offset to next line
	jp	l06b6
l06bf:
	ld	l,a		; Set pointer
	ld	a,(hl)		; Get piece
	sub	'.'		; Test free
	ret	nz		; Nope
	ld	(l0526),hl	; Save pointer
	jp	l06d3		; Test direction ok
;
; Load pointer and row/col
;
l06ca:
	ld	hl,(l0524)	; Load ..
	ld	b,l		; .. column
	ld	c,h		; .. row
	ld	hl,(l0526)	; Load board pointer
	ret
;
; Test direction ok - reg D holds result
;
l06d3:
	call	l06ca		; Load pointer and row/col
	ld	d,0		; Set up
	ld	e,2
	call	l0749		; Move thru the board
	ld	a,d
	sub	0fh
	ret	z
	call	l06ca		; Load pointer and row/col
	ld	d,1		; Set up right
	ld	e,2
	call	l0749		; Move thru the board
	ld	a,d
	sub	0fh
	ret	z
	call	l06ca		; Load pointer and row/col
	ld	d,1		; Set right
	ld	e,0
	call	l0749		; Move thru the board
	ld	a,d
	sub	0fh
	ret	z
	call	l06ca		; Load pointer and row/col
	ld	d,1		; Set down right
	ld	e,1
	call	l0749		; Move thru the board
	ld	a,d
	sub	0fh
	ret	z
	call	l06ca		; Load pointer and row/col
	ld	d,0		; Set down
	ld	e,1
	call	l0749		; Move thru the board
	ld	a,d
	sub	0fh
	ret	z
	call	l06ca		; Load pointer and row/col
	ld	d,2		; Set down left
	ld	e,1
	call	l0749		; Move thru the board
	ld	a,d
	sub	0fh
	ret	z
	call	l06ca		; Load pointer and row/col
	ld	d,2		; Set left
	ld	e,0
	call	l0749		; Move thru the board
	ld	a,d
	sub	0fh
	ret	z
	call	l06ca		; Load pointer and row/col
	ld	d,2		; Set up left
	ld	e,2
	call	l0749		; Move thru the board
	ld	a,d
	sub	0fh
	ret	z
	ld	d,0		; Set no hit
	ret
;
	nop
	nop
	nop
;
; Move thru the board
; ENTRY	Reg B holds column
;	Reg C holds row
;	Reg D holds horizontal direction 0 0 1 1 1 2 2 2
;	Reg E holds  vertical  direction 1 2 0 1 2 0 1 2
;	(0 means no motion, 1 means right or up, 2 means left or down)
;	Reg HL points to free board location
; EXIT	Reg D holds result - 0fh indicates match
;
l0749:
	ld	a,d		; Get direction
	sub	2		; Test left
	jp	nz,l0755	; Nope
	dec	l		; Get previous location
	dec	b		; Get previous column
	ret	z		; End if left corner
	jp	l075e
l0755:
	ld	a,l		; Get pointer
	add	a,d		; Add 0 or 1
	ld	l,a
	ld	a,b		; Get column
	add	a,d		; Add 0 or 1
	ld	b,a
	sub	8+1		; Test right corner
	ret	z		; End if so
l075e:
	ld	a,e		; Get direction
	sub	2		; Test down
	jp	nz,l076d	; Nope
	dec	c		; Get previous row
	ret	z		; End if upper row
	ld	a,l
	sub	8		; Set pointer to previous row
	ld	l,a
	jp	l077c
l076d:
	ld	a,e		; Get vertical direction
	sub	0		; Test motion
	jp	z,l077c		; Nope, leave
	inc	c		; Get next row
	ld	a,l
	add	a,8		; Set pointer to next row
	ld	l,a
	ld	a,c		; Get row
	sub	8+1		; End if lower row
	ret	z
;
; At this point we got the adjacent pointer
;
l077c:
	ld	a,(l0522)	; Get counterpart
	sub	(hl)		; Test match
	ret	nz		; Nope
;
; Loop for finding adjacents
;
l0781:
	ld	a,d		; Get direction
	sub	2		; Test left
	jp	nz,l0793	; Nope
	dec	l		; Get previous location
	dec	b		; Get previous column
	ret	z		; End if left corner
	nop
	nop
	nop
	nop
	nop
	nop
	jp	l079c
l0793:
	ld	a,l		; Get pointer
	add	a,d		; Add 0 or 1
	ld	l,a
	ld	a,b		; Get column
	add	a,d		; Add 0 or 1
	ld	b,a
	sub	8+1		; Test right corner
	ret	z		; End if so
l079c:
	ld	a,e		; Get direction
	sub	2		; Test down
	jp	nz,l07ab	; Nope
	dec	c		; Get previous row
	ret	z		; End if upper row
	ld	a,l
	sub	8		; Set pointer to previous row
	ld	l,a
	jp	l07ba
l07ab:
	ld	a,e		; Get vertical direction
	sub	0		; Test motion
	jp	z,l07ba		; Nope, leave
	inc	c		; Get next row
	ld	a,l
	add	a,8		; Set pointer to next row
	ld	l,a
	ld	a,c		; Get row
	sub	8+1		; End if lower row
	ret	z
;
; At this point we got the next adjacent pointer
;
l07ba:
	ld	a,(hl)		; Get piece
	sub	'.'		; Test free
	ret	z		; Yeap
	ld	a,(l0522)	; Get counterpart
	sub	(hl)		; Test match
	jp	z,l0781		; Yeap
	ld	d,0fh		; Got own piece
	ret
;
	nop
	nop
;
; Human starts
;
l07ca:
	call	l05b7		; Test any free location
	call	l0571		; Output the game board
l07d0:
	ld	a,1		; Init ..
	ld	(l04f0),a	; .. row 
	ld	(l04f1),a	; .. column
l07d8:
	ld	e,LOW l0461 - 1
	ld	d,_COMP
	ld	hl,(l04f0)
	ld	b,h
	ld	c,l
	call	l069c		; Test direction ok
	ld	a,d
	sub	0fh		; Test it
	jp	z,l080d		; Ok
	ld	hl,l04f0
	inc	(hl)		; Bump row
	ld	a,(hl)
	sub	8+1		; Test done
	jp	nz,l07d8	; Nope
	ld	(hl),1		; Reset row
	inc	hl
	inc	(hl)		; Bump column
	ld	a,(hl)
	sub	8+1		; Test done
	jp	nz,l07d8	; Nope
	jp	l0d67		; Test move possible
l0801:
	ld	de,l0429
	call	l0541		; Tell human cannot move
	jp	l09a8		; Computer moves
;
	nop
	nop
	nop
;
; Input a move
;
l080d:
	ld	bc,lcf00
	ld	de,l03d7
	call	l0541		; Tell what we expect
l0816:
	ld	hl,lcf35-1	; Init pointer
	nop
	nop
	call	l098c		; Get x-digit
	ld	(hl),a		; Store it
	ld	b,a
	sub	'1'		; Test range
	jp	p,l083c		; Ok
l0825:
	ld	bc,lcf40
	ld	de,l03bd
	call	l0541		; Tell illegal entry
	ld	hl,lcf35
	ld	(hl),' '	; Clear digits
	inc	hl
	ld	(hl),' '
	inc	hl
	ld	(hl),' '
	jp	l0816		; Retry
l083c:
	sub	7+1		; Test range
	jp	p,l0825		; Error
	ld	a,b		; Get digit
	sub	'0'		; Make binary
	ld	b,a
	call	l098c		; Get delimiter
	ld	(hl),a		; Store it
	sub	','
	jp	nz,l0825
	call	l098c		; Get y-digit
	ld	(hl),a		; Store it
	ld	c,a
	sub	'1'		; Test range
	jp	m,l0825
	sub	7+1
	jp	p,l0825
	ld	a,c		; Get digit
	sub	'0'		; Make binary
	ld	c,a
	ld	l,b
	ld	h,c
	ld	(l04f2),hl	; Store row and column
	ld	e,LOW l0461 - 1
	ld	d,_COMP
	call	l069c		; Test direction ok
	ld	a,d
	sub	0fh		; Test it
	jp	nz,l0825	; Not ok
	ld	hl,(l04f2)	; Get row and column
	ld	b,l
	ld	c,h
	ld	(l0524),hl	; Save row and column
	ld	hl,l0461-1	; Init board
	ld	a,l
	add	a,b		; Add column
l0880:
	dec	c
	jp	z,l0889
	add	a,8		; Add row
	jp	l0880
l0889:
	ld	l,a
	jp	l099d
;
; Flip the pieces to human ones
;
l088d:
	call	l06ca		; Load pointer and row/col
	ld	d,0		; Set up
	ld	e,2
	call	l0749		; Move thru the board
	ld	a,d
	sub	0fh
	jp	nz,l08a7
	ld	hl,(l0526)	; Load board pointer
	ld	d,2*8+8
	ld	e,_COMP
	call	l0966		; Move up
l08a7:
	call	l06ca		; Load pointer and row/col
	ld	d,1		; Set up right
	ld	e,2
	call	l0749		; Move thru the board
	ld	a,d
	sub	0fh
	jp	nz,l08c1
	ld	hl,(l0526)	; Load board pointer
	ld	d,2*8+7
	ld	e,_COMP
	call	l0966		; Move up right
l08c1:
	call	l06ca		; Load pointer and row/col
	ld	d,1		; Set right
	ld	e,0
	call	l0749		; Move thru the board
	ld	a,d
	sub	0fh
	jp	nz,l08db
	ld	hl,(l0526)	; Load board pointer
	ld	d,1
	ld	e,_COMP
	call	l0966		; Move right
l08db:
	call	l06ca		; Load pointer and row/col
	ld	d,1		; Set down right
	ld	e,1
	call	l0749		; Move thru the board
	ld	a,d
	sub	0fh
	jp	nz,l08f5
	ld	hl,(l0526)	; Load board pointer
	ld	d,8+1
	ld	e,_COMP
	call	l0966		; Move down right
l08f5:
	call	l06ca		; Load pointer and row/col
	ld	d,0		; Set down
	ld	e,1
	call	l0749		; Move thru the board
	ld	a,d
	sub	0fh
	jp	nz,l090f
	ld	hl,(l0526)	; Load board pointer
	ld	d,8
	ld	e,_COMP
	call	l0966		; Move down
l090f:
	call	l06ca		; Load pointer and row/col
	ld	d,2		; Set down left
	ld	e,1
	call	l0749		; Move thru the board
	ld	a,d
	sub	0fh
	jp	nz,l0929
	ld	hl,(l0526)	; Load board pointer
	ld	d,8-1
	ld	e,_COMP
	call	l0966		; Move down left
l0929:
	call	l06ca		; Load pointer and row/col
	ld	d,2		; Set left
	ld	e,0
	call	l0749		; Move thru the board
	ld	a,d
	sub	0fh
	jp	nz,l0943
	ld	hl,(l0526)	; Load board pointer
	ld	d,2*8+1
	ld	e,_COMP
	call	l0966		; Move left
l0943:
	call	l06ca		; Load pointer and row/col
	ld	d,2		; Set up left
	ld	e,2
	call	l0749		; Move thru the board
	ld	a,d
	sub	0fh
	jp	nz,l095d
	ld	hl,(l0526)	; Load board pointer
	ld	d,2*8+9
	ld	e,_COMP
	call	l0966		; Move up left
l095d:
	call	l05b7		; Test any free location
	jp	l09a8		; Computer moves
;
	nop
	nop
	nop
;
; Flip pieces
; ENTRY	Reg HL holds board pointer
;	Reg D holds direction
;	Reg E holds counterpart
;
l0966:
	ld	a,d		; Get direction
	sub	2*8		; Test range
	jp	m,l0973		; Next right direction
	ld	a,l
	add	a,2*8		; Fix for left
	sub	d
	jp	l0975
l0973:
	ld	a,l
	add	a,d
l0975:
	ld	l,a		; Set pointer
	ld	a,(hl)		; Get piece
	sub	e		; Test counterpart
	ret	nz		; Nope
	ld	a,e		; Test who we are
	sub	_COMP
	jp	z,l0984
	ld	(hl),_COMP	; Set computer
	jp	l0966
l0984:
	ld	(hl),_HUMAN	; Set human
	jp	l0966
;
	nop
	nop
	nop
;
; Input a character for human's move
;
l098c:
	inc	hl
	ld	(hl),' '+MSB	; Set cursor
	call	l056a		; Read character
	ld	c,a
	sub	'L'		; Test 'L'eave
	jp	z,l0693		; Yeap
	ld	a,c
	ret
;
	nop
	nop
	nop
;
; Part of conversion
;
; **** PATCH - MAY BE INSERTED INTO CODE ****
;
l099d:
	ld	(l0526),hl	; Save board pointer
	ld	(hl),_HUMAN	; Set human
	jp	l088d
;
	nop
	nop
	nop
;
; Computer starts or moves
;
l09a8:
	ld	b,1
	ld	c,1
	ld	d,_HUMAN
	ld	e,LOW l0461 - 1
	call	l069c		; Test direction ok
	ld	a,d
	sub	0fh		; Test it
	jp	z,l0c03		; Ok
	ld	b,1
	ld	c,8
	ld	d,_HUMAN
	ld	e,LOW l0461 - 1
	call	l069c		; Test direction ok
	ld	a,d
	sub	0fh		; Test it
	jp	z,l0c03		; Ok
	ld	b,8
	ld	c,1
	ld	d,_HUMAN
	ld	e,LOW l0461 - 1
	call	l069c		; Test direction ok
	ld	a,d
	sub	0fh		; Test it
	jp	z,l0c03		; Ok
	ld	b,8
	ld	c,8
	ld	d,_HUMAN
	ld	e,LOW l0461 - 1
	call	l069c		; Test direction ok
	ld	a,d
	sub	0fh		; Test it
	nop
	jp	z,l0c03		; Ok
	ld	hl,1*256+1
	ld	(l0500),hl	; Init row and column
	ld	hl,2
	jp	l0a5c
;
;
;
l09f9:
	ld	hl,l0508
	ld	bc,l04a1	;; Original l05a1 *** ERROR *** ???!!!
	ld	(hl),0		; Clear piece count
	inc	hl
	ld	(hl),0
	dec	hl
l0a05:
	ld	a,(bc)		; Get piece
	ld	d,a		; Save
	sub	'.'		; Test empty
	jp	nz,l0a16	; Nope
l0a0c:
	inc	bc		; Advance board pointer
	ld	a,c		; Test end
	sub	LOW (l04a1+8*8)
	ret	z		; Yeap
	nop
	nop
	jp	l0a05
l0a16:
	ld	a,d
	sub	_COMP		; Test own piece
	jp	nz,l0a27	; Nope
	inc	(hl)		; Advance count
	jp	nc,l0a0c
	inc	hl
	ld	(hl),0
	dec	hl
	jp	l0a0c
l0a27:
	dec	(hl)		; Count dowm
	jp	nc,l0a0c
	inc	hl
	ld	(hl),1
	dec	hl
	jp	l0a0c
l0a32:
	jp	l0d10
l0a35:
	ld	d,_HUMAN
	ld	e,LOW l0461 - 1
	call	l069c		; Test direction ok
	ld	a,d
	sub	0fh		; Test it
	jp	z,l0a65		; Ok
l0a42:
	ld	hl,l0500	; Point to column
	inc	(hl)		; Advance it
	ld	a,(hl)
	sub	8+1		; Test all scanned
	jp	nz,l0a32	; Nope
	ld	(hl),1		; Reset column
	inc	hl
	inc	(hl)		; Advance row
	ld	a,(hl)
	sub	8+1		; Test all scanned
	jp	z,l0c0e		; Yeap
	jp	l0a32
;
	nop
	nop
	nop
;
; Part of computer move
;
; **** PATCH - MAY BE INSERTED INTO CODE ****
;
l0a5c:
	ld	(l0504),hl
	jp	l0a32
;
	nop
	nop
	nop
;
; Computer moves, cont'd
;
l0a65:
	ld	hl,l0461	; Set board line pointers
	ld	bc,l04a1
l0a6b:
	ld	a,(hl)		; Unpack board
	ld	(bc),a
	inc	hl
	inc	bc
	ld	a,l
	sub	LOW l04a1
	jp	nz,l0a6b
	ld	hl,(l0526)	; Load board pointer
	ld	a,l
	add	a,maxcol	; Next video line
	ld	l,a
	ld	(l0526),hl	; Save board pointer
	ld	(hl),_COMP	; Set computer
	call	l06ca		; Load pointer and row/col
	ld	d,0		; Set up
	ld	e,2
	call	l0749		; Move thru the board
	ld	a,d
	sub	0fh
	jp	nz,l0a9b
	ld	hl,(l0526)	; Load board pointer
	ld	d,2*8+8
	ld	e,_HUMAN
	call	l0966		; Move up
l0a9b:
	call	l06ca		; Load pointer and row/col
	ld	d,1		; Set up right
	ld	e,2
	call	l0749		; Move thru the board
	ld	a,d
	sub	0fh
	jp	nz,l0ab5
	ld	hl,(l0526)	; Load board pointer
	ld	d,2*8+7
	ld	e,_HUMAN
	call	l0966		; Move up right
l0ab5:
	call	l06ca		; Load pointer and row/col
	ld	d,1		; Set right
	ld	e,0
	call	l0749		; Move thru the board
	ld	a,d
	sub	0fh
	jp	nz,l0acf
	ld	hl,(l0526)	; Load board pointer
	ld	d,1
	ld	e,_HUMAN
	call	l0966		; Move right
l0acf:
	call	l06ca		; Load pointer and row/col
	ld	d,1		; set down right
	ld	e,1
	call	l0749		; Move thru the board
	ld	a,d
	sub	0fh
	jp	nz,l0ae9
	ld	hl,(l0526)	; Load board pointer
	ld	d,8+1
	ld	e,_HUMAN
	call	l0966		; Move down right
l0ae9:
	call	l06ca		; Load pointer and row/col
	ld	d,0		; set down
	ld	e,1
	call	l0749		; Move thru the board
	ld	a,d
	sub	0fh
	jp	nz,l0b03
	ld	hl,(l0526)	; Load board pointer
	ld	d,8
	ld	e,_HUMAN
	call	l0966		; Move down
l0b03:
	call	l06ca		; Load pointer and row/col
	ld	d,2		; set down left
	ld	e,1
	call	l0749		; Move thru the board
	ld	a,d
	sub	0fh
	jp	nz,l0b1d
	ld	hl,(l0526)	; Load board pointer
	ld	d,8-1
	ld	e,_HUMAN
	call	l0966		; Move down left
l0b1d:
	call	l06ca		; Load pointer and row/col
	ld	d,2		; Set left
	ld	e,0
	call	l0749		; Move thru the board
	ld	a,d
	sub	0fh
	jp	nz,l0b37
	ld	hl,(l0526)	; Load board pointer
	ld	d,2*8+1
	ld	e,_HUMAN
	call	l0966		; Move left
l0b37:
	call	l06ca		; Load pointer and row/col
	ld	d,2		; Set up left
	ld	e,2
	call	l0749		; Move thru the board
	ld	a,d
	sub	0fh
	jp	nz,l0b54
	ld	hl,(l0526)	; Load board pointer
	ld	d,2*8+9
	ld	e,_HUMAN
	call	l0966		; Move up left
	nop
	nop
	nop
l0b54:
	call	l09f9
	ld	b,1
	ld	c,1
	ld	d,_COMP
	ld	e,LOW l04a1 - 1
	call	l069c		; Test direction ok
	ld	a,d
	sub	0fh		; Test it
	jp	z,l0b9b		; Ok
	ld	b,1
	ld	c,8
	ld	d,_COMP
	ld	e,LOW l04a1 - 1
	call	l069c		; Test direction ok
	ld	a,d
	sub	0fh		; Test it
	jp	z,l0b9b		; Ok
	ld	b,8
	ld	c,1
	ld	d,_COMP
	ld	e,LOW l04a1 - 1
	call	l069c		; Test direction ok
	ld	a,d
	sub	0fh		; Test it
	jp	z,l0b9b		; Ok
	ld	b,8
	ld	c,8
	ld	d,_COMP
	ld	e,LOW l04a1 - 1
	call	l069c		; Test direction ok
	ld	a,d
	sub	0fh		; Test it
	jp	nz,l0bb6	; Not ok
l0b9b:
	ld	a,(l0504)
	sub	2
	jp	nz,l0a42
	ld	hl,(l0500)	; Get row and column
	ld	(l0506),hl
	ld	h,1
	ld	l,1
	ld	(l0504),hl
	jp	l0a42
;
	nop
	nop
	nop
l0bb6:
	ld	a,(l0504)
	sub	2
	jp	nz,l0bd3
l0bbe:
	ld	hl,(l0500)	; Get row and column
	ld	(l0506),hl
	ld	a,(l0508)
	ld	(l0505),a
	ld	a,(l0509)
	ld	(l0504),a
	jp	l0a42
l0bd3:
	ld	hl,l0504
	ld	a,(l0509)
	sub	1
	jp	z,l0bef
	ld	a,(hl)
	sub	1
	jp	z,l0bbe
	ld	(l0508),a
	inc	hl
	sub	(hl)
	jp	m,l0a42
	jp	l0bbe
l0bef:
	ld	a,(hl)
	sub	1
	jp	nz,l0a42
	ld	(l0508),a
	inc	hl
	sub	(hl)
	jp	m,l0bbe
	jp	l0a42
;
	nop
	nop
	nop
l0c03:
	ld	hl,(l0524)	; Get row and column
	jp	l0d1b
l0c09:
	ld	a,0
	ld	(l0504),a
l0c0e:
	ld	a,(l0504)
	sub	2
	jp	nz,l0c28
	jp	l0d25
l0c19:
	ld	bc,lcf80
	ld	de,l0408
	call	l0541		; Computer cannot move
	jp	l07d0
;
	nop
	nop
	nop
l0c28:
	ld	hl,(l0506)
	ld	b,h
	ld	c,l
	ld	d,_HUMAN
	ld	e,LOW l0461 - 1
	call	l069c		; Test direction ok
	ld	hl,(l0526)	; Load board pointer
	ld	(hl),_COMP	; Set computer
	call	l06ca		; Load pointer and row/col
	ld	d,0		; Set up
	ld	e,2
	call	l0749		; Move thru the board
	ld	a,d
	sub	0fh
	jp	nz,l0c53
	ld	hl,(l0526)	; Load board pointer
	ld	d,2*8+8
	ld	e,_HUMAN
	call	l0966		; Move up
l0c53:
	call	l06ca		; Load pointer and row/col
	ld	d,1		; Set up right
	ld	e,2
	call	l0749		; Move thru the board
	ld	a,d
	sub	0fh
	jp	nz,l0c6d
	ld	hl,(l0526)	; Load board pointer
	ld	d,2*8+7
	ld	e,_HUMAN
	call	l0966		; Move up right
l0c6d:
	call	l06ca		; Load pointer and row/col
	ld	d,1		; Set right
	ld	e,0
	call	l0749		; Move thru the board
	ld	a,d
	sub	0fh
	jp	nz,l0c87
	ld	hl,(l0526)	; Load board pointer
	ld	d,1
	ld	e,_HUMAN
	call	l0966		; Move right
l0c87:
	call	l06ca		; Load pointer and row/col
	ld	d,1		; Set down right
	ld	e,1
	call	l0749		; Move thru the board
	ld	a,d
	sub	0fh
	jp	nz,l0ca1
	ld	hl,(l0526)	; Load board pointer
	ld	d,8+1
	ld	e,_HUMAN
	call	l0966		; Move down right
l0ca1:
	nop
	call	l06ca		; Load pointer and row/col
	ld	d,0		; Set down
	ld	e,1
	call	l0749		; Move thru the board
	ld	a,d
	sub	0fh
	jp	nz,l0cbc
	ld	hl,(l0526)	; Load board pointer
	ld	d,8		; Move down
	ld	e,_HUMAN
	call	l0966
l0cbc:
	call	l06ca		; Load pointer and row/col
	ld	d,2		; Set down left
	ld	e,1
	call	l0749		; Move thru the board
	ld	a,d
	sub	0fh
	jp	nz,l0cd6
	ld	hl,(l0526)	; Load board pointer
	ld	d,8-1
	ld	e,_HUMAN
	call	l0966		; Move down left
l0cd6:
	call	l06ca		; Load pointer and row/col
	ld	d,2		; Set left
	ld	e,0
	call	l0749		; Move thru the board
	ld	a,d
	sub	0fh
	jp	nz,l0cf0
	ld	hl,(l0526)	; Load board pointer
	ld	d,2*8+1
	ld	e,_HUMAN
	call	l0966		; Move left
l0cf0:
	call	l06ca		; Load pointer and row/col
	ld	d,2		; Set up left
	ld	e,2
	call	l0749		; Move thru the board
	ld	a,d
	sub	0fh
	jp	nz,l0d0a
	ld	hl,(l0526)	; Load board pointer
	ld	d,2*8+9
	ld	e,_HUMAN
	call	l0966		; Move up left
l0d0a:
	jp	l07ca		; Human moves
;
	nop
	nop
	nop
;
; Part of computer move
;
; **** PATCH - MAY BE INSERTED INTO CODE ****
;
l0d10:
	ld	hl,(l0500)	; Get row and column
	ld	b,h
	ld	c,l
	jp	l0a35
;
	nop
	nop
	nop
;
; Moving test
;
; **** PATCH - MAY BE INSERTED INTO CODE ****
;
l0d1b:
	ld	a,h
	ld	h,l
	ld	l,a
	ld	(l0506),hl
	jp	l0c09
;
	nop
;
; Moving test
;
; **** PATCH - MAY BE INSERTED INTO CODE ****
;
l0d25:
	ld	a,1
	ld	(l0500),a	; Init column
	ld	(l0501),a	; And row
l0d2d:
	ld	a,(l0500)	; Get column
	ld	b,a
	ld	a,(l0501)	; Get row
	ld	c,a
	ld	d,_HUMAN
	ld	e,LOW l0461 - 1
	call	l069c		; Test direction ok
	ld	a,d
	sub	0fh		; Test it
	jp	z,l0c03		; Ok
	ld	hl,l0500	; Point to column
	inc	(hl)		; Advance it
	ld	a,(hl)
	sub	8+1		; Test all scanned
	jp	nz,l0d2d
	ld	(hl),a		; Reset column
	inc	(hl)
	inc	hl
	inc	(hl)		; Advance row
	ld	a,(hl)
	sub	8+1		; Test all scanned
	jp	nz,l0d2d	; Nope
	call	l0571		; Output the game board
	jp	l0c19
;
	nop
;
; Test piece from human player
;
; **** PATCH - MAY BE INSERTED INTO CODE ****
;
l0d5d:
	ld	a,(hl)		; Get piece
	sub	'.'		; Test human
	jp	z,l05e3		; Nope
	inc	b		; Advance human count
	jp	l05e3
;
; Test move possible
;
; **** PATCH - MAY BE INSERTED INTO CODE ****
;
l0d67:
	ld	a,1		; Init ..
	ld	(l04f0),a	; .. row 
	ld	(l04f1),a	; .. column
l0d6f:
	ld	e,LOW l0461 - 1
	ld	d,_HUMAN
	ld	hl,(l04f0)
	ld	b,h
	ld	c,l
	call	l069c		; Test direction ok
	ld	a,d
	sub	0fh		; Test it
	jp	nz,l0d87	; Not ok
	ld	bc,lcf00
	jp	l0801
l0d87:
	ld	hl,l04f0
	inc	(hl)		; Bump row
	ld	a,(hl)
	sub	8+1		; Test done
	jp	nz,l0d6f	; Nope
	ld	(hl),1		; Reset row
	inc	hl
	inc	(hl)		; Bump column
	ld	a,(hl)
	sub	8+1		; Test done
	jp	nz,l0d6f	; Nope
	call	l0571		; Output the game board
	ld	bc,lcec0
	ld	de,l031d
	call	l0541		; Tell score
	ld	bc,lcf00
	ld	de,l039a
	call	l0541		; Tell not able to move
	jp	l05d1
;
	nop
	nop
	nop
;
; Part of conversion
;
; **** PATCH - MAY BE INSERTED INTO CODE ****
;
l0db6:
	ld	(hl),'0'	; Init 2nd digit
	inc	hl
	ld	a,d		; Test zero count
	sub	0
	ret	z		; Yeap, that's it
	jp	l0646		; Else go convert
;
	nop
	nop
;
; Get data status or byte - Z flag set says no data
;
l0dc2:
	in	a,(0fah)	; Get data status
	cpl
	and	1
	ret	z
	in	a,(0fch)
	ret
;
	nop
	nop
	nop
	nop
	nop
	nop
	nop

	.dephase

	end

