remstart
BLACK JACK 1.0
Text Based. Very simplified
07/07/2004
Remend
`Card data(number, suit, value)```````````````````````````````````````````````````````````
data "a","s",11,"a","c",11,"a","h",11,"a","d",11,"2","s",2,"2","c",2,"2","h",2,"2","d",2
data "3","s",3,"3","c",3,"3","h",3,"3","d",3,"4","s",4,"4","c",4,"4","h",4,"4","d",4
data "5","s",5,"5","c",5,"5","h",5,"5","d",5,"6","s",6,"6","c",6,"6","h",6,"6","d",6
data "7","s",7,"7","c",7,"7","h",7,"7","d",7,"8","s",8,"8","c",8,"8","h",8,"8","d",8
data "9","s",9,"9","c",9,"9","h",9,"9","d",9,"10","s",10,"10","c",10,"10","h",10,"10","d",10
data "j","s",10,"j","c",10,"j","h",10,"j","d",10,"q","s",10,"q","c",10,"q","h",10,"q","d",10
data "k","s",10,"k","c",10,"k","h",10,"k","d",10
`Create a card type``````````````````````````````````````````````````````````````````````````
Type Card
suit as string
number as string
value as integer
endtype
`Global Variables```````````````````````````````````````````````````````````````````````````
``````````The Deck``````````````````````````````````
Dim Deck(52) as card
` Pointer to the current card in the deck
global index as integer = 52
`````````The Player's Hand```````````````````````````
dim phand(8) as card
` Pointer to most recent card dealt to player
global pindex as integer = 1
` Number of aces not yet valued at 1 in player's hand
global paces as integer = 0
` The player's current total
global ptotal as integer = 0
` the player's current bet
bet as integer = 0
` the player's money
money as integer = 500
`Flags wether stay has been clicked 1 = yes, 0 = no
stayflag as integer = 0
`Bust flag 1 = busted 0 = good
pbust as integer = 0
`````````the dealer's hand``````````````````````````
dim dhand(8) as card
` pointer to most current card in dealer's hand
global dindex as integer = 1
` number of aces not yet valued at 1 in dealer's hand
global daces as integer = 0
` the dealer's current total
global dtotal as integer = 0
`bust flag bust = 1 good = 0
dbust as integer = 0
`Mouse Variables```````````````````````````````````````````````````````````````````````
mx as integer = 0
my as integer = 0
````````````````````````THE GAME~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
`Do the pregame housekeeping
gosub _init_game
`get player's bet
bet = Get_Bet(money)
`Tag for starting a new hand
_Start:
`Check to make sure there are enough cards to deal each player 2
if index < 4
`if not then reshuffle
restore
create_deck()
shuffle_cards()
index = 52
endif
`deal 4 cards
Deal_Card("p")
Check_Ace("p")
inc pindex
Deal_Card("d")
Check_Ace("d")
inc dindex
Deal_Card("p")
Check_Ace("p")
inc pindex
Deal_Card("d")
Check_Ace("d")
inc dindex
`check to see if anyone got two aces and lower the value if they did.
if dtotal = 22
dtotal = 12
dec daces
endif
if ptotal = 22
ptotal = 12
dec paces
endif
`start the main loop
do
CLS
Display_Screen(stayflag, money, bet, ptotal)
`Wait for the left button to be pressed
while mouseclick() <> 1
endwhile
`and released
while mouseclick() <> 0
`get the position
mx = mousex()
my = mousey()
endwhile
`check to see if hit was clicked
if mx >= 400 and mx <= (400 + text width("hit")) and my >= 390 and my<= (390 + text height("hit"))
`if so deal a card
Deal_Card("p")
`See if it's an ace
Check_Ace("p")
`advance the pointer
inc pindex
`if your hand is over 21
if ptotal > 21
`and you have an ace
if paces > 0
`lower the aces value to 1
ptotal = ptotal - 10
`lower the number of aces
dec paces
else
`otherwise you bust
pbust = 1
endif
endif
endif
`if your busted
if pbust = 1
CLS
`make sure the last card dealt shows
Display_Screen(stayflag, money, bet, ptotal)
CLS
set text size 52
`tell them they lost
text 10,300,"You Busted"
wait 1500
`apply the bet
money = money - bet
set text size 12
`reset variables except deck
gosub _Reset_Game
`Get the new bet
bet = get_bet(money)
goto _start
endif
`See if they clicked stay
if mx >= 500 and mx <= (500 + text width("stay")) and my >= 390 and my <= (390 + text height("stay"))
stayflag = 1
Display_Screen(stayflag, money, bet, ptotal)
`dealer's turn... dealer holds at 17
while dtotal < 17
deal_Card("d")
Check_Ace("d")
inc dindex
if dtotal > 21
if daces > 0
dtotal = dtotal - 10
dec daces
else
dbust = 1
endif
endif
Display_Screen(stayflag, money, bet, ptotal)
endwhile
if dbust = 1
CLS
Display_Screen(stayflag, money, bet, ptotal)
CLS
set text size 52
text 10,300,"Dealer Bust!"
wait 1500
money = money + bet
set text size 12
gosub _Reset_Game
bet = get_bet(money)
goto _start
else
`if no one busted
CLS
`make sure the last card dealt is shown
Display_Screen(stayflag, money, bet, ptotal)
wait 1000
CLS
set text size 52
if ptotal > dtotal
text 10,300,"You Win!"
wait 1500
money = money + bet
else
text 10,300,"You Lose!"
wait 1500
money = money - bet
endif
set text size 12
gosub _reset_game
endif
bet = get_bet( money )
goto _start
endif
`end the loop
loop
`````````````````````Initialization`````````````````````````````````````````````````````
_init_game:
randomize timer()
Create_Deck()
Shuffle_Cards()
Return
`````````````````````Support Functions````````````````````````````````````````````````````
function Create_Deck
`read data into the array
for x = 1 to 52
read deck(x).number
read deck(x).suit
read deck(x).value
next x
endfunction
function Shuffle_Cards
temp as card
for x = 1 to 200
y = rnd(51) + 1
z = rnd(51) + 1
temp.suit = deck(y).suit
deck(y).suit = deck(z).suit
deck(z).suit = temp.suit
temp.number = deck(y).number
deck(y).number = deck(z).number
deck(z).number = temp.number
temp.value = deck(y).value
deck(y).value = deck(z).value
deck(z).value = temp.value
next x
endfunction
Function Get_Bet( money )
`if they're out of money...end the game
if money = 0
CLS
set text size 52
text 10,300,"Game Over!"
wait 3000
end
endif
_bet:
CLS
pmoney = money
print "Current Money: ";
print pmoney
Input "Enter your bet: ";pbet
If pbet <5 or pbet > pmoney
print "Invalid Bet! Try Again."
wait 3000
goto _bet
endif
endfunction pbet
Function Display_Screen(stayflag, money, bet, ptotal)
CLS
Print " "
Print " Dealer's Hand: "
temp = 1
`if they've clicked stay..then show the dealer's cards
if stayflag = 1
print " ";
while temp <= dindex
print dhand(temp).number;dhand(temp).suit;
print " ";
inc temp
endwhile
print " "
print "Dealer's Total: " ; dtotal
else
`otherwise hide his second card
print " " ; dhand(1).number;dhand(1).suit; " " ; "??"
endif
print " "
print " "
print " "
print " "
print " "
print " "
print " "
print " "
print " Player's Hand: "
temp = 0
print " ";
while temp <= pindex
print " " ; phand(temp).number;phand(temp).suit;
inc temp
endwhile
print " "
print " "
print " "
print "Player's Hand Total: "; ptotal
print "Bet: " ; bet
print "Money: " ; money
Text 400,390,"hit"
Text 500,390,"stay"
wait 900
endfunction
function Deal_Card( char as string )
`if dealing to player
if char = "p"
phand(pindex).suit = deck(index).suit
phand(pindex).value = deck(index).value
phand(pindex).number = deck(index).number
ptotal = ptotal + deck(index).value
dec index
endif
`if dealing to dealer
if char = "d"
dhand(dindex).suit = deck(index).suit
dhand(dindex).value = deck(index).value
dhand(dindex).number = deck(index).number
dtotal = dtotal + deck(index).value
dec index
endif
`if the deck is empty
if index = 0
`reshuffle
restore
create_deck()
shuffle_cards()
index = 52
endif
endfunction
function Check_Ace( char as string )
`keep track of how many aces in each hand
if char = "p"
if phand(pindex).value = 11
inc paces
endif
endif
if char = "d"
if dhand(dindex).value = 11
inc daces
endif
endif
endfunction
_Reset_Game:
`reset everything for a new game except the deck and money
undim phand(8)
undim dhand(8)
dim phand(8) as card
dim dhand(8) as card
pindex = 1
dindex = 1
ptotal = 0
dtotal = 0
pbust = 0
dbust = 0
stayflag = 0
paces = 0
daces = 0
return