# blackjackPlay

minut += 1
gs 'stat'

if $ARGS[0] = 'new':
	killvar 'dealerHand'
	killvar 'playerHand'
	killvar 'playerBets'
	killvar 'playerPoints'
	dealerHand[] = 0
	dealerHand[] = 1
	playerHand[] = 2
	playerHand[] = 3
	cardsDealt = 4
	numHands = 1
	currentHand = 0
	handLosses = 0
	numAces = 0
	dealerPoints = 0
	insurance = 0
	betTotal = 0
	doubleBet = 0
	minBet = 10
	maxBet = 500
	*PL '<center><img src="images/locations/city/industrial/casino/stolbd.jpg"></center>'
	*PL 'You are sitting at the blackjack table.'
	act 'Bet' : gt 'blackjackPlay', 'bet'
	act 'Rules' : gt 'blackjackPlay', 'rules'
	act 'Leave' : gt 'cards'
end

if $ARGS[0] = 'rules':
	*PL '<center><img src="images/locations/city/industrial/casino/crupbd.jpg"></center>'
	*PL '<h2><u>Blackjack Rules:</u></h2>'
	*P '<ol><li>The player and dealer are each dealt 2 cards after initial bets (<<minBet>> <b>₽</b> - <<maxBet>> <b>₽</b>). The dealer will have 1 face-up card and 1 face-down card.</li>'
	*P '<li>Face cards are worth 10 points, aces can double as either 11 or 1, and all other cards are worth their numeric value.</li>'
	*P '<li>After the player finishes drawing, the dealer must continue to draw until he has 17 points or more.</li>'
	*P '<li>To win, the player must score more points than the dealer, have the dealer bust, or get a blackjack.</li>'
	*P '<li>Getting over 21 points is known as a bust, and an automatic loss.</li>'
	*P '<li>Getting an ace and a 10 point card in the first 2 cards is known as a blackjack, and is an automatic win which pays 3 : 2.</li>'
	*P '<li>If the dealer''s face-up card is an ace, the player can make a side-bet called "insurance" that pays 2 : 1 if the dealer has a blackjack.</li>'
	*P '<li>At any time, the player can choose to double his/her bet, which gives the player 1 more card but prevents further draws on that hand.</li>'
	*P '<li>If the player''s hand only consists of two cards of equal value, he/she can split them into 2 hands. Each hand must be covered by an amount equal to the original bet.</li>'
	*P '<li>The player cannot score a blackjack using a split hand.</li>'
	act 'Return' : gt 'blackjackPlay', 'new'
end

if $ARGS[0] = 'bet':
	*PL '<center><img src="images/locations/city/industrial/casino/stavkabd.jpg"></center>'
	playerBets[0] = INPUT('Place your bet.')
	if money < playerBets[0]:
		MSG 'You don''t have enough money for that bet.'
		gt 'blackjackPlay', 'new'
	elseif playerBets[0] < minBet:
		MSG 'There is a minimum bet of <<minBet>> <b>₽</b>.'
		gt 'blackjackPlay', 'new'
	elseif playerBets[0] > maxBet:
		MSG 'There is a maximum bet of <<maxBet>> <b>₽</b>.'
		gt 'blackjackPlay', 'new'
	end
	betTotal = playerBets[0]
	gs 'deckShuffle'
	if (deckFace[playerHand[0]] = 1 and deckFace[playerHand[1]] > 9) or (deckFace[playerHand[0]] > 9 and deckFace[playerHand[1]] = 1):
		gt 'blackjackPlay', 'blackjack'
	elseif deckFace[dealerHand[0]] > 9 and deckFace[dealerHand[1]] = 1:
		gt 'blackjackPlay', 'blackjack'
	elseif deckFace[dealerCards[0]] = 1:
		gt 'blackjackPlay', 'insurance1'
	else
		gt 'blackjackPlay', 'player'
	end
end

