Author Topic: Blackjack  (Read 56318 times)

0 Members and 1 Guest are viewing this topic.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Blackjack
« Reply #195 on: July 05, 2020, 11:14:34 pm »
Here is that first version in SCREEN 0 with same functions as Big Brother Graphics versions ready to replace bplus participation with AI:
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. '2020-07-05 fix hit (it wasn't brolen), delete line card$ = inkey$ before loop
  4. ' force dealer to hit on 16 stay 17
  5. ' ties are push
  6. ' bring in double down option
  7. ' fix dealer has Blackjack should tell also fix exposing 2nd card
  8. ' work this towards converting to multiple AI players, so players stats are all located in PlayerType
  9.  
  10.  
  11. _TITLE "Blackjack Best B+ mod" ' started 2019-06-06, revisit and match functions with B+J Balckjack with audio and graphics.
  12. CONST rank$ = "A23456789XJQK"
  13. CONST Player = 1
  14. CONST Dealer = 2
  15.  
  16. TYPE PlayerType
  17.     ID AS STRING
  18.     Hand AS STRING
  19.     Ace AS INTEGER
  20.     Total AS INTEGER
  21.     chips AS _INTEGER64
  22.     setBet AS _INTEGER64
  23.     bet AS _INTEGER64
  24.  
  25. DIM SHARED deck$(1 TO 52), Players(1 TO 2) AS PlayerType, deckIndex
  26.  
  27. DIM i, r, card$
  28. Players(Player).ID = "Player"
  29. Players(Dealer).ID = "Dealer"
  30. FOR i = 1 TO 52
  31.     deck$(i) = MID$(rank$ + rank$ + rank$ + rank$, i, 1)
  32. Players(Dealer).chips = -100
  33. Players(Player).chips = 100
  34. cp 8, "For this Blackjack Game, you start with 100 chips"
  35. cp 9, "and can bet any amount of them for each game."
  36. cp 10, "If you'd like to set your bet for each game now"
  37. cp 11, "enter that amount otherwise enter 0 "
  38. cp 12, "Dealer must hit on 16 or less."
  39. cp 13, "Blackjack pays 1.5 X the bet."
  40. cp 14, "Double down option available when you get 2 cards."
  41. cp 15, "It Doubles the bet and you get one more card."
  42.  
  43. LOCATE 17, 38: INPUT ""; Players(Player).setBet
  44.     IF Players(Player).setBet = 0 THEN
  45.         CLS
  46.         cp 10, "You have" + STR$(Players(Player).chips) + "."
  47.         LOCATE 11, 25: INPUT "(0 quits) Enter your bet > ", Players(Player).bet
  48.         IF Players(Player).bet = 0 THEN EXIT DO
  49.         IF Players(Player).bet >= Players(Player).chips THEN Players(Player).bet = Players(Player).chips
  50.     ELSE
  51.         IF Players(Player).chips < Players(Player).setBet THEN Players(Player).bet = Players(Player).chips ELSE Players(Player).bet = Players(Player).setBet
  52.     END IF
  53.     clearPlayers 'clears screen too
  54.     cp 4, "BLACKJACK    chips:" + STR$(Players(Player).chips) + "   betting:" + STR$(Players(Player).bet) + " chips."
  55.     FOR i = 52 TO 2 STEP -1 'shuffle
  56.         r = INT(RND * i) + 1
  57.         SWAP deck$(r), deck$(i)
  58.     NEXT
  59.     deckIndex = 0
  60.     FOR i = 1 TO 2 'each Player is dealt 2 cards
  61.         PlayerAddCard Player
  62.         IF i = 1 THEN cp 10, playerShow$(Player)
  63.         _DELAY 2
  64.         PlayerAddCard Dealer
  65.         IF i = 1 THEN cp 7, playerShow$(Dealer)
  66.         _DELAY 2
  67.     NEXT
  68.     cp 10, playerShow$(Player)
  69.     _DELAY 2
  70.     cp 7, Players(Dealer).ID + ": " + MID$(Players(Dealer).Hand, 1, 1) + " ?  Total = ??"
  71.     _DELAY 2
  72.     '   BJ  debug players having BJ
  73.     'Players(Dealer).Total = 21
  74.     'Players(Player).Total = 21
  75.     IF Players(Player).Total = 21 AND Players(Dealer).Total = 21 THEN
  76.         cp 7, playerShow$(Dealer) + " Blackjack!"
  77.         cp 10, playerShow$(Player) + " Blackjack!"
  78.         cp 13, "Push"
  79.         GOTO BJskip
  80.     ELSEIF Players(Player).Total = 21 THEN
  81.         cp 10, playerShow$(Player) + " Blackjack!"
  82.         Players(Player).chips = Players(Player).chips + 1.5 * Players(Player).bet
  83.         Players(Dealer).chips = Players(Dealer).chips - 1.5 * Players(Player).bet
  84.         cp 13, "You won" + STR$(2 * Players(Player).bet)
  85.         GOTO BJskip
  86.     ELSEIF Players(Dealer).Total = 21 THEN
  87.         cp 7, playerShow$(Dealer) + " Blackjack!"
  88.         Players(Player).chips = Players(Player).chips - Players(Player).bet
  89.         Players(Dealer).chips = Players(Dealer).chips + Players(Player).bet
  90.         cp 13, "You lost" + STR$(Players(Player).bet)
  91.         _DELAY 2
  92.         GOTO BJskip
  93.     END IF
  94.     WHILE Players(Player).Total < 21
  95.         IF LEN(Players(Player).Hand) = 2 THEN
  96.             cp 15, "Press h for Hit,   d for Double Down,   or any other to stay..."
  97.         ELSE
  98.             cp 15, "Press h for Hit, any other to stay..."
  99.         END IF
  100.         card$ = ""
  101.         WHILE LEN(card$) = 0: card$ = INKEY$: _LIMIT 60: WEND
  102.         cp 15, SPACE$(70) 'erase line
  103.         IF card$ = "h" THEN
  104.             PlayerAddCard Player
  105.             cp 11, SPACE$(50)
  106.             cp 10, playerShow$(Player)
  107.             _DELAY 2
  108.             IF Players(Player).Total > 21 THEN GOTO finalReckoning 'busted! skip dealer expose 2nd and play
  109.         ELSEIF card$ = "d" AND LEN(Players(Player).Hand) = 2 THEN
  110.             Players(Player).bet = 2 * Players(Player).bet
  111.             PlayerAddCard Player
  112.             cp 11, SPACE$(50)
  113.             cp 10, playerShow$(Player)
  114.             _DELAY 2
  115.             IF Players(Player).Total > 21 THEN GOTO finalReckoning 'bustest! skip dealer expose 2nd and play
  116.             EXIT WHILE
  117.         ELSE 'stayed
  118.             cp 11, SPACE$(50)
  119.             EXIT WHILE
  120.         END IF
  121.     WEND
  122.     cp 7, playerShow$(Dealer)
  123.     _DELAY 1
  124.     WHILE Players(Player).Total < 22 AND Players(Dealer).Total < 17
  125.         cp 8, "Dealer takes a card."
  126.         _DELAY 2
  127.         PlayerAddCard Dealer
  128.         cp 7, playerShow$(Dealer)
  129.         cp 8, SPACE$(50)
  130.         _DELAY 2
  131.     WEND
  132.     finalReckoning:
  133.     IF Players(Player).Total > 21 OR (Players(Player).Total < Players(Dealer).Total AND Players(Dealer).Total < 22) THEN
  134.         cp 13, "You lose" + STR$(Players(Player).bet)
  135.         Players(Player).chips = Players(Player).chips - Players(Player).bet
  136.         Players(Dealer).chips = Players(Dealer).chips + Players(Player).bet
  137.     ELSEIF Players(Player).Total = Players(Dealer).Total THEN
  138.         cp 13, "Push"
  139.     ELSE
  140.         cp 13, "You win!" + STR$(Players(Player).bet)
  141.         Players(Player).chips = Players(Player).chips + Players(Player).bet
  142.         Players(Dealer).chips = Players(Dealer).chips - Players(Player).bet
  143.     END IF
  144.  
  145.     BJskip:
  146.     IF Players(Player).chips = 0 THEN cp 15, "Out of chips!"
  147.     _DELAY 5
  148. LOOP UNTIL Players(Player).chips = 0
  149. cp 17, "Goodbye"
  150.  
  151. SUB clearPlayers
  152.     DIM i
  153.     CLS
  154.     FOR i = 1 TO 2
  155.         Players(i).Hand = ""
  156.         Players(i).Ace = 0
  157.         Players(i).Total = 0
  158.     NEXT
  159.  
  160. SUB PlayerAddCard (receiver) 'updates player's hand and total
  161.     DIM i AS INTEGER, cv AS INTEGER
  162.     deckIndex = deckIndex + 1
  163.     Players(receiver).Hand = Players(receiver).Hand + deck$(deckIndex)
  164.     IF deck$(deckIndex) = "A" THEN Players(receiver).Ace = -1
  165.     Players(receiver).Total = 0
  166.     FOR i = 1 TO LEN(Players(receiver).Hand)
  167.         IF INSTR(rank, MID$(Players(receiver).Hand, i, 1)) > 10 THEN cv = 10 ELSE cv = INSTR(rank, MID$(Players(receiver).Hand, i, 1))
  168.         Players(receiver).Total = Players(receiver).Total + cv
  169.     NEXT
  170.     IF Players(receiver).Total < 12 AND Players(receiver).Ace THEN Players(receiver).Total = Players(receiver).Total + 10
  171.  
  172. FUNCTION playerShow$ (shower) 'creates string with player name: hand Total = xx and "Busted!" if so.
  173.     DIM i AS INTEGER, S$
  174.     S$ = Players(shower).ID + ": "
  175.     FOR i = 1 TO LEN(Players(shower).Hand)
  176.         S$ = S$ + MID$(Players(shower).Hand, i, 1) + " "
  177.     NEXT
  178.     S$ = S$ + "Total =" + STR$(Players(shower).Total)
  179.     IF Players(shower).Total > 21 THEN S$ = S$ + " Busted!"
  180.     playerShow$ = S$
  181.  
  182. SUB cp (row, s AS STRING) 'print centered on row the string
  183.     LOCATE row, 1: PRINT SPACE$(80); 'clear row of text
  184.     LOCATE row, (80 - LEN(s)) / 2: PRINT s;
  185.  

