Author Topic: Blackjack  (Read 5496 times)

This topic contains a post which is marked as Best Answer. Press here if you would like to see it.

Online bplus

  • Forum Resident
  • Posts: 4616
  • B+ nots
Blackjack
« on: June 07, 2019, 02:54:46 PM »
Simple game without card images and doesn't bother with suits. X is Roman for 10 when looking at the hands. Dealer wins all ties, except Blackjack for Player immediately pays double the bet.
Code: QB64: [Select]
  1. OPTION _EXPLICIT ' No suits shown for cards A is Ace, J, Q, K are Jack, Queen, King, X is for 10
  2. DEFINT A-Z '       Player's Blackjack pays double the bet set at the beginning of the game
  3.  
  4. _TITLE "Blackjack Best B+ mod" ' started 2019-06-06
  5.  
  6. CONST rank$ = "A23456789XJQK"
  7. CONST player = 1
  8. CONST dealer = 2
  9.  
  10. TYPE playerType
  11.     ID AS STRING
  12.     Hand AS STRING
  13.     Ace AS INTEGER
  14.     Total AS INTEGER
  15. DIM SHARED deck$(1 TO 52), players(1 TO 2) AS playerType, deckIndex
  16.  
  17. DIM i, r, card$, chips, bet, setbet
  18. players(player).ID = "Player"
  19. players(dealer).ID = "Dealer"
  20. FOR i = 1 TO 52
  21.     deck$(i) = MID$(rank$ + rank$ + rank$ + rank$, i, 1)
  22. chips = 100
  23. cp 8, "For this Blackjack Game, you start with 100 chips"
  24. cp 9, "and can bet any amount of them for each game."
  25. cp 10, "If you'd like to set your bet for each game now"
  26. cp 11, "enter that amount otherwise enter 0 "
  27. LOCATE 12, 38: INPUT ""; setbet
  28.     IF setbet = 0 THEN
  29.         CLS
  30.         cp 10, "You have" + STR$(chips) + " chips to bet."
  31.         LOCATE 11, 25: INPUT "(0 quits) Enter your bet > ", bet
  32.         IF bet = 0 THEN EXIT DO
  33.         IF bet > chips THEN bet = chips
  34.     ELSE
  35.         bet = setbet
  36.     END IF
  37.     clearPlayers 'clears screen too
  38.     cp 4, "BLACKJACK    chips:" + STR$(chips) + "   betting:" + STR$(bet) + " chips."
  39.     FOR i = 52 TO 2 STEP -1 'shuffle
  40.         r = INT(RND * i) + 1
  41.         SWAP deck$(r), deck$(i)
  42.     NEXT
  43.     deckIndex = 0
  44.     FOR i = 1 TO 2 'each Player is dealt 2 cards
  45.         PlayerAddCard player
  46.         PlayerAddCard dealer
  47.     NEXT
  48.     cp 7, players(dealer).ID + " ? " + MID$(players(dealer).Hand, 2, 1)
  49.     cp 10, playerShow$(player)
  50.     IF players(player).Total = 21 THEN
  51.         chips = chips + 2 * bet
  52.         cp 11, "BlackJack! You added" + STR$(2 * bet) + " to your chips."
  53.         _DELAY 2
  54.         GOTO BJskip
  55.     END IF
  56.     WHILE players(player).Total < 21
  57.         cp 11, "Press h for Hit, any other to stay..."
  58.         card$ = INKEY$
  59.         WHILE LEN(card$) = 0: card$ = INKEY$: _LIMIT 60: WEND
  60.         IF card$ = "h" THEN
  61.             PlayerAddCard player
  62.             cp 11, SPACE$(50)
  63.             cp 10, playerShow$(player)
  64.         ELSE
  65.             cp 11, SPACE$(50)
  66.             EXIT WHILE
  67.         END IF
  68.     WEND
  69.     cp 7, playerShow$(dealer)
  70.     _DELAY 1
  71.     WHILE players(player).Total < 22 AND (players(dealer).Total < 21 AND players(dealer).Total < players(player).Total)
  72.         cp 8, "Dealer takes a card."
  73.         _DELAY 1
  74.         PlayerAddCard dealer
  75.         cp 7, playerShow$(dealer)
  76.         cp 8, SPACE$(50)
  77.         _DELAY 2
  78.     WEND
  79.     IF players(player).Total > 21 OR (players(player).Total <= players(dealer).Total AND players(dealer).Total < 22) THEN
  80.         cp 13, "You lose."
  81.         chips = chips - bet
  82.     ELSE
  83.         cp 13, "You win!"
  84.         chips = chips + bet
  85.     END IF
  86.     BJskip:
  87.     IF chips = 0 THEN cp 15, "Out of chips!"
  88.     _DELAY 4
  89. LOOP UNTIL chips = 0
  90. cp 17, "Goodbye"
  91.  
  92. SUB clearPlayers
  93.     DIM i
  94.     CLS
  95.     FOR i = 1 TO 2
  96.         players(i).Hand = ""
  97.         players(i).Ace = 0
  98.         players(i).Total = 0
  99.     NEXT
  100.  
  101. SUB PlayerAddCard (receiver)
  102.     DIM i AS INTEGER, cv AS INTEGER
  103.     deckIndex = deckIndex + 1
  104.     players(receiver).Hand = players(receiver).Hand + deck$(deckIndex)
  105.     IF deck$(deckIndex) = "A" THEN players(receiver).Ace = -1
  106.     players(receiver).Total = 0
  107.     FOR i = 1 TO LEN(players(receiver).Hand)
  108.         IF INSTR(rank, MID$(players(receiver).Hand, i, 1)) > 10 THEN cv = 10 ELSE cv = INSTR(rank, MID$(players(receiver).Hand, i, 1))
  109.         players(receiver).Total = players(receiver).Total + cv
  110.     NEXT
  111.     IF players(receiver).Total < 12 AND players(receiver).Ace THEN players(receiver).Total = players(receiver).Total + 10
  112.  
  113. FUNCTION playerShow$ (shower)
  114.     DIM i AS INTEGER, S$
  115.     S$ = players(shower).ID + " "
  116.     FOR i = 1 TO LEN(players(shower).Hand)
  117.         S$ = S$ + MID$(players(shower).Hand, i, 1) + " "
  118.     NEXT
  119.     S$ = S$ + "Total =" + STR$(players(shower).Total)
  120.     IF players(shower).Total > 21 THEN S$ = S$ + " Busted!"
  121.     playerShow$ = S$
  122.  
  123. SUB cp (row, s AS STRING)
  124.     LOCATE row, (80 - LEN(s)) / 2: PRINT s
  125.  

