; Game for Zcasino

; Battle a two dice
Batt2DeGame:
	ld	hl,0001h
	ld	(mise),hl
	set	3,(iy+$05)
	call	DiceInterface		; Draw the general interface
	res	3,(iy+$05)
	
	ld	hl,YourDice			; Finish the interface
	ld	de,$0101
	call	setvputs
	
	ld	hl,DiceOfDealer
	ld	de,$0131
	call	setvputs
	
	
	res	7,(iy+$14)	; Res TextWrite
Batt2DeMiseLoop:
	call	fastcopy				; Reset the screen
	call	PutMiseCashonScreen	; Put Mise and cash on the screen
	call	inputMise				; input mise
	or	a	\	jr	z,Batt2DeMiseLoop 	; only mise have changed
Batt2DeRollDe:
;------------Generate Random Number
	ld	hl,Dee
	ld	b,$04
Batt2Derandomlp:
	ld	d,b
	ld	b,6 
	call	irandom
;	inc	a
	ld	(hl),a
	inc	hl
	ld	b,d
	djnz	Batt2Derandomlp
	ld	hl,Dee
	
	ld	a,(hl)
	ld	de,$0209
	call	drawdice
	inc	hl
	
	ld	a,(hl)
	ld	de,$180D
	call	drawdice
	inc	hl
	
	ld	a,(hl)
	ld	de,$320D
	call	drawdice
	inc	hl
	
	ld	a,(hl)
	ld	de,$4A09
	call	drawdice
	
	set	7,(iy+$14)	; Set textwrite
	call	PutMiseCashonScreen
	res	7,(iy+$14)	; Set textwrite
	call	fastcopy
	call	D_I_Waitkey
	ld	hl,Dee
	
	ld	a,(hl)
	inc	hl
	add	a,(hl)
	ld	d,a			; d=player
	inc	hl
	ld	a,(hl)
	inc	hl
	add	a,(hl)
	
	sub	d
	jr	c,WinGame
	jr	z,NullGame

	ld	hl,(cash)
	ld	de,(mise)
	or	a
	sbc	hl,de
	ld	(cash),hl

	ld	hl,YouLost
	ld	a,30h
End2DiceBattleGame:
	set	3,(iy+05h)	
	call	centertext
	res	3,(iy+05h)
	call	D_I_Waitkey
	jp	Batt2DeGame
	
WinGame:

	ld	hl,(cash)
	ld	de,(mise)
	add	hl,de
	ld	(cash),hl

	ld	hl,YouWin
	ld	a,30h
	jr	End2DiceBattleGame
				
NullGame:
	ld	hl,YouAnule
	ld	a,30h
	jr	End2DiceBattleGame
	

;##########################################################################################################
;CHANCE DICE PROG
ChanceDiceGame:
	ld	hl,mise
	ld	(hl),1
	set	3,(iy+$05)
	call	DiceInterface		; Draw the general interface(in black)
	res	3,(iy+$05)

	ld	hl,YourDice			; Finish the interface
	ld	de,$0101
	call	setvputs
	
	ld	hl,YouChoiceStr
	ld	de,$0131
	call	setvputs

	res	7,(iy+$14)	; Res TextWrite
	
ChanceDiceMiselp:
	call	fastcopy				; Reset the screen
	call	PutMiseCashonScreen	; Put Mise and cash on the screen
	call	inputMise				; input mise
	or	a	\	jr	z,ChanceDiceMiselp 	; only mise have changed
	
	call	DiceInterface		; Draw the general interface(in black)

	ld	hl,YourDice			; Finish the interface
	ld	de,$0101
	call	setvputs

	set	3,(iy+$05)
	ld	hl,YouChoiceStr
	ld	de,$0131
	call	setvputs
	res	3,(iy+$05)

	call	PutMiseCashonScreen	

	res	7,(iy+$14)	; Res TextWrite

ChanceDiceChoicelp:
	call	fastcopy
	call	inputdice
;	or	a	\	jr z,ChanceDiceChoicelp
ChanceRandom:
	ld	b,6
	call	irandom
	push	af
	ld	de,$0209
	call	drawdice
	call	fastcopy
	pop	af
	ld	hl,dee
	cp	(hl)
	jr	z,ChanceDiceYouWon
	ld	hl,(cash)
	ld	de,(mise)
	or	a
	sbc	hl,de
	ld	(cash),hl
	ld	hl,YouLost
	ld	a,30h
ChanceDiceEndGame:
	set	3,(iy+05h)	
	call	centertext
	res	3,(iy+05h)
	call	D_I_Waitkey
	jp	ChanceDiceGame
	
ChanceDiceYouWon:
	ld	a,(mise)
	ld	e,a
	ld	d,0
	ld	hl,$0000
	add	hl,de
	add	hl,hl
	add	hl,hl
	add	hl,hl		
	ld	de,(cash)
	add	hl,de
	ld	(cash),hl
	ld	hl,ChanceDiceYouWonStr
	ld	a,30h
	jr	ChanceDiceEndGame
	ret
	