Offline johnno56

  • Forum Resident
  • Posts: 1270
  • Live long and prosper.
    • View Profile
Re: Blackjack
« Reply #196 on: July 05, 2020, 11:43:16 pm »
Aww man... Two hands and I'm cleaned out... lol
Logic is the beginning of wisdom.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Blackjack
« Reply #197 on: July 06, 2020, 12:20:16 am »
I am now phoning it in with AI:
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. '2020-07-05 fix hit (it wasn't brolen), delete line card$ = inkey$ before loop
  4. ' force dealer to hit on 16 stay 17
  5. ' ties are push
  6. ' bring in double down option
  7. ' fix dealer has Blackjack should tell also fix exposing 2nd card
  8. ' work this towards converting to multiple AI players, so players stats are all located in PlayerType
  9. ' 2020-07-06 Installed AI and mods for it
  10.  
  11. _TITLE "Blackjack Best with AI" ' started 2019-06-06, revisit and match functions with B+J Balckjack with audio and graphics.
  12. CONST rank$ = "A23456789XJQK"
  13. CONST Player = 1
  14. CONST Dealer = 2
  15.  
  16. TYPE PlayerType
  17.     ID AS STRING
  18.     Hand AS STRING
  19.     Ace AS INTEGER
  20.     Total AS INTEGER
  21.     chips AS _INTEGER64
  22.     setBet AS _INTEGER64
  23.     bet AS _INTEGER64
  24.  
  25. DIM SHARED deck$(1 TO 52), Players(1 TO 2) AS PlayerType, deckIndex, round
  26.  
  27. DIM i, r, card$
  28. Players(Player).ID = "Player"
  29. Players(Dealer).ID = "Dealer"
  30. FOR i = 1 TO 52
  31.     deck$(i) = MID$(rank$ + rank$ + rank$ + rank$, i, 1)
  32. Players(Dealer).chips = -100
  33. Players(Player).chips = 100
  34. cp 8, "For this Blackjack Game, you start with 100 chips"
  35. cp 9, "and can bet any amount of them for each game."
  36. cp 10, "If you'd like to set your bet for each game now"
  37. cp 11, "enter that amount otherwise enter 0, AI is setting at 2."
  38. cp 12, "Dealer must hit on 16 or less."
  39. cp 13, "Blackjack pays 1.5 X the bet."
  40. cp 14, "Double down option available when you get 2 cards."
  41. cp 15, "It Doubles the bet and you get one more card."
  42. cp 17, "press any to continue..."
  43. Players(Player).setBet = 2
  44. 'LOCATE 17, 38: INPUT ""; Players(Player).setBet
  45.     IF Players(Player).setBet = 0 THEN
  46.         CLS
  47.         cp 10, "You have" + STR$(Players(Player).chips) + "."
  48.         LOCATE 11, 25: INPUT "(0 quits) Enter your bet > ", Players(Player).bet
  49.         IF Players(Player).bet = 0 THEN EXIT DO
  50.         IF Players(Player).bet >= Players(Player).chips THEN Players(Player).bet = Players(Player).chips
  51.     ELSE
  52.         IF Players(Player).chips < Players(Player).setBet THEN Players(Player).bet = Players(Player).chips ELSE Players(Player).bet = Players(Player).setBet
  53.     END IF
  54.     clearPlayers 'clears screen too
  55.     cp 4, "BLACKJACK    chips:" + STR$(Players(Player).chips) + "   betting:" + STR$(Players(Player).bet) + " chips."
  56.     FOR i = 52 TO 2 STEP -1 'shuffle
  57.         r = INT(RND * i) + 1
  58.         SWAP deck$(r), deck$(i)
  59.     NEXT
  60.     deckIndex = 0
  61.     FOR i = 1 TO 2 'each Player is dealt 2 cards
  62.         PlayerAddCard Player
  63.         IF i = 1 THEN cp 10, playerShow$(Player)
  64.         _DELAY 1
  65.         PlayerAddCard Dealer
  66.         IF i = 1 THEN cp 7, playerShow$(Dealer)
  67.         _DELAY 1
  68.     NEXT
  69.     cp 10, playerShow$(Player)
  70.     _DELAY 1
  71.     cp 7, Players(Dealer).ID + ": " + MID$(Players(Dealer).Hand, 1, 1) + " ?  Total = ??"
  72.     _DELAY 1
  73.     '   BJ  debug players having BJ
  74.     'Players(Dealer).Total = 21
  75.     'Players(Player).Total = 21
  76.     IF Players(Player).Total = 21 AND Players(Dealer).Total = 21 THEN
  77.         cp 7, playerShow$(Dealer) + " Blackjack!"
  78.         cp 10, playerShow$(Player) + " Blackjack!"
  79.         cp 13, "Push"
  80.         GOTO BJskip
  81.     ELSEIF Players(Player).Total = 21 THEN
  82.         cp 10, playerShow$(Player) + " Blackjack!"
  83.         Players(Player).chips = Players(Player).chips + 1.5 * Players(Player).bet
  84.         Players(Dealer).chips = Players(Dealer).chips - 1.5 * Players(Player).bet
  85.         cp 13, "You won" + STR$(2 * Players(Player).bet)
  86.         GOTO BJskip
  87.     ELSEIF Players(Dealer).Total = 21 THEN
  88.         cp 7, playerShow$(Dealer) + " Blackjack!"
  89.         Players(Player).chips = Players(Player).chips - Players(Player).bet
  90.         Players(Dealer).chips = Players(Dealer).chips + Players(Player).bet
  91.         cp 13, "You lost" + STR$(Players(Player).bet)
  92.         _DELAY 1
  93.         GOTO BJskip
  94.     END IF
  95.     WHILE Players(Player).Total < 21
  96.         IF LEN(Players(Player).Hand) = 2 THEN
  97.             cp 15, "Press h for Hit,   d for Double Down,   or any other to stay..."
  98.         ELSE
  99.             cp 15, "Press h for Hit, any other to stay..."
  100.         END IF
  101.         'card$ = ""
  102.         'WHILE LEN(card$) = 0: card$ = INKEY$: _LIMIT 60: WEND   ' bplus calling it in now!
  103.         card$ = bplusAI$
  104.         IF card$ = "h" THEN cp 17, "Hit"
  105.         IF card$ = "d" THEN cp 17, "Double Down"
  106.         IF card$ <> "h" AND card$ <> "d" THEN cp 17, "Stay"
  107.         cp 15, SPACE$(70) 'erase line
  108.         _DELAY 2
  109.         cp 17, SPACE$(50)
  110.         IF card$ = "h" THEN
  111.             PlayerAddCard Player
  112.             'cp 11, SPACE$(50)
  113.             cp 10, playerShow$(Player)
  114.             IF Players(Player).Total > 21 THEN GOTO finalReckoning 'busted! skip dealer expose 2nd and play
  115.         ELSEIF card$ = "d" AND LEN(Players(Player).Hand) = 2 THEN
  116.             Players(Player).bet = 2 * Players(Player).bet
  117.             PlayerAddCard Player
  118.             'cp 11, SPACE$(50)
  119.             cp 10, playerShow$(Player)
  120.             IF Players(Player).Total > 21 THEN GOTO finalReckoning 'bustest! skip dealer expose 2nd and play
  121.             EXIT WHILE
  122.         ELSE 'stayed
  123.             ' cp 11, SPACE$(50)
  124.             EXIT WHILE
  125.         END IF
  126.     WEND
  127.     cp 7, playerShow$(Dealer)
  128.     _DELAY 1
  129.     WHILE Players(Player).Total < 22 AND Players(Dealer).Total < 17
  130.         cp 8, "Dealer takes a card."
  131.         _DELAY 1
  132.         PlayerAddCard Dealer
  133.         cp 7, playerShow$(Dealer)
  134.         cp 8, SPACE$(50)
  135.         _DELAY 1
  136.     WEND
  137.     finalReckoning:
  138.     IF Players(Player).Total > 21 OR (Players(Player).Total < Players(Dealer).Total AND Players(Dealer).Total < 22) THEN
  139.         cp 13, "You lose" + STR$(Players(Player).bet)
  140.         Players(Player).chips = Players(Player).chips - Players(Player).bet
  141.         Players(Dealer).chips = Players(Dealer).chips + Players(Player).bet
  142.     ELSEIF Players(Player).Total = Players(Dealer).Total THEN
  143.         cp 13, "Push"
  144.     ELSE
  145.         cp 13, "You win!" + STR$(Players(Player).bet)
  146.         Players(Player).chips = Players(Player).chips + Players(Player).bet
  147.         Players(Dealer).chips = Players(Dealer).chips - Players(Player).bet
  148.     END IF
  149.  
  150.     BJskip:
  151.     IF Players(Player).chips = 0 THEN cp 15, "Out of chips!"
  152.     _DELAY 2
  153. LOOP UNTIL Players(Player).chips = 0
  154. cp 17, "Goodbye"
  155.  
  156. SUB clearPlayers
  157.     DIM i
  158.     CLS
  159.     round = round + 1
  160.     cp 2, "Round:" + STR$(round)
  161.     FOR i = 1 TO 2
  162.         Players(i).Hand = ""
  163.         Players(i).Ace = 0
  164.         Players(i).Total = 0
  165.     NEXT
  166.  
  167. FUNCTION bplusAI$
  168.     SELECT CASE Players(Player).Total
  169.         CASE IS < 10: bplusAI$ = "h" 'no caps!
  170.         CASE 10, 11
  171.             IF LEN(Players(Player).Hand) = 2 THEN bplusAI$ = "d" ELSE bplusAI$ = "h"
  172.         CASE IS < 15
  173.             IF RND < .5 THEN bplusAI$ = "h" ELSE bplusAI$ = "whatever"
  174.         CASE ELSE: bplusAI$ = "Show me the money!"
  175.     END SELECT
  176.  
  177. SUB PlayerAddCard (receiver) 'updates player's hand and total
  178.     DIM i AS INTEGER, cv AS INTEGER
  179.     deckIndex = deckIndex + 1
  180.     Players(receiver).Hand = Players(receiver).Hand + deck$(deckIndex)
  181.     IF deck$(deckIndex) = "A" THEN Players(receiver).Ace = -1
  182.     Players(receiver).Total = 0
  183.     FOR i = 1 TO LEN(Players(receiver).Hand)
  184.         IF INSTR(rank, MID$(Players(receiver).Hand, i, 1)) > 10 THEN cv = 10 ELSE cv = INSTR(rank, MID$(Players(receiver).Hand, i, 1))
  185.         Players(receiver).Total = Players(receiver).Total + cv
  186.     NEXT
  187.     IF Players(receiver).Total < 12 AND Players(receiver).Ace THEN Players(receiver).Total = Players(receiver).Total + 10
  188.  
  189. FUNCTION playerShow$ (shower) 'creates string with player name: hand Total = xx and "Busted!" if so.
  190.     DIM i AS INTEGER, S$
  191.     S$ = Players(shower).ID + ": "
  192.     FOR i = 1 TO LEN(Players(shower).Hand)
  193.         S$ = S$ + MID$(Players(shower).Hand, i, 1) + " "
  194.     NEXT
  195.     S$ = S$ + "Total =" + STR$(Players(shower).Total)
  196.     IF Players(shower).Total > 21 THEN S$ = S$ + " Busted!"
  197.     playerShow$ = S$
  198.  
  199. SUB cp (row, s AS STRING) 'print centered on row the string
  200.     LOCATE row, 1: PRINT SPACE$(80); 'clear row of text
  201.     LOCATE row, (80 - LEN(s)) / 2: PRINT s;
  202.  

