; 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,$0001
	ld	(mise),hl
	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,0001h
	ld	(mise),hl
	
	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	clrgrbuf
	
	ld	a,(card1)
	ld	hl,$130D
	call	DrawCardInA

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


;	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:
	ld	a,0
	ld	(aceflag),a
	
	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,2
BlackJackLoop1:
;	push	bc
	call	DrawACard
;	pop	bc
	ld	(hl),a
	inc	hl
	djnz	BlackJackLoop1
	ld	b,2		
	
		push	bc
		push	hl
	ld	l,a
	ld	h,0
	ld	a,13
	bcall(_divhlbya)
	or	a		\	call	z,Setdealerace
		pop	hl
		pop	bc

BlackJackLoop12
;	push	bc
	call	DrawACard
;	pop	bc
	ld	(hl),a
	inc	hl
		push	bc
		push	hl
	ld	l,a
	ld	h,0
	ld	a,13
	bcall(_divhlbya)
	or	a		\	call	z,Setace
		pop	hl
		pop	bc
	djnz	BlackJackLoop12
;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+2)
	call	DrawCardinA

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

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

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

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

	call	fastcopy

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

	ld	hl,DrawStandMenuStr
	ld	de,0000h
	call	YesNoMenu	
	call	fastcopys	

	ld	a,(option)
	or	a		\	jr	z,BlackJackDealerLoop

	call	DrawACard
		push	af
	
	ld	l,a
	ld	h,0
	ld	a,13
	bcall(_divhlbya)
	or	a		\	call	z,Setace
		pop	af
		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	\ 	jp	nc,BlackJackYouWon
	cp	16		\	jr	nc,BlackJackCheck
	
	ld	b,30
	call	delayb
	call	DrawACard		
		push	af
	ld	l,a
	ld	h,0
	ld	a,13
	bcall(_divhlbya)
	or	a	\	call	Setdealerace
		pop	af
		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

BlackJackaceckeck:
	ld	a,(aceflag)
	bit	0,a
	jr	z,BlackJackYouLose
	ld	a,0
	ld	(aceflag),a
	ld	a,(PlayerSum)
	sub	10
	ld	(PlayerSum),a
	jp	BlackJackPlayerLoop

Setace:
	ld	a,255
	ld	(aceflag),a
	ret

Setdealerace:
	ld	a,255
	ld	(acedealerflag),a
	ret

BlackJackdealerace:
	ld	a,(acedealerflag)
	cp	255	\	jr	nz,BlackJackYouWon
	xor	a
	ld	(acedealerflag),a
	ld	a,(dealersum)
	sub	10
	ld	(dealersum),a
	jp	blackjackdealerinternal

;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		
	
;!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
;SLOT Machine game!
	
SlotMachine:			
	RTextInverse
	STextWrite
	call clrgrbuf
	call fastcopy

	ld	hl,slotmachinefacade
	ld	de,gbuf
	ld	bc,288
	ldir
	
	ld	hl,slotbetstr
	ld	de,3900h
	call	setvputs
	
	ld	hl,YourCashStr
	ld	de,$2b00
	call 	setvputs
	
	ld	e,$30
	ld	(pencol),de
	ld	hl,(cash)
	call	vputshl
	
	ld	hl,modequitstr
	ld	de,$3200
	call	setvputs

	RTextWrite
	call fastcopy
SlotDiwaitkeyloop:
	call	D_I_Waitkey
	cp	$BF
	jr	nz,SlotDiwaitkeyloop
	ld	a,c
	cp	$BF
	ret	z
	cp	$DF
	jr		z,slottestmise
slottestmise:
	ld	hl,(cash)
	ld	de,25
	or	a
	sbc	hl,de
	jp	m,slotmachine
;Interface de mise menkente!
	ld	hl,0
	ld	(slottemp),hl
	ld	b,5
	call	ionrandom
	ld	(card1),a
	ld	b,5
	call	ionrandom
	ld	(card2),a
	ld	b,5
	call	ionrandom
	ld	(card3),a
;calcule le counter pour la un	

	ld	a,(card1)
	ld	d,0
	ld	e,a
	ld	hl,$0000
	add	hl,de	;hl=de
	add	hl,hl	;*2
	add	hl,hl	;*04
	add	hl,hl	;*08
	ld	de,0028h	;un tour de plus
	add	hl,de
	
	ld	(SlotCounter1),hl
	ld	a,(card2)
	ld	d,0
	ld	e,a
	ld	hl,$0000
	add	hl,de	;hl=de
	add	hl,hl	;*2
	add	hl,hl	;*04
	add	hl,hl	;*08
	ld	de,0050h	;un tour de plus
	add	hl,de
	ld	(SlotCounter2),hl
	
	ld	a,(card3)
	ld	d,0
	ld	e,a
	ld	hl,$0000
	add	hl,de	;hl=de
	add	hl,hl	;*2
	add	hl,hl	;*04
	add	hl,hl	;*08
	ld	de,0079h	;un tour de plus
	add	hl,de
	ld	(SlotCounter3),hl
	ld	bc,1
	push	bc
