# 2009-12-25 j.m.reneau
# create a deck of cards, shuffle them, and display
# 5 of them graphically on the screen
# requires 0.9.4g
#
# shapes of the card suits
dim diamond(8)
diamond = {0, 2.5, 2, 0, 4, 2.5, 2, 5}
dim heart(12)
heart = {0, 1, 1, 0, 2, 1, 3, 0, 4, 1, 2, 5}
dim spade(18)
spade = {0, 3, 2, 0, 4, 3, 4, 4, 2, 3, 3, 5, 1, 5, 2, 3, 0, 4}
dim club(30)
club = {0, 1, 1, 0, 2, 1, 3, 0, 4, 1, 3, 2, 4, 3, 3, 4, 2, 3, 3, 5, 1, 5, 2, 3, 1, 4, 0, 3, 1, 2}
#
# suits and faces
dim suits$(4)
suits$ = {"D", "H", "S", "C"}
dim cards$(13)
cards$ = {"A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J" ,"Q", "K"}
#
# array to hold the shuffled deck
dim deck$(52)

gosub makedeck
gosub shuffledeck

clg
# draw poker table
color rgb(139,69,19)
rect 0,0,300,300
color darkgreen
rect 10,10,280,280

print "click on cards to turn over"

# show 5 cards face down
for t = 1 to 5
  x = 25 + t*40
  y = 100
  gosub drawcardback
next t

clickloop:
  pause .1
  # if we have not clicked - go back and wait
  if clickb = -1 then goto clickloop
  card1 = -1
  # see if the click was on a card
  for t = 1 to 5
    if clickx >= 25 + t*40 and clickx <= 55 + t*40 and clicky >= 100 and clicky <= 150 then card1 = t
  next t
  # clear the click
  clickclear
  if card1 = -1 then goto clickloop
  # we clicked on a card - show it
  card$ = deck$[card1]
  x = 25 + card1*40
  y = 100
  gosub drawcard
  goto clickloop

end

makedeck: #
# makedeck - loop through the suits$ and cards$ arrays
# to build 52 card deck$
for suit = 0 to 3
  for card = 0 to 12
    deck$[suit*13+card] = suits$[suit] + cards$[card]
  next card
next suit
return

shuffledeck: #
# shuffledeck - for many times pick two random
# cards from the deck and switch them
for t = 1 to 200
  card1 = int(rand*52)
  card2 = int(rand*52)
  card$ = deck$[card2]
  deck$[card2] = deck$[card1]
  deck$[card1] = card$
next t
return

drawcardback: #
# draw a moire pattern for the back of a card
gosub drawcardborder
color blue
for card1 = 3 to 30 step 4
   line x+2,y+2,x+card1,y+48
next card1
for card1 = 3 to 50 step 4
   line x+2,y+2,x+28,y+card1
next card1
return

drawcardborder: #
# draw blank card with border
color blue
rect x,y,30,50
color white
rect x+2,y+2,26,46
return

drawcard: #
# drawcard - draw a 30x50 card at the location x,y
# on the screen.  also set card$ to be the card
# you want to be displayed
#
# set card color
gosub drawcardborder
color black
if mid(card$,1,1) = "H" or mid(card$,1,1) = "D" then color red
# stamp suit on card
if mid(card$,1,1) = "H" then stamp x+7, y+26, 4, heart[]
if mid(card$,1,1) = "D" then stamp x+7, y+26, 4, diamond[]
if mid(card$,1,1) = "S" then stamp x+7, y+26, 4, spade[]
if mid(card$,1,1) = "C" then stamp x+7, y+26, 4, club[]
# now show card value
font "tahoma",13,100
text x + 4, y + 1, mid(card$,2,2)
return