Started at 100 chips and always 2 chip bets. Currently: dang! I lost the screen at 60 Rounds I was ahead!

Nuts 2nd Session and I  am down a bits...
with AI.PNG
* with AI.PNG (Filesize: 4.89 KB, Dimensions: 644x299, Views: 216)
« Last Edit: July 06, 2020, 12:25:20 am by bplus »

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Blackjack
« Reply #198 on: July 06, 2020, 01:13:56 am »
Session 3 is hanging in there.

Made a compact version for when watching YouTube.
withAI 2.PNG
* withAI 2.PNG (Filesize: 30.51 KB, Dimensions: 999x441, Views: 227)

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Blackjack
« Reply #199 on: July 06, 2020, 11:09:13 am »
There is something spooky between running the code in SCREEN 0 (just default) and using tiny graphics screen.

True I have only tested a long run once but the SCREEN 0 code holds it's own for a long time, watching it play I can't tell if if will loose in long run.

Running the code in a small graphics screen and it is easy to see the AI is loser and can't last the night.

And that's what happened.


I am getting superstitious! Today
 
Yep I am getting superstitious.PNG


BTW you start with 100 chips, plus or minus from that is how to judge progress in the session.


To all,

I am writing a program to test several AI's together against Dealer, if you'd like to be included I just need your AI$ sub routine:

my first as example:
Code: QB64: [Select]
  1. FUNCTION bplusAI$
  2.     SELECT CASE Players(Player).Total
  3.         CASE IS < 10: bplusAI$ = "h" 'no caps!
  4.         CASE 10, 11
  5.             IF LEN(Players(Player).Hand) = 2 THEN bplusAI$ = "d" ELSE bplusAI$ = "h"
  6.         CASE IS < 15
  7.             IF RND < .5 THEN bplusAI$ = "h" ELSE bplusAI$ = "whatever"
  8.         CASE ELSE: bplusAI$ = "Show me the money!"
  9.     END SELECT

Each player has a name or number in the Players() array to track their cards, total, setbet, current bet... so Players(player).Total is your current total for the hand you hold. Just use Players(Player).Total

You can check the Dealer's first card with something like DealerFirst$ =  LEFT$(Players(Dealer).hand, 1) - not allowed to peak at 2nd card. If you are holding an Ace there is a flag for that and be careful how Total is calculated (it assumes the first ace is 11 unless total goes over 21).

So your AI$ function has to return an "h" to hit, an "d" to double down when you have 2 cards that option is open, anything else is equivalent to Stay.


yep opposite from last night!
 
yep opposite of last nigh.PNG
* yep opposite of last nigh.PNG (Filesize: 13.87 KB, Dimensions: 544x604, Views: 232)
« Last Edit: July 06, 2020, 11:34:42 am by bplus »

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Blackjack
« Reply #200 on: July 06, 2020, 02:25:45 pm »
Here is my compact AI tester with my 2nd AI, a mod of first that decreases probability to hit between 12 and 14 and refusing to Double Down if Dealer shows an Ace:
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. '2020-07-05 fix hit (it wasn't brolen), delete line card$ = inkey$ before loop
  4. ' force dealer to hit on 16 stay 17
  5. ' ties are push
  6. ' bring in double down option
  7. ' fix dealer has Blackjack should tell also fix exposing 2nd card
  8. ' work this towards converting to multiple AI players, so players stats are all located in PlayerType
  9. ' 2020-07-06 Installed AI and mods for it
  10. ' 2020-07-06 new Compact Tester with 2nd AI
  11.  
  12. _TITLE "Blackjack AI Tester" ' started 2019-06-06, revisit and match functions with B+J Balckjack with audio and graphics.
  13. CONST rank$ = "A23456789XJQK"
  14. CONST Player = 1
  15. CONST Dealer = 2
  16.  
  17. TYPE PlayerType
  18.     ID AS STRING
  19.     Hand AS STRING
  20.     Ace AS INTEGER
  21.     Total AS INTEGER
  22.     chips AS _INTEGER64
  23.     setBet AS _INTEGER64
  24.     bet AS _INTEGER64
  25.  
  26. DIM SHARED deck$(1 TO 52), Players(1 TO 2) AS PlayerType, deckIndex, round
  27. SCREEN _NEWIMAGE(400, 300, 32)
  28. DIM i, r, card$
  29. Players(Player).ID = "Player"
  30. Players(Dealer).ID = "Dealer"
  31. FOR i = 1 TO 52
  32.     deck$(i) = MID$(rank$ + rank$ + rank$ + rank$, i, 1)
  33. Players(Dealer).chips = -100
  34. Players(Player).chips = 100
  35. 'bbbbb"1234567890123456789012345678901234567890
  36. cp 4, "*** BJ Compact AI Tester ***"
  37. cp 6, "You start with 100 chips."
  38. cp 7, "AI sets bet amount to 2."
  39. cp 8, "Dealer must hit on 16 or less."
  40. cp 9, "Blackjack pays 1.5 X the bet."
  41. cp 10, "Double down option available"
  42. cp 11, "when you get 2 cards."
  43. cp 12, "It Doubles the bet and you get one"
  44. cp 13, "more card."
  45. cp 17, "press any to continue..."
  46. Players(Player).setBet = 2
  47. 'LOCATE 17, 38: INPUT ""; Players(Player).setBet
  48.     IF Players(Player).setBet = 0 THEN
  49.         CLS
  50.         cp 10, "You have" + STR$(Players(Player).chips) + "."
  51.         LOCATE 11, 25: INPUT "(0 quits) Enter your bet > ", Players(Player).bet
  52.         IF Players(Player).bet = 0 THEN EXIT DO
  53.         IF Players(Player).bet >= Players(Player).chips THEN Players(Player).bet = Players(Player).chips
  54.     ELSE
  55.         IF Players(Player).chips < Players(Player).setBet THEN Players(Player).bet = Players(Player).chips ELSE Players(Player).bet = Players(Player).setBet
  56.     END IF
  57.     clearPlayers 'clears screen too
  58.     cp 4, "Chips:" + STR$(Players(Player).chips) + "   betting:" + STR$(Players(Player).bet) + " chips."
  59.     FOR i = 52 TO 2 STEP -1 'shuffle
  60.         r = INT(RND * i) + 1
  61.         SWAP deck$(r), deck$(i)
  62.     NEXT
  63.     deckIndex = 0
  64.     FOR i = 1 TO 2 'each Player is dealt 2 cards
  65.         PlayerAddCard Player
  66.         IF i = 1 THEN cp 10, playerShow$(Player)
  67.         _DELAY 1
  68.         PlayerAddCard Dealer
  69.         IF i = 1 THEN cp 7, playerShow$(Dealer)
  70.         _DELAY 1
  71.     NEXT
  72.     cp 10, playerShow$(Player)
  73.     _DELAY 1
  74.     cp 7, Players(Dealer).ID + ": " + MID$(Players(Dealer).Hand, 1, 1) + " ?  Total = ??"
  75.     _DELAY 1
  76.     '   BJ  debug players having BJ
  77.     'Players(Dealer).Total = 21
  78.     'Players(Player).Total = 21
  79.     IF Players(Player).Total = 21 AND Players(Dealer).Total = 21 THEN
  80.         cp 7, playerShow$(Dealer) + " Blackjack!"
  81.         cp 10, playerShow$(Player) + " Blackjack!"
  82.         cp 13, "Push"
  83.         GOTO BJskip
  84.     ELSEIF Players(Player).Total = 21 THEN
  85.         cp 10, playerShow$(Player) + " Blackjack!"
  86.         Players(Player).chips = Players(Player).chips + 1.5 * Players(Player).bet
  87.         Players(Dealer).chips = Players(Dealer).chips - 1.5 * Players(Player).bet
  88.         cp 13, "You won" + STR$(2 * Players(Player).bet)
  89.         GOTO BJskip
  90.     ELSEIF Players(Dealer).Total = 21 THEN
  91.         cp 7, playerShow$(Dealer) + " Blackjack!"
  92.         Players(Player).chips = Players(Player).chips - Players(Player).bet
  93.         Players(Dealer).chips = Players(Dealer).chips + Players(Player).bet
  94.         cp 13, "You lost" + STR$(Players(Player).bet)
  95.         _DELAY 1
  96.         GOTO BJskip
  97.     END IF
  98.     WHILE Players(Player).Total < 21
  99.         IF LEN(Players(Player).Hand) = 2 THEN
  100.             cp 15, "Hit Double or Stay"
  101.         ELSE
  102.             cp 15, "Hit or Stay"
  103.         END IF
  104.         'card$ = ""
  105.         'WHILE LEN(card$) = 0: card$ = INKEY$: _LIMIT 60: WEND   ' bplus calling it in now!
  106.         card$ = bplusAI$
  107.         IF card$ = "h" THEN cp 17, "Hit"
  108.         IF card$ = "d" THEN cp 17, "Double Down"
  109.         IF card$ <> "h" AND card$ <> "d" THEN cp 17, "Stay"
  110.         cp 15, SPACE$(39) 'erase line
  111.         _DELAY 2
  112.         cp 17, SPACE$(39)
  113.         IF card$ = "h" THEN
  114.             PlayerAddCard Player
  115.             'cp 11, SPACE$(50)
  116.             cp 10, playerShow$(Player)
  117.             IF Players(Player).Total > 21 THEN GOTO finalReckoning 'busted! skip dealer expose 2nd and play
  118.         ELSEIF card$ = "d" AND LEN(Players(Player).Hand) = 2 THEN
  119.             Players(Player).bet = 2 * Players(Player).bet
  120.             PlayerAddCard Player
  121.             'cp 11, SPACE$(50)
  122.             cp 10, playerShow$(Player)
  123.             IF Players(Player).Total > 21 THEN GOTO finalReckoning 'bustest! skip dealer expose 2nd and play
  124.             EXIT WHILE
  125.         ELSE 'stayed
  126.             ' cp 11, SPACE$(50)
  127.             EXIT WHILE
  128.         END IF
  129.     WEND
  130.     cp 7, playerShow$(Dealer)
  131.     _DELAY 1
  132.     WHILE Players(Player).Total < 22 AND Players(Dealer).Total < 17
  133.         cp 8, "Dealer takes a card."
  134.         _DELAY 1
  135.         PlayerAddCard Dealer
  136.         cp 7, playerShow$(Dealer)
  137.         cp 8, SPACE$(39)
  138.         _DELAY 1
  139.     WEND
  140.     finalReckoning:
  141.     IF Players(Player).Total > 21 OR (Players(Player).Total < Players(Dealer).Total AND Players(Dealer).Total < 22) THEN
  142.         cp 13, "You lose" + STR$(Players(Player).bet)
  143.         Players(Player).chips = Players(Player).chips - Players(Player).bet
  144.         Players(Dealer).chips = Players(Dealer).chips + Players(Player).bet
  145.     ELSEIF Players(Player).Total = Players(Dealer).Total THEN
  146.         cp 13, "Push"
  147.     ELSE
  148.         cp 13, "You win!" + STR$(Players(Player).bet)
  149.         Players(Player).chips = Players(Player).chips + Players(Player).bet
  150.         Players(Dealer).chips = Players(Dealer).chips - Players(Player).bet
  151.     END IF
  152.  
  153.     BJskip:
  154.     IF Players(Player).chips = 0 THEN cp 15, "Out of chips!"
  155.     _DELAY 2
  156. LOOP UNTIL Players(Player).chips = 0
  157. cp 17, "Goodbye"
  158.  
  159. SUB clearPlayers
  160.     DIM i
  161.     CLS
  162.     round = round + 1
  163.     cp 2, "Round:" + STR$(round)
  164.     FOR i = 1 TO 2
  165.         Players(i).Hand = ""
  166.         Players(i).Ace = 0
  167.         Players(i).Total = 0
  168.     NEXT
  169.  
  170. 'FUNCTION bplusAI$     ' first try
  171. '    SELECT CASE Players(Player).Total
  172. '        CASE IS < 10: bplusAI$ = "h" 'no caps!
  173. '        CASE 10, 11
  174. '            IF LEN(Players(Player).Hand) = 2 THEN bplusAI$ = "d" ELSE bplusAI$ = "h"
  175. '        CASE IS < 15
  176. '            IF RND < .5 THEN bplusAI$ = "h" ELSE bplusAI$ = "whatever"
  177. '        CASE ELSE: bplusAI$ = "Show me the money!"
  178. '    END SELECT
  179. 'END FUNCTION
  180.  
  181. FUNCTION bplusAI$ 'after trying first want to try this make it more likely to hit on 12 than 13, 14...
  182.     SELECT CASE Players(Player).Total
  183.         CASE IS < 9: bplusAI$ = "h" 'no caps!
  184.         CASE 9, 10, 11 'double down unless dealer showing an Ace which is like insurance for dealer
  185.             IF LEN(Players(Player).Hand) = 2 AND LEFT$(Players(Dealer).Hand, 1) <> "A" THEN bplusAI$ = "d" ELSE bplusAI$ = "h"
  186.         CASE 12: IF RND < .75 THEN bplusAI$ = "h" ELSE bplusAI$ = "whatever"
  187.         CASE 13: IF RND < .5 THEN bplusAI$ = "h" ELSE bplusAI$ = "whatever"
  188.         CASE 14: IF RND < .25 THEN bplusAI$ = "h" ELSE bplusAI$ = "whatever"
  189.         CASE ELSE: bplusAI$ = "Show me the money!"
  190.     END SELECT
  191.  
  192. SUB PlayerAddCard (receiver) 'updates player's hand and total
  193.     DIM i AS INTEGER, cv AS INTEGER
  194.     deckIndex = deckIndex + 1
  195.     Players(receiver).Hand = Players(receiver).Hand + deck$(deckIndex)
  196.     IF deck$(deckIndex) = "A" THEN Players(receiver).Ace = -1
  197.     Players(receiver).Total = 0
  198.     FOR i = 1 TO LEN(Players(receiver).Hand)
  199.         IF INSTR(rank, MID$(Players(receiver).Hand, i, 1)) > 10 THEN cv = 10 ELSE cv = INSTR(rank, MID$(Players(receiver).Hand, i, 1))
  200.         Players(receiver).Total = Players(receiver).Total + cv
  201.     NEXT
  202.     IF Players(receiver).Total < 12 AND Players(receiver).Ace THEN Players(receiver).Total = Players(receiver).Total + 10
  203.  
  204. FUNCTION playerShow$ (shower) 'creates string with player name: hand Total = xx and "Busted!" if so.
  205.     DIM i AS INTEGER, S$
  206.     S$ = Players(shower).ID + ": "
  207.     FOR i = 1 TO LEN(Players(shower).Hand)
  208.         S$ = S$ + MID$(Players(shower).Hand, i, 1) + " "
  209.     NEXT
  210.     S$ = S$ + "Total =" + STR$(Players(shower).Total)
  211.     IF Players(shower).Total > 21 THEN S$ = S$ + " Bust"
  212.     playerShow$ = S$
  213.  
  214. SUB cp (row, s AS STRING) 'print centered on row the string
  215.     LOCATE row, 1: PRINT SPACE$(50); 'clear row of text
  216.     LOCATE row, (50 - LEN(s)) / 2: PRINT s;
  217.  