if $ARGS[0] = 'blackjack':
	gs 'blackjackView', 1
	if playerPoints[0] = 21 and dealerPoints = 21:
		*PL 'DRAW GAME'
		*PL 'You retrieve your bet.'
	elseif playerPoints[0] = 21:
		*PL 'PLAYER WINS'
		*PL 'You receive <<playerBets[0] * 3 / 2>> <b>₽</b> in winnings.'
		money += playerBets[0] * 3 / 2
	else
		*PL 'DEALER WINS'
		*PL 'The dealer takes your <<playerBets[0]>> <b>₽</b> bet.'
		money -= playerBets[0]
	end
	act 'Continue' : gt 'blackjackPlay', 'new'
end

if $ARGS[0] = 'insurance1':
	gs 'blackjackView'
	*PL 'The dealer has an ace showing.  Would you like to place a bet for insurance?'
	insurance = 0
	act 'Bet insurance' :
		insurance = INPUT('Place your bet.')
		if money < insurance:
			MSG 'You don''t have enough money for that bet.'
			gt 'blackjackPlay', 'insurance1'
		elseif insurance < 0:
			MSG 'You cannot bet a negative amount of money.'
			gt 'blackjackPlay', 'insurance1'
		elseif insurance > playerBets[0] / 2:
			MSG 'Insurance cannot exceed half the original bet.'
			gt 'blackjackPlay', 'insurance1'
		else
			gt 'blackjackPlay', 'insurance2'
		end
	end
	act 'No insurance' : gt 'blackjackPlay', 'insurance2'
end

if $ARGS[0] = 'insurance2':
	if deckFace[dealerCards[1]] > 9: gs 'blackjackView', 1
	if deckFace[dealerCards[1]] < 10: gs 'blackjackView'
	if dealerPoints = 21:
		*PL 'DEALER WINS'
		*P 'The dealer takes your <<playerBets[0]>> <b>₽</b> bet'
		if insurance > 0: *P ' but also gives you <<insurance>> <b>₽</b> for your side bet'
		*P '.'
		money -= playerBets[0]
		money += insurance
		act 'Continue' : gt 'blackjackPlay', 'new'
	else
		*PL 'The dealer takes a peek at his hole card; no blackjack.'
		if insurance > 0: *P 'He relieves you of <<insurance>> <b>₽</b> for your side bet.'
		money -= insurance
		act 'Continue' :
			CLA
			wait 500
			gt 'blackjackPlay', 'player'
		end
	end
end