;######################################################################
;######
; |---/
;/ffffffffffffffff
; Bettween Game

BetweenGame:
	ld	hl,mise
	ld	(hl),1
	
	call	clrgrbuf

	call	fastcopy
	

	call	ShuffleDeck		; Reset the deck

	call	DrawACard			; get the first card
	ld	(Card1),a
	ld	hl,$130D
	call	DrawCardInA

	call	DrawACard
	ld	(card2),a
	ld	hl,$300D
	call	DrawCardInA

	
	call	DrawAcard
	ld	(Card3),a

	call GrBufBac2
	
	set	7,(iy+$14)	; Res TextWrite
	ld	hl,Doyouwantbetstr
	ld	de,$2300
	call setvputs
	res	7,(iy+$14)	; Res TextWrite
	
	ld	hl,plotsscreen+516
	ld	(hl),$FF
	ld	de,plotsscreen+517
	ld	bc,000Bh
	ldir

	ld	hl,plotsscreen+600
	ld	(hl),$FF
	ld	de,plotsscreen+601
	ld	bc,000Bh
	ldir

	call	fastcopy

	ld	de,$002C
	ld	hl,yesnomenustr
	call	yesnomenu
	or	a		\	jr	z,BetweenGame

	call	grres2
    
	set	7,(iy+$14)	; Set textwrite
	
		
	ld	de,$2101
	ld	hl,YourCashStr
	call	setvputs
	
	ld	de,$2A01
	ld	hl,YourBetStr
	call	setvputs

	res	7,(iy+$14)	; Set textwrite
	call	fastcopy

BeetweenMiseLoop
	call	fastcopy				; Reset the screen
	call	PutMiseCashonScreen	; Put Mise and cash on the screen
	call	inputMise				; input mise
	or	a	\	jr	z,BeetweenMiseLoop 	; only mise have changed

	ld	a,(card3)
	ld	hl,$200D
	call	DrawCardinA
	call	fastcopy

;NOW THE REAL PROGRAMMING!!!!!!!!!!!!!!!
	ld	a,(card1)
	ld	l,a
	ld	h,$00
	ld	a,13
	bcall(_divhlbya)		; divise
	ld	(card1),a

	ld	a,(card2)
	ld	l,a
	ld	h,$00
	ld	a,13
	bcall(_divhlbya)		; divise
	ld	(card2),a

	ld	a,(card3)
	ld	l,a
	ld	h,$00
	ld	a,13
	bcall(_divhlbya)		; divise
	ld	(card3),a
	
	xor	a
	ld	a,(card2)
	ld	b,a
	ld	a,(card1)
	cp	b	\	jr	z,BeetweenGameLost	\	jr	c,Card2isGretter
	jr	BettwenCardCheck
Card2isGretter:
	ld	(card2),a
	ld	a,b
	ld	(card1),a

BettwenCardCheck:
	xor	a
	ld	a,(card3)
	ld	b,a
	ld	a,(card1)
	cp	b	\	jr	z,BeetweenGameLost	\ 	jr	c,BeetweenGameLost

	xor	a
	ld	a,(card2)
	cp	b	\	jr	z,BeetweenGameLost	\	jr	nc,BeetweenGameLost
	ld	hl,(cash)
	ld	de,(mise)
	add	hl,de
	ld	(cash),hl
	ld	hl,YouWin
	jr	BetweenGameEnd
BeetweenGameLost:
	or	a
	ld	hl,(cash)
	ld	de,(mise)
	sbc	hl,de
	ld	(cash),hl
	ld	hl,YouLost
BetweenGameEnd:		
	ld	a,30
	set	7,(iy+14h)
	set	3,(iy+05h)		; Text Inverse
	call	centertext
	ld	hl,Continuestr
	ld	de,0000h
	call	setvputs
	res	7,(iy+14h)
	res	3,(iy+05h)		; Text Inverse
	ld	de,0007h
	ld	hl,YesNoMenuStr
	call	yesnomenu
	or	a	\	ret z
	jp	BetweenGame


;_______________________________
;Black Jack
BlackJackGame:
	call	ShuffleDeck
BJal:
	call	clrgrbuf

	ld	hl,0001h
	ld	(mise),hl
	STextWrite
	
	ld	de,$2101
	ld	hl,YourCashStr
	call	setvputs
	
	ld	de,$2A01
	ld	hl,YourBetStr
	call	setvputs
	
	RTextWrite
	