Update 250 Rounds one of 4 still winning:


Final Results, Rounds lasted: 327, 410, 649, 899.
250 rounds 1 of 4 winning.PNG
* 250 rounds 1 of 4 winning.PNG (Filesize: 20.96 KB, Dimensions: 799x651, Views: 171)
« Last Edit: July 06, 2020, 05:50:14 pm by bplus »

Offline johnno56

  • Forum Resident
  • Posts: 1270
  • Live long and prosper.
    • View Profile
Re: Blackjack
« Reply #201 on: July 06, 2020, 06:48:30 pm »
Umm... 252 rounds? Can I assume that one round is equivalent to one hand? If so, 'that' is a lot of card games... With such devotion to a specific task, may I suggest either a pastime of jigsaws or golf ball dimple counting?  I could probably attempt 50 rounds, but unfortunately, I do not have enough coffee stockpiled to get me through it... lol

With your coloured version, may I suggest a background of rgb(31,45,80) (a kind of slate-blue) with either white or light blue text? Pure blue, for me anyway, is quite bright on 'pre-coffee' eyes... lol

I would be happy to help but I'm not that familiar with coding AI. Crumbs, I have enough trouble with my 'I'... lol
Logic is the beginning of wisdom.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Blackjack
« Reply #202 on: July 06, 2020, 07:13:09 pm »
Hi @johnno56