SlotPrintLoop:
	call GrBufBackup
	pop	bc
	inc	bc
	push	bc

	ld	hl,(slotcounter1)
	call	cphlbc	\	call	z,setnope1
	ld	a,(slottemp)
	bit	1,a	\	call	nz,setendoff1
	call	slotcalcoff
	ld	ix,SlotSprite
	add	ix,bc
	ld	a,20
	ld	l,8
	ld	b,8
	call	ionputsprite

	pop	bc			; prevenir de decisse bc
	push	bc

	ld	hl,(slotcounter2)
	call	cphlbc	\	call	z,setnope2
	ld	a,(slottemp)
	bit	2,a	\	call	nz,setendoff2
	call	slotcalcoff
	ld	ix,SlotSprite
	add	ix,bc
	ld	a,44
	ld	l,8
	ld	b,8
	call	ionputsprite

	pop	bc			; prevenir de decisse bc
	push	bc

	ld	hl,(slotcounter3)
	call	cphlbc	\	call	z,setnope3
	ld	a,(slottemp)
	bit	0,a	\	call	nz,slotQuit
	call	slotcalcoff
	ld	ix,SlotSprite
	add	ix,bc
	ld	a,68
	ld	l,8
	ld	b,8

	call	ionputsprite

	call ionfastcopy	

	call	GrBufResBackup

	ld	b,3
	call	delayb
	jp	SlotPrintLoop
setnope1:
	ld	a,(slottemp)
	set	1,a
	ld	(slottemp),a
	ret
setnope2:
	ld	a,(slottemp)
	set	2,a
	ld	(slottemp),a
	ret
setnope3:
	ld	a,(slottemp)
	set	0,a
	ld	(slottemp),a
	ret
		
setendoff1:
	ld	bc,(slotcounter1)
	ret
setendoff2:
	ld	bc,(slotcounter2)
	ret


slotcalcoff:
	or	a
	ld	h,b
	ld	l,c
	ld	bc,0028h
		
	call	cphlbc	\	jr	nc,Sub28hfortheworld
	ld	b,h
	ld	c,l
	ret

Sub28hfortheworld:
	ld	de,0028h
	sbc	hl,de
	ld	b,h
	ld	c,l	
	jr	slotcalcoff

slotquit:
	pop	bc
	
	ld	a,(card2)			; offset de limage 2 est dans b
	ld	b,a
	ld	a,(card3)			; offset de limage 3 est dns c
	ld	c,a
	ld	a,(card1)		; offset de l'image 1 dns a
	cp	b				; comparer sil sont paril(1 et 2)
	jr	z,slot1et2sontpareil
	cp	c				; comparer si 1 et 3 son pareil
	jr	z,slot1et3sontpareil
	ld	d,a				;1 est rendu dns d
	ld	a,b			; a est rendu 2
	cp	c				; comparer si 2 et 3 son pariel
	jr	z,slot2et3sontpareil
slotyoulose:	
	ld	hl,(cash)		; perte si ya une pas de pareil
	ld	de,25
	or	a
	sbc	hl,de
	ld	(cash),hl
		
	STextInverse			; imprimente de la str you loose

	ld	hl,YouLost
	ld	de,$1010
	call	setvputs

	RTextInverse
	call	D_I_WaitKey
	jp 	SlotMachine
	
	
slot1et2sontpareil:
	cp	c			; comparer si 3 est le meme
	jr	z,SlotWinner
	ld	a,c
	cp	4			; comparrer si le 3 est bar car sa donne kelke piassen (25)
	jr	z,SlotbarWinner
	jr	slotyoulose
	
slot1et3sontpareil:
	ld	a,b		;coparer si le 2 est un bar?
	cp	4
	jr	nz,slotyoulose
SlotbarWinner:
	ld	de,50
	ld	hl,(cash)
	add	hl,de
	ld	(cash),hl

Slotyouwin:	
	STextInverse
	ld	hl,YouWin
	ld	de,$1010
	call	setvputs
	RTextInverse
	call	D_I_WaitKey
	jp	SlotMachine

slot2et3sontpareil:
	ld	a,d
	cp	4
	jr	z,SlotbarWinner
	jp	slotyoulose
SlotWinner:
	ld	de,500
	ld	hl,(cash)
	add	hl,de
	ld	(cash),hl
	
	jp	Slotyouwin
.end	
	
	