BlackMiseLoop:
	call	fastcopy				; Reset the screen
	call	PutMiseCashonScreen	; Put Mise and cash on the screen
	call	inputMise				; input mise
	or	a	\	jr	z,BlackMiseLoop 	; only mise have changed
	
	call	clrgrbuf
	
	STextWrite
	
	ld	hl,TheDealerCard
	ld	de,0000h
	call	setvputs
	
	ld	hl,YourCardStr
	ld	de,$1C00
	call	setvputs
	
	RTextWrite
	
	
	ld	hl,BlackJackCard
	ld	b,5
BlackJackLoop1:
;	push	bc
	call	DrawACard
;	pop	bc
	ld	(hl),a
	inc	hl
	djnz	BlackJackLoop1
;Draw First Card	
	ld	l,08h
	xor	a
	ld	bc,$1302
	ld	ix,BlankcardSpr
	call	ilsprite


	;ld	a,(BlackJackCard)
	;call	DrawCardinA
	
	ld	hl,1008h
	ld	a,(BlackJackCard+1)
	call	DrawCardinA

	ld	hl,0023h
	ld	a,(BlackJackCard+3)
	call	DrawCardinA

	ld	hl,1023h
	ld	(lastcardposition),hl
	ld	a,(BlackJackCard+4)
	call	DrawCardinA
	
;Card Draw
	ld	a,(BlackJackCard)
	ld	hl,0000h
	call	addCardtoSum

	ld	a,(BlackJackCard+1)
	call	addcardtosum
	
	ld	(DealerSum),hl	

	ld	a,(BlackJackCard+3)
	ld	hl,0000h
	call	addCardtoSum

	ld	a,(BlackJackCard+4)
	call	addcardtosum
	
	ld	(PlayerSum),hl	

	call	fastcopy

	
BlackJackPlayerLoop:
	ld	a,(PlayerSum)
	cp	21		\	jp	z,BlackJackDealerLoop	\	jp	nc,BlackJackYouLose
	call	GrBufBac2

	ld	hl,DrawStandMenuStr
	ld	de,0000h
	call	YesNoMenu	

	call	grres2

	call	fastcopys	

	or	a		\	jr	z,BlackJackDealerLoop

	call	DrawACard
		push	af
	ld	hl,(playerSum)
	call	addCardtoSum
	ld	(playerSum),hl
	
	ld	hl,(lastcardposition)
	ld	a,h
	add	a,16
	ld	h,a
	ld	(lastcardposition),hl
		pop	af
	call	drawCardinA
	call	fastcopy
	jr	BlackJackPlayerLoop
BlackJackDealerLoop:	
	ld	hl,$1008
	ld	(lastcardposition),hl
	ld	hl,0008h
	ld	a,(BlackJackCard)
	call	DrawCardinA
	ld	l,08h
	xor	a
	ld	bc,$1302
	ld	ix,BlankcardSpr
	call	ilsprite
	call	fastcopy
blackjackdealerinternal:	
	ld	a,(dealersum)
	cp	21		\	jr	z,BlackJackCheck	\ 	jr	nc,BlackJackYouWon
	cp	16		\	jr	nc,BlackJackCheck
	
	ld	b,30
	call	delayb
	call	DrawACard		
		push	af
	ld	hl,(dealerSum)
	call	addCardtoSum
	ld	(dealerSum),hl
	ld	hl,(lastcardposition)
	ld	a,h
	add	a,16
	ld	h,a
	ld	(lastcardposition),hl
		pop	af
	call	drawcardina
	
	call	fastcopy
	
	jr	blackjackdealerinternal	
BlackJackCheck:				; b =dealer
	ld	hl,(playersum)	
	cp	l		\	jr	z,BlackJackYouLose	\	jr	c,BlackJackYouWon	
BlackJackYouLose:
	ld	hl,(cash)
	ld	de,(mise)
	or	a
	sbc	hl,de
	ld	(cash),hl
	ld	hl,YouLost
	jr	blackJackEnd
BlackJackYouWon:	
	ld	hl,(cash)
	ld	de,(mise)
	add	hl,de
	ld	(cash),hl
	ld	hl,YouWin
blackJackEnd:
	ld	a,30
	
	STextInverse
	STextWrite

	call	centertext
	call	fastcopy
	call	D_I_Waitkey			;'''''''''''''''''''
	ld	hl,Continuestr
	ld	de,0000h
	call	setvputs

	RTextWrite
	RTextInverse
	ld	de,0007h
	ld	hl,YesNoMenuStr
	call	yesnomenu
	or	a	\	ret z
	jp	BJal

	
	ret















;Second routine for BlackJack	
addCardtoSum:
	ex	de,hl
	ld	h,0
	ld	l,a
	ld	a,13
	bcall(_divhlbya)
	inc	a
	cp	1	\	jr	z,add11tothesum
	cp	10	\	jr	nc,Add10ToTheSum
	jr	AddEnd
add11tothesum:
	ld	a,11
	jr	AddEnd
Add10ToTheSum:
	ld	a,10
AddEnd:
	ld	h,0
	ld	l,a
	add	hl,de
	ret				
	
	
	