Here is my first AI, all I need to know is when to hit = "h", when to double down (after first 2 cards) = "d" else the code assumes you want to stay = CASE ELSE neither "h" or "d"

Players(Player).Total = current card total in your hand
IF LEFT$(Players(Dealer).Cards, 1) = "A" THEN ' Dealer is showing an Ace
IF LEN(Players(Player).Hand) = 2 THEN ' player only has 2 cards so Double Down Option is available

Code: QB64: [Select]
  1. 'FUNCTION bplusAI$     ' first try
  2. '    SELECT CASE Players(Player).Total
  3. '        CASE IS < 10: bplusAI$ = "h" 'no caps!  == If my Total < 10 the hit me!
  4. '        CASE 10, 11 '=================== If my Total is 10 or 11 then Double down if I have only 2 cards else hit me!
  5. '            IF LEN(Players(Player).Hand) = 2 THEN bplusAI$ = "d" ELSE bplusAI$ = "h"
  6. '        CASE IS < 15 ' ================== If my Total < 15 toss a coin to hit or stay
  7. '            IF RND < .5 THEN bplusAI$ = "h" ELSE bplusAI$ = "whatever"
  8. '        CASE ELSE: bplusAI$ = "Show me the money!"   "If my Total > 14 just Stay! maybe the Dealer will Bust
  9. '    END SELECT
  10. 'END FUNCTION
  11.  
« Last Edit: July 06, 2020, 08:28:07 pm by bplus »

Offline johnno56

  • Forum Resident
  • Posts: 1270
  • Live long and prosper.
    • View Profile
Re: Blackjack
« Reply #203 on: July 06, 2020, 08:25:38 pm »
I am no card player, but if it was me and I could see the dealers' card is less than say 6, I would stay on 15 because the odds are good that the dealers' next card would be less than 9. If the dealer's card is closer to 9 then I would probably reconsider staying. eg: If the dealer's first card is 6, then yes, stay. If 7 or 8, may be not stay - toss coin 50/50. If 9 or more, definitely not stay - hit.

But that's me...  I hope that I have explained all that properly?
Logic is the beginning of wisdom.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Blackjack
« Reply #204 on: July 06, 2020, 08:33:30 pm »
OK I can code that for you. Do you ever try Double Down?

Also do you know Aces are little insurance agents? That is the first one is taken as 11 until or unless you go over 21 then it's 1 and you get a 2nd chance with another hit.

So Double Down when you have an Ace might be a waste of it and not wise if Dealer shows an Ace.

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Blackjack
« Reply #205 on: July 06, 2020, 08:43:21 pm »
I'd go with some simple kid style logic for a random AI:  "If the dealer stays at 17, then as long as I stay at 18+, I have a good chance to beat them."
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline johnno56

  • Forum Resident
  • Posts: 1270
  • Live long and prosper.
    • View Profile
Re: Blackjack
« Reply #206 on: July 06, 2020, 09:48:18 pm »
I think Steve may be right, in respect to keeping it simple. My idea may not be the best to use... lol  I have used DD in the past. Usually if both cards add up to either 10 or 11. That didn't happen all that often, but when it did, DD was the way to go...
Logic is the beginning of wisdom.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Blackjack
« Reply #207 on: July 07, 2020, 02:21:37 am »
Thanks for input on AI guys! For Steve I used the absolutely simplest thing I could think of. Ha! it did pretty good on first round of tests.