I am thinking of setting up a number of dummy players so Dealer wouldn't be looking to beat any one player in particular, ie it would have to stay more often than it does here, playing one-on-one.

« Last Edit: June 09, 2019, 08:53:19 AM by bplus »

Offline johnno56

  • Forum Resident
  • Posts: 790
  • Live long and prosper.
Re: Blackjack
« Reply #1 on: June 07, 2019, 07:08:23 PM »
Cut and paste into IDE. Run... Then this....

Logic is the beginning of wisdom.

Online bplus

  • Forum Resident
  • Posts: 4616
  • B+ nots
Re: Blackjack
« Reply #2 on: June 07, 2019, 07:57:09 PM »
Hi Johnno,

Yeah time to update to QB64 v1.3 where Type allows variable length strings. :)

This version of Blackjack will save you bundles of money lost at casinos. ;D

Besides, you might have card images and noises for a real app. ;-))

Offline johnno56

  • Forum Resident
  • Posts: 790
  • Live long and prosper.
Re: Blackjack
« Reply #3 on: June 07, 2019, 09:05:34 PM »
Well spotted. I indeed was 'still' using version 1.2. New version installed. No more error. Many thanks.

J

ps: Your 'dealer' cleaned me out!! :(
Logic is the beginning of wisdom.

Online bplus

  • Forum Resident
  • Posts: 4616
  • B+ nots
Re: Blackjack
« Reply #4 on: June 07, 2019, 09:19:59 PM »
Well spotted. I indeed was 'still' using version 1.2. New version installed. No more error. Many thanks.

J

ps: Your 'dealer' cleaned me out!! :(


Yeah! That's why I need to add more dummy players, so it has to "stay" more conservative, instead of drawing cards until it matches or beats or busts the only player it goes against.

Offline Pete

  • Forum Resident
  • Posts: 1983
  • Cuz I sez so, varmint!
Re: Blackjack
« Reply #5 on: June 07, 2019, 09:28:47 PM »
One on my first programs after I graduated colors was Black Jack, played Las Vegas style, out of a shoe. Instead of a two player game, I made it an analysis program, to test different well documented playing strategies. I can credit that project with keeping out of casinos. In order to win anything of any significance, you have to make large bets, and play all day. You also literally cannot afford to make mistakes. You risk being banned for card counting if you actually do well, and, as one interesting book I read stated, "If you think people who built these casinos on drug money and prostitution wouldn't cheat you are cards, there is no helping you." All the casino has to do is take out a ace, pop in a couple of extra 5's or 2's, and you're totally screwed, no matter how well you play the system. I wish I could credit that program to QBasic, but at that time, I was using an Atari.

Pete

Online bplus

  • Forum Resident
  • Posts: 4616
  • B+ nots
Re: Blackjack
« Reply #6 on: June 08, 2019, 10:09:47 AM »
Hi Pete,

You know, adding aces to the deck might make my little Blackjack game more interesting and be another way to overcome the dealer's advantage of winning ties. I would have to change the neat way to total a hand and create the deck but I am no longer interested in minimum lines of code.


Preliminary plans with dummy players:
The shoe in my game is only 1 deck and is shuffled before each round. I like the simplicity of that.

So I think 3 dummies plus me and dealer is ideal, just enough cards used that card counting might work, if you want to try a hand at that. Let the 3 dummies go first get a feel of cards played.

So far my dummy players have a choice of playing a set bet amount all the time or altering the bet. But what conditions could possible be used to alter the bet intelligently?

As I have been coding the game, all the betting occurs before any cards are shown. I know in real games after 2 cards dealt (dealer shows 1 face up), you can double down or split or buy insurance... however that goes, that too might make game more enjoyable against dealer and house rules.

Main rule at casinos: The Odds must be in favor of the house! Which is no fun!

My main rule: Have fun playing (and coding). Which means a probable chance of payoff for diligence and skill employed.

Well I guess I am just thinking out loud here... coding rarely ever goes as I had thought in preliminary plans.
But the aces idea with dummy players seem a good experiment.

Online bplus

  • Forum Resident
  • Posts: 4616
  • B+ nots
Re: Blackjack
« Reply #7 on: June 09, 2019, 12:18:52 AM »
12 Aces and 4 bots including the dealer:
Code: QB64: [Select]
  1. OPTION _EXPLICIT '     No suits for cards A is Ace, J, Q, K are Jack, Queen, King, X is Roman for 10
  2. DEFINT A-Z '              Dealer wins all ties except a Player's Blackjack which pays double the bet
  3. _TITLE "Blackjack with Bots" ' started 2019-06-07 Players 1 through 3 are bots you play bottom right
  4.  
  5. TYPE XY 'graphic or cell location on screen or in array
  6.     X AS INTEGER
  7.     Y AS INTEGER
  8.  
  9. TYPE Box
  10.     TopLeft AS XY
  11.     W AS INTEGER
  12.     H AS INTEGER
  13.     BC AS _UNSIGNED LONG
  14.     FC AS _UNSIGNED LONG
  15.  
  16. TYPE PlayerType 'a container for all program data!
  17.     Win AS Box 'for screening the players data
  18.     ID AS STRING
  19.     Chips AS INTEGER
  20.     SetBet AS INTEGER
  21.     Bet AS INTEGER
  22.     Hand AS STRING
  23.     Ace AS INTEGER
  24.     Total AS INTEGER
  25.     Blackjack AS INTEGER
  26.  
  27. CONST xmax = 640
  28. CONST ymax = 432
  29. CONST nPlayers = 5 '       the 5th is the Dealer, the 4th is You and 1 - 3 are the bots who go first
  30. CONST You = 4
  31. CONST Dealer = 5
  32. CONST dk = "A2345A6789AXJQKA2345A6789AXJQKA2345A6789AXJQKA2345A6789AXJQK"
  33. CONST white = &HFFFDDFDD
  34. CONST black = &HFF000000
  35. CONST green = &HFF004515
  36. CONST orange = &HFFFF3300
  37. CONST brown = &HFF3C1D06
  38. CONST cyan = &HFF3774C0
  39. CONST ltCyan = &HFF99BBFF
  40. CONST drkRed = &HFF220102
  41. CONST mRed = &HFF7F0000
  42. CONST yellow = &HFFFFFF00
  43. CONST blue = &HFF00008D
  44.  
  45. DIM SHARED deck$(1 TO LEN(dk)), player(1 TO nPlayers) AS PlayerType, deckIndex
  46. DIM i, j, r, in, IS$, stillPlayer
  47. SCREEN _NEWIMAGE(xmax, ymax, 32)
  48. COLOR yellow, green
  49. FOR i = 1 TO LEN(dk)
  50.     deck$(i) = MID$(dk, i, 1)
  51. initPlayers
  52.     clearPlayers 'clears screen too                                                    start a round
  53.     stillPlayer = 0 ' to check at end of round if there is still a player playing
  54.  
  55.     IF player(3).Chips > 0 THEN '                                                        handle bets
  56.         player(3).Bet = INT(player(3).Chips / 2 + .5)
  57.     END IF
  58.     IF player(You).Chips > 0 THEN
  59.         IF player(You).SetBet = 0 THEN
  60.             CLS
  61.             cp 0, 10, "You have" + STR$(player(4).Chips) + " chips to bet."
  62.             LOCATE 11, 25: INPUT "Enter your bet > ", in
  63.             IF in = 0 THEN EXIT DO
  64.             IF in > player(You).Chips THEN player(You).Bet = player(You).Chips ELSE player(You).Bet = in
  65.             CLS
  66.         ELSE
  67.             player(You).Bet = player(You).SetBet
  68.         END IF
  69.     END IF
  70.     FOR i = 1 TO 4 'make sure everyone is not betting more than amount of chips they have
  71.         IF player(i).Bet > player(i).Chips THEN player(i).Bet = player(i).Chips
  72.     NEXT
  73.  
  74.     FOR i = LEN(dk) TO 2 STEP -1 '                                                                shuffle
  75.         r = INT(RND * i) + 1
  76.         SWAP deck$(r), deck$(i)
  77.     NEXT
  78.     deckIndex = 0
  79.  
  80.     FOR j = 1 TO 2 '                                 everybody gets 2 cards if they still have chips
  81.         FOR i = 1 TO nPlayers 'each Player is dealt 2 cards
  82.             IF ((player(i).Chips > 0) AND (i <> Dealer)) OR (i = Dealer) THEN
  83.                 addCard i
  84.                 showPlayer i
  85.                 IF i = Dealer THEN
  86.                     cp Dealer, 7, SPACE$(18)
  87.                     cp Dealer, 8, SPACE$(18)
  88.                     IF j = 1 THEN
  89.                         cp Dealer, 7, "? "
  90.                         cp Dealer, 8, "Total: ?"
  91.                     ELSE
  92.                         cp Dealer, 7, SPACE$(18)
  93.                         cp Dealer, 7, "Hand: ? " + MID$(player(Dealer).Hand, 2, 1)
  94.                         cp Dealer, 8, "Total: ?"
  95.                     END IF
  96.                 END IF
  97.                 _DELAY 1
  98.                 IF i <> Dealer AND j = 2 AND player(i).Total = 21 THEN
  99.                     cp i, 9, "Blackjack!"
  100.                     player(i).Blackjack = -1
  101.                     _DELAY 1
  102.                 END IF
  103.             ELSE
  104.                 clsPlayer i
  105.             END IF
  106.         NEXT
  107.     NEXT
  108.  
  109.     FOR i = 1 TO nPlayers - 1 '                    OK now handle the players until they stay or bust
  110.         IF player(i).Chips > 0 THEN
  111.             WHILE player(i).Total < 21
  112.                 IF i = You THEN
  113.                     cp i, 11, "<h>it <s>tay"
  114.                     IS$ = INKEY$
  115.                     WHILE LEN(IS$) = 0: IS$ = INKEY$: _LIMIT 60: WEND
  116.                     IF IS$ = "h" THEN
  117.                         addCard You
  118.                         cp i, 11, SPACE$(18)
  119.                         showPlayer i
  120.                     ELSE
  121.                         cp i, 11, SPACE$(18)
  122.                         EXIT WHILE
  123.                     END IF
  124.                 ELSE
  125.                     IF player(i).Total < 16 THEN '                               bots to hit on < 16
  126.                         cp i, 11, "Hit me."
  127.                         _DELAY 1
  128.                         addCard i
  129.                         showPlayer i
  130.                         _DELAY 2
  131.                     ELSE
  132.                         EXIT WHILE 'stay
  133.                     END IF
  134.                 END IF
  135.             WEND
  136.         END IF
  137.     NEXT
  138.  
  139.     showPlayer Dealer '                                                                dealer's turn
  140.     _DELAY 1
  141.     WHILE player(Dealer).Total < 17
  142.         cp Dealer, 10, "(Takes a card)"
  143.         _DELAY 1
  144.         addCard Dealer
  145.         showPlayer Dealer
  146.         cp Dealer, 10, SPACE$(18)
  147.         _DELAY 2
  148.     WEND
  149.  
  150.     FOR i = 1 TO nPlayers - 1 '                                   name the winners and losers, payup
  151.         IF player(i).Chips > 0 THEN
  152.             IF player(i).Blackjack = 0 THEN
  153.                 IF player(i).Total > 21 OR (player(i).Total <= player(Dealer).Total AND player(Dealer).Total < 22) THEN
  154.                     cp i, 11, player(i).ID + " lost."
  155.                     player(i).Chips = player(i).Chips - player(i).Bet
  156.                     player(Dealer).Chips = player(Dealer).Chips + player(i).Bet
  157.                 ELSE
  158.                     cp i, 11, player(i).ID + " won!"
  159.                     player(i).Chips = player(i).Chips + player(i).Bet
  160.                     player(Dealer).Chips = player(Dealer).Chips - player(i).Bet
  161.                 END IF
  162.             ELSE
  163.                 cp i, 11, player(i).ID + " won!"
  164.                 player(i).Chips = player(i).Chips + 2 * player(i).Bet
  165.                 player(Dealer).Chips = player(Dealer).Chips - 2 * player(i).Bet
  166.             END IF
  167.             IF player(i).Chips = 0 THEN cp i, 12, "Out of chips!" ELSE stillPlayer = -1
  168.         END IF
  169.         _DELAY .5
  170.     NEXT
  171.     _DELAY 3
  172. LOOP UNTIL stillPlayer = 0
  173. cp 0, 12, "Goodbye"
  174.  
  175. SUB clearPlayers 'clear player data for new game
  176.     DIM i
  177.     CLS
  178.     FOR i = 1 TO nPlayers
  179.         player(i).Hand = ""
  180.         player(i).Ace = 0
  181.         player(i).Total = 0
  182.         player(i).Blackjack = 0
  183.     NEXT
  184.  
  185. SUB addCard (nPlayer) 'add a card to reciever's hand and retotal points according to aces present
  186.     DIM i AS INTEGER
  187.     deckIndex = deckIndex + 1
  188.     player(nPlayer).Hand = player(nPlayer).Hand + deck$(deckIndex)
  189.     IF deck$(deckIndex) = "A" THEN player(nPlayer).Ace = -1
  190.     player(nPlayer).Total = 0
  191.     FOR i = 1 TO LEN(player(nPlayer).Hand)
  192.         'IF INSTR(rank, MID$(player(nPlayer).Hand, i, 1)) > 10 THEN cv = 10 ELSE cv = INSTR(rank, MID$(player(nPlayer).Hand, i, 1))
  193.         player(nPlayer).Total = player(nPlayer).Total + CardValue(MID$(player(nPlayer).Hand, i, 1))
  194.     NEXT
  195.     IF player(nPlayer).Total < 12 AND player(nPlayer).Ace THEN player(nPlayer).Total = player(nPlayer).Total + 10
  196.  
  197. SUB showPlayer (nPlayer)
  198.     DIM i AS INTEGER, S$
  199.     clsPlayer nPlayer
  200.     cp nPlayer, 1, player(nPlayer).ID
  201.     cp nPlayer, 3, "Chips:" + STR$(player(nPlayer).Chips)
  202.     IF nPlayer <> Dealer THEN
  203.         cp nPlayer, 4, "Bet:" + STR$(player(nPlayer).Bet)
  204.     END IF
  205.     FOR i = 1 TO LEN(player(nPlayer).Hand) 'with 12 aces could get very long hands
  206.         IF LEN(player(nPlayer).Hand) * 2 < player(nPlayer).Win.W THEN
  207.             S$ = S$ + MID$(player(nPlayer).Hand, i, 1) + " "
  208.         ELSE
  209.             S$ = S$ + MID$(player(nPlayer).Hand, i, 1)
  210.         END IF
  211.     NEXT
  212.     cp nPlayer, 6, "___Hand___"
  213.     cp nPlayer, 7, S$
  214.     cp nPlayer, 8, "Total:" + STR$(player(nPlayer).Total)
  215.     IF player(nPlayer).Total > 21 THEN
  216.         cp nPlayer, 9, "Busted!"
  217.     END IF
  218.  
  219. SUB clsPlayer (nPlayer)
  220.     DIM i, lastR
  221.     COLOR player(nPlayer).Win.FC, player(nPlayer).Win.BC
  222.     IF nPlayer = Dealer THEN
  223.         lastR = player(nPlayer).Win.TopLeft.Y + player(nPlayer).Win.H - 1
  224.     ELSE
  225.         lastR = player(nPlayer).Win.TopLeft.Y + player(nPlayer).Win.H
  226.     END IF
  227.     FOR i = player(nPlayer).Win.TopLeft.Y TO lastR
  228.         LOCATE i, player(nPlayer).Win.TopLeft.X: PRINT SPACE$(player(nPlayer).Win.W);
  229.     NEXT
  230.     COLOR yellow, green
  231.  
  232. SUB cp (nPlayer, row, s AS STRING) 'center print a string on the given row
  233.     IF nPlayer THEN
  234.         COLOR player(nPlayer).Win.FC, player(nPlayer).Win.BC
  235.         LOCATE player(nPlayer).Win.TopLeft.Y + row, player(nPlayer).Win.TopLeft.X + (player(nPlayer).Win.W - LEN(s)) / 2: PRINT s;
  236.         COLOR yellow, green
  237.     ELSE
  238.         COLOR yellow, green
  239.         LOCATE row, (80 - LEN(s)) / 2: PRINT s;
  240.     END IF
  241.  
  242. SUB initPlayers 'the stuff that never changes
  243.     DIM i, widt, height
  244.  
  245.     'for printing in each players window area of screen = blackjack table
  246.     widt = _WIDTH / (4 * 8)
  247.     height = _HEIGHT / (2 * 16)
  248.     FOR i = 1 TO nPlayers - 1
  249.         player(i).Win.TopLeft.X = widt * (i - 1) + 2
  250.         player(i).Win.TopLeft.Y = height - 1
  251.         player(i).Win.W = widt - 2
  252.         player(i).Win.H = height - 1
  253.         player(i).Chips = 100
  254.     NEXT
  255.     player(Dealer).Win.TopLeft.X = (_WIDTH / 8 - widt) / 2 + 1
  256.     player(Dealer).Win.TopLeft.Y = 1
  257.     player(Dealer).Win.W = widt
  258.     player(Dealer).Win.H = height - 2
  259.     player(Dealer).Chips = -400 'the dealer has "lent" 100 chips to each player
  260.  
  261.     player(1).Win.FC = brown: player(1).Win.BC = orange: player(1).ID = "Pete": player(1).SetBet = 5: player(1).Bet = 5
  262.     player(2).Win.FC = drkRed: player(2).Win.BC = cyan: player(2).ID = "Qwerkey": player(2).SetBet = 15: player(2).Bet = 15
  263.     player(3).Win.FC = white: player(3).Win.BC = mRed: player(3).ID = "Johnno": player(3).SetBet = 0
  264.     player(4).Win.FC = ltCyan: player(4).Win.BC = blue: player(4).ID = "You"
  265.     player(5).Win.FC = yellow: player(5).Win.BC = green: player(5).ID = "Dealer"
  266.  
  267.     'ask the user / YOU what e prefers for betting
  268.     CLS
  269.     cp 0, 8, "For this Blackjack Game, you start with 100 chips"
  270.     cp 0, 9, "and can bet any amount of them for each game."
  271.     cp 0, 10, "If you'd like to set your bet for each game now"
  272.     cp 0, 11, "enter that amount otherwise enter 0 and get asked"
  273.     cp 0, 12, "before each round at the table."
  274.     LOCATE 13, 38: INPUT ""; i
  275.     IF i < 0 THEN i = 0 'lets not do allot of checking and reasking
  276.     IF i > 100 THEN i = 100
  277.     player(You).SetBet = i
  278.  
  279. FUNCTION CardValue (s$)
  280.     IF s$ = "A" THEN
  281.         CardValue = 1
  282.     ELSEIF INSTR("23456789", s$) > 0 THEN
  283.         CardValue = VAL(s$)
  284.     ELSEIF INSTR("XJQK", s$) > 0 THEN
  285.         CardValue = 10
  286.     END IF
  287.  

Offline QBExile

  • Newbie
  • Posts: 25
Re: Blackjack
« Reply #8 on: June 09, 2019, 05:17:25 AM »
Very nice program!
One spelling error i encountered is "You loose." should be "You lose."

Online bplus

  • Forum Resident
  • Posts: 4616
  • B+ nots
Re: Blackjack
« Reply #9 on: June 09, 2019, 08:54:50 AM »
Very nice program!
One spelling error i encountered is "You loose." should be "You lose."

Oh, 'loose" was in first program... OK fixed, Thanks

A couple of things I like in second program is using Types within Types that creates a window for each player, and a perfect accounting system: Dealer's Total Chips + all the Player's Chips = 0 (and the Player's chips are always positive or 0).
« Last Edit: June 09, 2019, 09:07:18 AM by bplus »

Online bplus

  • Forum Resident
  • Posts: 4616
  • B+ nots
Re: Blackjack
« Reply #10 on: June 10, 2019, 11:47:14 AM »
Thanks to Code Hunter and his deck image png file, I have more visual representation of cards.

I also eased up on the House Rules st the Dealer only draws until the total > 16 PLUS this game allows more ties eg when both players bust or match totals.

Code: QB64: [Select]
  1. OPTION _EXPLICIT ' Easier House Rules: Dealer must draw if card total < 17.
  2. DEFINT A-Z '       Player's Blackjack pays double the bet set at the beginning of the game.
  3. RANDOMIZE TIMER '  If both Dealer and Player match totals or bust, it is a tie.
  4. _TITLE "BJ 1on1 wImages" ' started 2019-06-09 B+ thanks to Code Hunter Card Image file.
  5. CONST xmax = 800, ymax = 640 '40 lines
  6. SCREEN _NEWIMAGE(xmax, ymax, 32)
  7. _SCREENMOVE 300, 20
  8.  
  9. TYPE CardType
  10.     ID AS INTEGER
  11.     cname AS STRING
  12.     value AS INTEGER '1 to 13
  13.     suit AS INTEGER
  14.     points AS INTEGER '1 to 10
  15.  
  16. TYPE PlayerType
  17.     ID AS STRING
  18.     hand AS STRING
  19.     cardY AS INTEGER
  20.     Ace AS INTEGER
  21.     Total AS INTEGER
  22.  
  23. CONST player = 1
  24. CONST dealer = 2
  25.  
  26. DIM SHARED Deck(1 TO 52) AS CardType, Players(1 TO 2) AS PlayerType, deckIndex
  27. DIM SHARED CardDeck AS LONG, round
  28. Players(player).ID = "Player": Players(player).cardY = 420
  29. Players(dealer).ID = "Dealer": Players(dealer).cardY = 100
  30. DIM SHARED bet, chips
  31. DIM i, card$, setbet, loaded
  32.  
  33. loaded = loadCardImages
  34. IF loaded = 0 THEN PRINT "PlayingCards.png failed to load, bye!": END
  35.  
  36. initDeck
  37.  
  38. chips = 100
  39. cp 8, "For this Blackjack Game, you start with 100 chips"
  40. cp 9, "and can bet any amount of them for each game."
  41. cp 10, "If you'd like to set your bet for each game now"
  42. cp 11, "enter that amount otherwise enter 0 "
  43. cp 12, ""
  44. INPUT ""; setbet
  45.     IF setbet = 0 THEN
  46.         CLS
  47.         cp 10, "You have" + STR$(chips) + " chips to bet."
  48.         LOCATE 11, 25: INPUT "(0 quits) Enter your bet > ", bet
  49.         IF bet = 0 THEN EXIT DO
  50.         IF bet > chips THEN bet = chips
  51.     ELSE
  52.         bet = setbet
  53.     END IF
  54.     clearPlayers 'clears screen too
  55.     shuffle
  56.     deckIndex = 0
  57.     FOR i = 1 TO 2 'each Player is dealt 2 cards
  58.         PlayerAddCard player
  59.         PlayerAddCard dealer
  60.         round = round + 1
  61.     NEXT
  62.     'cp 7, Players(dealer).ID + " ? " + MID$(Players(dealer).hand, 2, 1)
  63.     playerShow dealer
  64.     playerShow player
  65.     IF Players(player).Total = 21 THEN
  66.         chips = chips + 2 * bet
  67.         cp 36, "BlackJack! You added" + STR$(2 * bet) + " to your chips."
  68.         _DELAY 2
  69.         GOTO BJskip
  70.     END IF
  71.     WHILE Players(player).Total < 21
  72.         cp 36, "Press h for Hit, any other to stay..."
  73.         card$ = INKEY$
  74.         WHILE LEN(card$) = 0: card$ = INKEY$: _LIMIT 60: WEND
  75.         IF card$ = "h" THEN
  76.             PlayerAddCard player
  77.             playerShow player
  78.         ELSE
  79.             cp 38, SPACE$(50)
  80.             EXIT WHILE
  81.         END IF
  82.     WEND
  83.     round = round + 1 'now round > 2 so first card won't be hidden
  84.     playerShow dealer
  85.     _DELAY 1
  86.     WHILE Players(dealer).Total < 17
  87.         cp 16, "Dealer takes a card."
  88.         _DELAY 1
  89.         PlayerAddCard dealer
  90.         playerShow dealer
  91.         _DELAY 2
  92.     WEND
  93.     IF (Players(player).Total > 21 AND Players(dealer).Total < 22) OR (Players(player).Total < Players(dealer).Total AND Players(dealer).Total < 22) THEN
  94.         cp 38, "You lost."
  95.         chips = chips - bet
  96.     ELSEIF (Players(player).Total > Players(dealer).Total AND Players(player).Total < 22) OR (Players(player).Total < 22 AND Players(dealer).Total > 21) THEN
  97.         cp 38, "You won!"
  98.         chips = chips + bet
  99.     ELSEIF (Players(player).Total = Players(dealer).Total) OR (Players(player).Total > 21 AND Players(dealer).Total > 21) THEN
  100.         cp 38, "We tied..."
  101.     END IF
  102.     BJskip:
  103.     IF chips = 0 THEN cp 40, "Out of chips!"
  104.     _DELAY 4
  105. LOOP UNTIL chips = 0
  106. cp 5, "Goodbye"
  107.  
  108. SUB clearPlayers
  109.     DIM i
  110.     CLS
  111.     FOR i = 1 TO 2
  112.         Players(i).hand = ""
  113.         Players(i).Ace = 0
  114.         Players(i).Total = 0
  115.     NEXT
  116.     round = 0
  117.  
  118. SUB PlayerAddCard (receiver)
  119.     DIM i, di
  120.     deckIndex = deckIndex + 1
  121.     Players(receiver).hand = Players(receiver).hand + d2$(deckIndex)
  122.     IF Deck(deckIndex).value = 1 THEN Players(receiver).Ace = -1
  123.     Players(receiver).Total = 0
  124.     FOR i = 1 TO LEN(Players(receiver).hand) STEP 2
  125.         di = VAL(MID$(Players(receiver).hand, i, 2))
  126.         Players(receiver).Total = Players(receiver).Total + Deck(di).points
  127.     NEXT
  128.     IF Players(receiver).Total < 12 AND Players(receiver).Ace THEN Players(receiver).Total = Players(receiver).Total + 10
  129.  
  130. FUNCTION d2$ (num) 'we have to store 1 or 2 digit numbers in a string to represent hand that is buch of card indexes
  131.     d2$ = LEFT$(_TRIM$(STR$(num)) + "  ", 2)
  132.  
  133. SUB playerShow (shower)
  134.     DIM i, xoff, di
  135.  
  136.     IF shower = dealer THEN
  137.         LINE (0, 0)-STEP(xmax, 319), &HFF000000, BF
  138.         cp 2, "B+ BLACKJACK: If Dealer total < 16 then must hit else stays."
  139.         cp 5, Players(shower).ID
  140.         cp 15, "Total =" + STR$(Players(shower).Total)
  141.         IF Players(shower).Total > 21 THEN cp 16, "Busted!"
  142.     ELSE
  143.         LINE (0, 320)-STEP(xmax, 320), &HFF000000, BF
  144.         cp 25, "Chips:" + STR$(chips) + "   Betting:" + STR$(bet) + " chips."
  145.         cp 23, Players(shower).ID
  146.         cp 35, "Total =" + STR$(Players(shower).Total)
  147.         IF Players(shower).Total > 21 THEN cp 36, "Busted!"
  148.     END IF
  149.     xoff = (xmax - 43 * LEN(Players(shower).hand)) / 2
  150.     FOR i = 1 TO LEN(Players(shower).hand) STEP 2
  151.         di = VAL(MID$(Players(shower).hand, i, 2))
  152.         showCardDI xoff, Players(shower).cardY, di
  153.         xoff = xoff + 86
  154.     NEXT
  155.     IF shower = dealer AND round <= 2 THEN 'hide first card
  156.         xoff = (xmax - 43 * LEN(Players(shower).hand)) / 2
  157.         ShowCard xoff, Players(dealer).cardY, 0, 4
  158.         cp 15, " Total = ?? "
  159.     END IF
  160.  
  161. SUB cp (row, s AS STRING)
  162.     LOCATE row, (xmax / 8 - LEN(s)) / 2: PRINT s;
  163.  
  164. SUB initDeck
  165.     DIM i, s, v
  166.     DIM suit$(3)
  167.     suit$(0) = "Hearts": suit$(1) = "Clubs": suit$(2) = "Diamonds": suit$(3) = "Spades"
  168.     DIM v$(1 TO 13)
  169.     v$(1) = "Ace": v$(11) = "Jack": v$(12) = "Queen": v$(13) = "King"
  170.     FOR i = 2 TO 10
  171.         v$(i) = _TRIM$(STR$(i))
  172.     NEXT
  173.     i = 0
  174.     FOR s = 0 TO 3
  175.         FOR v = 1 TO 13
  176.             i = i + 1
  177.             Deck(i).ID = i
  178.             Deck(i).cname = v$(v) + " of " + suit$(s)
  179.             Deck(i).value = v
  180.             Deck(i).suit = s
  181.             IF v < 11 THEN Deck(i).points = v ELSE Deck(i).points = 10
  182.         NEXT
  183.     NEXT
  184.  
  185. SUB shuffle
  186.     DIM i, r
  187.     FOR i = 52 TO 2 STEP -1
  188.         r = INT(i * RND) + 1
  189.         SWAP Deck(i), Deck(r)
  190.     NEXT
  191.  
  192. SUB showCardDI (x AS INTEGER, y AS INTEGER, deckI AS INTEGER)
  193.     ShowCard x, y, Deck(deckI).value - 1, Deck(deckI).suit
  194.  
  195. 'at scale = 2 need screen space of 76 x 100 for one card
  196. SUB ShowCard (ScrnX, ScrnY, imageCol, imageRow) 'screen x, y  h= horizontal  , v =vertical  image h, v
  197.     DIM ch, cv, scale
  198.     ch = 38 * imageCol + 1: cv = 51 * imageRow + 1: scale = 2 ' <  might want to change?
  199.     _PUTIMAGE (ScrnX, ScrnY)-STEP(38 * scale, 50 * scale), CardDeck, 0, (ch, cv)-(ch + 37, cv + 50)
  200.  
  201. FUNCTION loadCardImages%
  202.     CardDeck = _LOADIMAGE("PlayingCards.png")
  203.     IF CardDeck = -1 THEN EXIT FUNCTION
  204.     _SOURCE CardDeck
  205.     _CLEARCOLOR POINT(0, 0), CardDeck
  206.     _SOURCE 0
  207.     loadCardImages% = -1
  208.  
  209.  

 

Offline Pete

  • Forum Resident
  • Posts: 1983
  • Cuz I sez so, varmint!
Re: Blackjack
« Reply #11 on: June 10, 2019, 04:10:40 PM »
You owe me 500 chips. I like the ones with ruffles.

Any thoughts to add a way to change the betting amount?

Pete

Online bplus

  • Forum Resident
  • Posts: 4616
  • B+ nots
Re: Blackjack
« Reply #12 on: June 10, 2019, 05:24:02 PM »
You owe me 500 chips. I like the ones with ruffles.

Any thoughts to add a way to change the betting amount?

Pete

You can bet any amount up to the amount of chips you have each round by entering 0 or just enter on very first screen.
Quote
cp 8, "For this Blackjack Game, you start with 100 chips"
cp 9, "and can bet any amount of them for each game."
cp 10, "If you'd like to set your bet for each game now"
cp 11, "enter that amount otherwise enter 0 "

I am planning more options for the player at Hit or Stay time so if you've won so much money it's burning holes in your pocket you will have option to change the SetBet amount and maybe change your room to the Penthouse Suite.
« Last Edit: June 10, 2019, 05:29:15 PM by bplus »

Offline johnno56

  • Forum Resident
  • Posts: 790
  • Live long and prosper.
Re: Blackjack
« Reply #13 on: June 10, 2019, 05:45:00 PM »
Nicely done! But, not so much, the $600 your dealer took from me!!  lol
Logic is the beginning of wisdom.

Online bplus

  • Forum Resident
  • Posts: 4616
  • B+ nots
Re: Blackjack
« Reply #14 on: June 10, 2019, 05:59:40 PM »
Nicely done! But, not so much, the $600 your dealer took from me!!  lol

See the bundle I am saving you by keeping you out of casinos! :-))