if $ARGS[0] = 'player':
	gs 'blackjackView'
	if playerPoints[currentHand] > 21: currentHand += 1 & handLosses += 1 & doubleBet = 0
	if playerPoints[currentHand] = 21 or doubleBet = 1: currentHand += 1 & doubleBet = 0
	if handLosses = numHands:
		*PL 'DEALER WINS'
		*P 'The dealer relieves you of <<betTotal>> <b>₽</b>.'
		money -= betTotal
		act 'Continue' : gt 'blackjackPlay', 'new'
	elseif currentHand >= numHands:
		*PL 'You can take no further actions.'
		act 'Continue' :
			CLA
			wait 500
			gt 'blackjackPlay', 'dealer'
		end
	else
		if numHands = 1:
			*PL 'You are considering actions for your hand.'
			$text = ''
		else
			*PL 'You are considering your actions for <b>hand #<<currentHand + 1>></b>.'
			$text = 'Hand #<<currentHand + 1>>: '
		end
		act '<<$text>>Hit' :
			i = currentHand * 16 + 2
			:loop1
			if playerHand[i] ! 0: i += 1 & jump 'loop1'
			playerHand[i] = cardsDealt
			cardsDealt += 1
			*CLR & CLA
			*PL '<center><img src="images/locations/city/industrial/casino/cartbde.jpg"></center>'
			wait 750
			gt 'blackjackPlay', 'player'
		end
		if money >= betTotal + playerBets[currentHand]:
			act '<<$text>>Double' :
				doubleBet = 1
				betTotal += playerBets[currentHand]
				playerBets[currentHand] *= 2
				i = currentHand * 16 + 2
				:loop2
				if playerHand[i] ! 0: i += 1 & jump 'loop2'
				playerHand[i] = cardsDealt
				cardsDealt += 1
				*CLR & CLA
				*PL '<center><img src="images/locations/city/industrial/casino/cartbde.jpg"></center>'
				wait 750
				gt 'blackjackPlay', 'player'
			end
			if playerHand[currentHand * 16 + 2] = 0 and ((deckFace[playerHand[currentHand * 16]] > 9 and deckFace[playerHand[currentHand * 16 + 1]] > 9) or deckFace[playerHand[currentHand * 16]] = deckFace[playerHand[currentHand * 16 + 1]]):
				act '<<$text>>Split' :
					betTotal += playerBets[currentHand]
					playerBets[numHands] = playerBets[currentHand]
					numHands += 1
					i = (currentHand + 1) * 16
					:loop3
					if playerHand[i] ! 0: i += 16 & jump 'loop3'
					playerHand[i] = playerHand[currentHand * 16 + 1]
					playerHand[currentHand * 16 + 1] = cardsDealt
					cardsDealt += 1
					playerHand[i + 1] = cardsDealt
					cardsDealt += 1
					*CLR & CLA
					*PL '<center><img src="images/locations/city/industrial/casino/cartbde.jpg"></center>'
					wait 750
					gt 'blackjackPlay', 'player'
				end
			end
		end
		act '<<$text>>Stay' :
			currentHand += 1
			CLA
			wait 500
			if currentHand < numHands: gt 'blackjackPlay', 'player'
			if currentHand >= numHands: gt 'blackjackPlay', 'dealer'
		end
	end
end

if $ARGS[0] = 'dealer':
	gs 'blackjackView', 1
	if dealerPoints < 17:
		act 'Continue' :
			i = 2
			:loop4
			if dealerHand[i] ! 0: i += 1 & jump 'loop4'
			dealerHand[i] = cardsDealt
			cardsDealt += 1
			*CLR & CLA
			*PL '<center><img src="images/locations/city/industrial/casino/cartbde.jpg"></center>'
			wait 750
			gt 'blackjackPlay', 'dealer'
		end
	elseif dealerPoints > 21:
		betTotal = 0
		i = 0
		:loop5
		if playerPoints[i] <= 21: betTotal += playerBets[i]
		i += 1
		if i < numHands: jump 'loop5'
		*PL 'PLAYER WINS'
		*P 'You receive <<betTotal>> <b>₽</b> in winnings.'
		money += betTotal
		act 'Continue' : gt 'blackjackPlay', 'new'
	else
		betTotal = 0
		i = 0
		:loop6
		if playerPoints[i] > 21:
			betTotal -= playerBets[i]
		elseif playerPoints[i] < dealerPoints:
			betTotal -= playerBets[i]
		elseif playerpoints[i] > dealerPoints:
			betTotal += playerBets[i]
		end
		i += 1
		if i < numHands: jump 'loop6'
		if betTotal = 0:
			*PL 'DRAW GAME'
			if numHands = 1: *P 'You retrieve your bet.'
			if numHands > 1: *P 'You retrieve your bets.'
		elseif betTotal < 0:
			*PL 'DEALER WINS'
			if numHands > 1: *P 'After taking into account all the hands, you have lost. '
			*P 'The dealer relieves you of <<0 - betTotal>> <b>₽</b>.'
		else
			*PL 'PLAYER WINS'
			if numHands > 1: *P 'After taking into account all the hands, you have won. '
			*P 'You receive <<betTotal>> <b>₽</b> in winnings.'
		end
		money += betTotal
		act 'Continue' : gt 'blackjackPlay', 'new'
	end
end

--- blackjackPlay ---------------------------------