AI multi Tester:
Code: QB64: [Select]
  1. ' No suits shown for cards A is Ace, J, Q, K are Jack, Queen, King, X is for 10
  2. '2020-07-05 fix hit (it wasn't broken), delete line card$ = inkey$ before loop
  3. ' force dealer to hit on 16 stay 17
  4. ' ties are push
  5. ' bring in double down option
  6. ' fix dealer has Blackjack should tell also fix exposing 2nd card
  7. ' work this towards converting to multiple AI players, so players stats are all located in PlayerType
  8. ' 2020-07-06 Installed AI and mods for it
  9. ' 2020-07-06 new Compact Tester with 2nd AI
  10. ' 2020-07-06 start Blackjack Multi AI Tester off of Compact Tester
  11. '            add in allot of code worked out in Blackjack with Bots 2019-06-07
  12. '            Yes, major overhaul of last year bot code which was/is neat!
  13. '            Thanks Johnno and Steve for some interesting AI ideas to try!
  14.  
  15.  
  16. DEFINT A-Z
  17.  
  18. _TITLE "Blackjack Multi AI Tester" ' started 2019-06-06, revisit and match functions with B+J Balckjack with audio and graphics.
  19.  
  20. CONST nPlayers = 5 'dealer counts as the last player   this number depends on how many AI's to test
  21. CONST xmax = 800, ymax = 500, margin = 4
  22. CONST rank$ = "A23456789XJQK" 'for building deck and figuring Totals
  23. CONST bH = 14, bW = 20 ' box height and box width in rows and columns
  24. CONST white = &HFFFFFFFF, black = &HFF000000, bc = &HFF008822, fc = &HFFCCCCFF
  25.  
  26. TYPE PlayerType
  27.     ID AS STRING '        name of bot including dealer bot
  28.     Col AS INTEGER '      left corner column
  29.     Row AS INTEGER '      top row
  30.     FC AS _UNSIGNED LONG 'player colors are assigned in init FC are alternating Black and White
  31.     BC AS _UNSIGNED LONG 'random light and dark opposite print FC
  32.     Hand AS STRING '      cards are 1 char strings 10 is X
  33.     Ace AS INTEGER '      flag has ace for totalling hand
  34.     Bust AS INTEGER '     flag for final reckoning
  35.     BJ AS INTEGER '       flag for final reckoning
  36.     Total AS INTEGER '    card total of hand
  37.     Chips AS _INTEGER64 ' players status
  38.     SetBet AS _INTEGER64 'regular bet amount
  39.     Bet AS _INTEGER64 '   players bet each round
  40.  
  41. DIM SHARED deck$(1 TO 52), p(1 TO nPlayers) AS PlayerType, deckIndex, round, allOut, player, blockDealer
  42. SCREEN _NEWIMAGE(xmax, ymax, 32)
  43. DIM i, card$
  44. initGame
  45. 'FOR i = 1 TO nPlayers
  46. '    showPlayer i
  47. 'NEXT
  48.     clearPlayers 'clears screen too and shuffles deck
  49.  
  50.     FOR i = 1 TO 2
  51.         FOR player = 1 TO nPlayers 'each Player is dealt 2 cards
  52.             IF player <> nPlayers THEN
  53.                 IF p(player).Chips > 0 THEN
  54.                     allOut = 0 'signal everyone is out of chips is false
  55.                     PlayerAddCard player
  56.                     showPlayer player
  57.                     _DELAY .5
  58.                 END IF
  59.             ELSE
  60.                 IF allOut THEN
  61.                     EXIT DO
  62.                 ELSE
  63.                     PlayerAddCard player
  64.                     showPlayer player
  65.                     _DELAY .5
  66.                 END IF
  67.             END IF
  68.         NEXT
  69.     NEXT
  70.  
  71.     IF p(nPlayers).BJ = 0 THEN 'dealer does not have BJ
  72.         FOR player = 1 TO nPlayers - 1
  73.             showPlayer player
  74.             _DELAY 1
  75.             WHILE p(player).Total < 21
  76.                 SELECT CASE player ' this has to be coded for exactly all nPlayers - 1
  77.                     CASE 1: card$ = Johnno$(player)
  78.                     CASE 2: card$ = Steve$(player)
  79.                     CASE 3: card$ = bplusAI$(player)
  80.                     CASE 4: card$ = bplusAI2$(player)
  81.                 END SELECT
  82.                 IF card$ = "h" THEN cp player, 13, "Hit"
  83.                 IF card$ = "d" THEN cp player, 13, "Double Down"
  84.                 IF card$ <> "h" AND card$ <> "d" THEN cp player, 13, "Stay"
  85.                 _DELAY 2
  86.                 IF card$ = "h" THEN
  87.                     PlayerAddCard player
  88.                     showPlayer player
  89.                     _DELAY 1
  90.                 ELSEIF card$ = "d" AND LEN(p(player).Hand) = 2 THEN
  91.                     p(player).Bet = 2 * p(player).Bet
  92.                     PlayerAddCard player
  93.                     showPlayer player
  94.                     _DELAY 1
  95.                     EXIT WHILE
  96.                 ELSE 'stayed
  97.                     showPlayer player
  98.                     _DELAY 1
  99.                     EXIT WHILE
  100.                 END IF
  101.             WEND
  102.         NEXT
  103.     END IF
  104.  
  105.     blockDealer = 0
  106.     showPlayer nPlayers
  107.     _DELAY 1
  108.     WHILE p(nPlayers).Total < 17
  109.         cp nPlayers, 11, "Dealer takes a card."
  110.         _DELAY 1
  111.         PlayerAddCard nPlayers
  112.         showPlayer nPlayers
  113.         _DELAY 1
  114.     WEND
  115.  
  116.     '    final Reckoning
  117.     FOR player = 1 TO nPlayers - 1
  118.         showPlayer player
  119.         IF p(player).Total > 21 OR (p(player).Total < p(nPlayers).Total AND p(nPlayers).Total < 22) THEN
  120.             cp player, 13, "Lost" + STR$(p(player).Bet)
  121.             p(player).Chips = p(player).Chips - p(player).Bet
  122.             p(nPlayers).Chips = p(nPlayers).Chips + p(player).Bet
  123.         ELSEIF p(player).Total = p(nPlayers).Total AND p(player).BJ = 0 THEN
  124.             cp player, 13, "Push"
  125.         ELSE
  126.             IF p(player).BJ THEN
  127.                 cp player, 13, "Win!" + STR$(p(player).Bet + .5 * p(player).Bet)
  128.                 p(player).Chips = p(player).Chips + p(player).Bet + .5 * p(player).Bet
  129.                 p(nPlayers).Chips = p(nPlayers).Chips - p(player).Bet - .5 * p(player).Bet
  130.             ELSE
  131.                 cp player, 13, "Win!" + STR$(p(player).Bet)
  132.                 p(player).Chips = p(player).Chips + p(player).Bet
  133.                 p(nPlayers).Chips = p(nPlayers).Chips - p(player).Bet
  134.             END IF
  135.         END IF
  136.         IF p(player).Chips = 0 THEN cp player, 14, "Out of chips!"
  137.         _DELAY 3
  138.     NEXT
  139. LOOP UNTIL allOut
  140.  
  141. SUB clearPlayers
  142.     DIM i
  143.     COLOR fc, bc: CLS
  144.     round = round + 1: allOut = -1
  145.     FOR i = 1 TO nPlayers
  146.         p(i).Hand = ""
  147.         p(i).Ace = 0
  148.         p(i).Total = 0
  149.         p(i).BJ = 0
  150.         p(i).Bust = 0
  151.         'because of double down option I have to reset bet during play and set it back at each new round
  152.         'make sure we aren't betting more than our chips count at least to start
  153.         IF p(i).Chips < p(i).SetBet THEN p(i).Bet = p(i).Chips ELSE p(i).Bet = p(i).SetBet
  154.     NEXT
  155.     FOR i = 52 TO 2 STEP -1 'shuffle
  156.         SWAP deck$(INT(RND * i) + 1), deck$(i)
  157.     NEXT
  158.     'deck$(5) = "A": deck$(10) = "J"  'check immediate show of Blackjack for dealer
  159.     deckIndex = 0: blockDealer = -1
  160.  
  161. FUNCTION bplusAI$ (pn) ' first try
  162.     SELECT CASE p(pn).Total
  163.         CASE IS < 10: bplusAI$ = "h" 'no caps!
  164.         CASE 10, 11
  165.             IF LEN(p(pn).Hand) = 2 THEN bplusAI$ = "d" ELSE bplusAI$ = "h"
  166.         CASE IS < 15
  167.             IF RND < .5 THEN bplusAI$ = "h" ELSE bplusAI$ = "whatever"
  168.         CASE ELSE: bplusAI$ = "Show me the money!"
  169.     END SELECT
  170.  
  171. FUNCTION bplusAI2$ (pN) 'after trying first want to try this make it more likely to hit on 12 than 13, 14...
  172.     SELECT CASE p(pN).Total
  173.         CASE IS < 10: bplusAI2$ = "h" 'no caps!
  174.         CASE 10, 11 'double down unless dealer showing an Ace which is like insurance for dealer
  175.             IF LEN(p(pN).Hand) = 2 AND LEFT$(p(nPlayers).Hand, 1) <> "A" THEN bplusAI2$ = "d" ELSE bplusAI2$ = "h"
  176.         CASE 12: IF RND < .75 THEN bplusAI2$ = "h" ELSE bplusAI2$ = "whatever"
  177.         CASE 13: IF RND < .5 THEN bplusAI2$ = "h" ELSE bplusAI2$ = "whatever"
  178.         CASE 14: IF RND < .25 THEN bplusAI2$ = "h" ELSE bplusAI2$ = "whatever"
  179.         CASE ELSE: bplusAI2$ = "Show me the money!"
  180.     END SELECT
  181.  
  182. FUNCTION Johnno$ (pN)
  183.     IF p(pN).Total = 10 OR p(pN).Total = 11 AND LEN(p(pN).Hand) = 2 THEN
  184.         Johnno$ = "d"
  185.     ELSEIF p(pN).Total = 10 OR p(pN).Total = 11 AND LEN(p(pN).Hand) <> 2 THEN
  186.         Johnno$ = "h"
  187.     ELSEIF 12 <= p(pN).Total AND p(pN).Total <= 15 THEN
  188.         SELECT CASE LEFT$(p(nPlayers).Hand, 1)
  189.             CASE "A", "K", "Q", "J", "X", "9": Johnno$ = "h"
  190.             CASE "7", "8": IF RND < .5 THEN Johnno$ = "h"
  191.             CASE ELSE: Johnno$ = "Hope to Bust em"
  192.         END SELECT
  193.     ELSEIF p(pN).Total < 12 THEN
  194.         Johnno$ = "h"
  195.     ELSE
  196.         Johnno$ = "Stay"
  197.     END IF
  198.  
  199. FUNCTION Steve$ (pN) 'how simple is this! never bust make dealer work for it
  200.     IF p(pN).Total < 12 THEN Steve$ = "h"
  201.  
  202. SUB PlayerAddCard (rec) 'updates player's hand and total
  203.     DIM i AS INTEGER, cv AS INTEGER
  204.     deckIndex = deckIndex + 1
  205.     p(rec).Hand = p(rec).Hand + deck$(deckIndex)
  206.     IF deck$(deckIndex) = "A" THEN p(rec).Ace = -1
  207.     p(rec).Total = 0
  208.     FOR i = 1 TO LEN(p(rec).Hand)
  209.         IF INSTR(rank$, MID$(p(rec).Hand, i, 1)) > 10 THEN cv = 10 ELSE cv = INSTR(rank$, MID$(p(rec).Hand, i, 1))
  210.         p(rec).Total = p(rec).Total + cv
  211.     NEXT
  212.     IF p(rec).Total < 12 AND p(rec).Ace THEN p(rec).Total = p(rec).Total + 10
  213.     IF LEN(p(rec).Hand) = 2 AND p(rec).Total = 21 THEN p(rec).BJ = -1
  214.     IF p(rec).Total > 21 THEN p(rec).Bust = -1
  215.  
  216. SUB showPlayer (nPlayer)
  217.     DIM i AS INTEGER, S$
  218.     COLOR p(nPlayer).FC, p(nPlayer).BC
  219.     FOR i = 0 TO bH - 1 'clear our block
  220.         'PRINT p(nPlayer).Row + i, p(nPlayer).Col
  221.         LOCATE p(nPlayer).Row + i, p(nPlayer).Col: PRINT SPACE$(bW);
  222.     NEXT
  223.     cp nPlayer, 1, "Round:" + STR$(round)
  224.     cp nPlayer, 3, p(nPlayer).ID
  225.     cp nPlayer, 5, "Chips:" + STR$(p(nPlayer).Chips)
  226.     IF nPlayer <> nPlayers THEN cp nPlayer, 6, "Bet:" + STR$(p(nPlayer).Bet)
  227.     FOR i = 1 TO LEN(p(nPlayer).Hand) 'with 12 aces could get very long hands
  228.         S$ = S$ + MID$(p(nPlayer).Hand, i, 1) + " "
  229.     NEXT
  230.     cp nPlayer, 8, "___Hand___"
  231.     IF nPlayer = nPlayers AND LEN(p(nPlayer).Hand) = 2 AND p(nPlayer).Total <> 21 AND blockDealer THEN
  232.         S$ = LEFT$(S$, 2) + "?"
  233.         cp nPlayer, 9, S$
  234.         cp nPlayer, 10, "Total: ??"
  235.     ELSE
  236.         cp nPlayer, 9, S$
  237.         cp nPlayer, 10, "Total:" + STR$(p(nPlayer).Total)
  238.     END IF
  239.  
  240.     IF p(nPlayer).Bust THEN cp nPlayer, 11, "Busted"
  241.     IF p(nPlayer).BJ THEN cp nPlayer, 11, "Blackjack"
  242.  
  243. SUB initGame 'the stuff that never changes
  244.     DIM i, spacer
  245.     FOR i = 1 TO 52 'get deck ready
  246.         deck$(i) = MID$(rank$ + rank$ + rank$ + rank$, i, 1)
  247.     NEXT
  248.  
  249.     'for printing in each players window area of screen = blackjack table
  250.     'xmax/8 =  2*margin + bW *(nplayers -1) + spacer *(nplayer-2)
  251.     spacer = ((xmax / 8) - margin - bW * (nPlayers - 1)) / (nPlayers - 2)
  252.  
  253.     FOR i = 1 TO nPlayers
  254.         IF i <> nPlayers THEN
  255.             SELECT CASE i
  256.                 CASE 1: p(i).ID = "Johnno"
  257.                 CASE 2: p(i).ID = "Steve"
  258.                 CASE 3: p(i).ID = "bplus AI"
  259.                 CASE 4: p(i).ID = "bplus AI2"
  260.             END SELECT
  261.             p(i).Col = margin + (i - 1) * (spacer + bW)
  262.             p(i).Row = 18
  263.             p(i).Chips = 100
  264.             p(i).SetBet = 2
  265.             IF i MOD 2 THEN
  266.                 p(i).BC = _RGB32(RND * 128, RND * 128, RND * 128)
  267.             ELSE
  268.                 p(i).BC = _RGB32(RND * 128 + 127, RND * 128 + 127, RND * 128 + 127)
  269.             END IF
  270.         ELSE
  271.             p(i).ID = "Dealer"
  272.             p(i).Col = (xmax / 8 - bW) / 2
  273.             p(i).Row = 2
  274.             p(i).Chips = -100 * (nPlayers - 1)
  275.             IF i MOD 2 THEN p(i).BC = &HFF000000 ELSE p(i).BC = &HFFFFFFFF
  276.         END IF
  277.         IF i MOD 2 THEN p(i).FC = &HFFFFFFFF ELSE p(i).FC = &HFF000000
  278.     NEXT
  279.  
  280.     'ask the user / YOU what e prefers for betting
  281.     COLOR fc, bc: CLS
  282.     cp 0, 10, "*** Blackjack Multi AI Tester ***"
  283.     cp 0, 12, "Each AI bot starts with 100 chips and bets 2 each round."
  284.     cp 0, 13, "Dealer must hit on 16 or less."
  285.     cp 0, 14, "Blackjack pays 1.5 X the bet."
  286.     cp 0, 15, "Double down option available when you get 2 cards."
  287.     cp 0, 16, "It Doubles the bet and you get one more card."
  288.     cp 0, 20, "press any to continue..."
  289.     SLEEP
  290.  
  291. SUB cp (nPlayer, row, s AS STRING) 'center print a string on the given row
  292.     IF nPlayer THEN
  293.         COLOR p(nPlayer).FC, p(nPlayer).BC
  294.         LOCATE p(nPlayer).Row + row, p(nPlayer).Col + (bW - LEN(s)) / 2: PRINT s;
  295.     ELSE
  296.         COLOR fc, bc
  297.         LOCATE row, (xmax / 8 - LEN(s)) / 2: PRINT s;
  298.     END IF
  299.  
  300.  

 
BJ Multi Test AI.PNG

Offline johnno56

  • Forum Resident
  • Posts: 1270
  • Live long and prosper.
    • View Profile
Re: Blackjack
« Reply #208 on: July 07, 2020, 08:46:21 am »
I know. I know. I'm not a fan of Star Wars but... Darth Vader. Episode IV: A New Hope. "Impressive. Most impressive."
Logic is the beginning of wisdom.

Offline George McGinn

  • Global Moderator
  • Forum Regular
  • Posts: 210
    • View Profile
    • Resume
Re: Blackjack
« Reply #209 on: July 07, 2020, 11:15:45 am »
Over on my iPad, I have a deck shuffling program which is very simple. I create a random array with numbers from 1 to 52, and these numbers serve as an index to another table that holds the card number and suit. I use graphics (UNICODE or the iPad's expanded ASCII Code) and the suit is the same size as the number.

I can bring it over to QB64 and get it to work if you are interested in it. I can even create options where you can create a shuffled array with 4 to 8 decks if you can use it.
____________________________________________________________________
George McGinn
Theoretical/Applied Computer Scientist
Member: IEEE, IEEE Computer Society
Technical Council on Software Engineering
IEEE Standards Association
American Association for the Advancement of Science (AAAS)