Author Topic: Blackjack  (Read 9209 times)

0 Members and 1 Guest are viewing this topic.

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

bplus

• Forum Resident
• Posts: 6567
• What could possibly go wrong?
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.         cp 10, "You have" + STR\$(chips) + " chips to bet."
30.         LOCATE 11, 25: INPUT "(0 quits) Enter your bet > ", bet
31.         IF bet = 0 THEN EXIT DO
32.         IF bet > chips THEN bet = chips
33.         bet = setbet
34.     clearPlayers 'clears screen too
35.     cp 4, "BLACKJACK    chips:" + STR\$(chips) + "   betting:" + STR\$(bet) + " chips."
36.     FOR i = 52 TO 2 STEP -1 'shuffle
37.         r = INT(RND * i) + 1
38.         SWAP deck\$(r), deck\$(i)
39.     deckIndex = 0
40.     FOR i = 1 TO 2 'each Player is dealt 2 cards
43.     cp 7, players(dealer).ID + " ? " + MID\$(players(dealer).Hand, 2, 1)
44.     cp 10, playerShow\$(player)
45.     IF players(player).Total = 21 THEN
46.         chips = chips + 2 * bet
47.         cp 11, "BlackJack! You added" + STR\$(2 * bet) + " to your chips."
48.         GOTO BJskip
49.     WHILE players(player).Total < 21
50.         cp 11, "Press h for Hit, any other to stay..."
51.         card\$ = INKEY\$
52.         WHILE LEN(card\$) = 0: card\$ = INKEY\$: _LIMIT 60: WEND
53.         IF card\$ = "h" THEN
55.             cp 11, SPACE\$(50)
56.             cp 10, playerShow\$(player)
57.             cp 11, SPACE\$(50)
58.     cp 7, playerShow\$(dealer)
59.     WHILE players(player).Total < 22 AND (players(dealer).Total < 21 AND players(dealer).Total < players(player).Total)
60.         cp 8, "Dealer takes a card."
62.         cp 7, playerShow\$(dealer)
63.         cp 8, SPACE\$(50)
64.     IF players(player).Total > 21 OR (players(player).Total <= players(dealer).Total AND players(dealer).Total < 22) THEN
65.         cp 13, "You lose."
66.         chips = chips - bet
67.         cp 13, "You win!"
68.         chips = chips + bet
69.     BJskip:
70.     IF chips = 0 THEN cp 15, "Out of chips!"
71. LOOP UNTIL chips = 0
72. cp 17, "Goodbye"
73.
74. SUB clearPlayers
75.     DIM i
76.     FOR i = 1 TO 2
77.         players(i).Hand = ""
78.         players(i).Ace = 0
79.         players(i).Total = 0
80.
82.     deckIndex = deckIndex + 1
84.     IF deck\$(deckIndex) = "A" THEN players(receiver).Ace = -1
85.     players(receiver).Total = 0
86.     FOR i = 1 TO LEN(players(receiver).Hand)
87.         IF INSTR(rank, MID\$(players(receiver).Hand, i, 1)) > 10 THEN cv = 10 ELSE cv = INSTR(rank, MID\$(players(receiver).Hand, i, 1))
90.
91. FUNCTION playerShow\$ (shower)
92.     DIM i AS INTEGER, S\$
93.     S\$ = players(shower).ID + " "
94.     FOR i = 1 TO LEN(players(shower).Hand)
95.         S\$ = S\$ + MID\$(players(shower).Hand, i, 1) + " "
96.     S\$ = S\$ + "Total =" + STR\$(players(shower).Total)
97.     IF players(shower).Total > 21 THEN S\$ = S\$ + " Busted!"
98.     playerShow\$ = S\$
99.
100. SUB cp (row, s AS STRING)
101.     LOCATE row, (80 - LEN(s)) / 2: PRINT s
102.

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 »

johnno56

• Forum Resident
• Posts: 1014
• 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.

bplus

• Forum Resident
• Posts: 6567
• What could possibly go wrong?
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. ;-))

johnno56

• Forum Resident
• Posts: 1014
• 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.

bplus

• Forum Resident
• Posts: 6567
• What could possibly go wrong?
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.

Pete

• Forum Resident
• Posts: 2554
• 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

bplus

• Forum Resident
• Posts: 6567
• What could possibly go wrong?
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.

bplus

• Forum Resident
• Posts: 6567
• What could possibly go wrong?
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.
7. TYPE Box
8.     TopLeft AS XY
9.
10. TYPE PlayerType 'a container for all program data!
11.     Win AS Box 'for screening the players data
12.     ID AS STRING
13.     Chips AS INTEGER
14.     SetBet AS INTEGER
15.     Bet AS INTEGER
16.     Hand AS STRING
17.     Ace AS INTEGER
18.     Total AS INTEGER
19.     Blackjack AS INTEGER
20.
21. CONST xmax = 640
22. CONST ymax = 432
23. CONST nPlayers = 5 '       the 5th is the Dealer, the 4th is You and 1 - 3 are the bots who go first
24. CONST You = 4
25. CONST Dealer = 5
26. CONST dk = "A2345A6789AXJQKA2345A6789AXJQKA2345A6789AXJQKA2345A6789AXJQK"
27. CONST white = &HFFFDDFDD
28. CONST black = &HFF000000
29. CONST green = &HFF004515
30. CONST orange = &HFFFF3300
31. CONST brown = &HFF3C1D06
32. CONST cyan = &HFF3774C0
33. CONST ltCyan = &HFF99BBFF
34. CONST drkRed = &HFF220102
35. CONST mRed = &HFF7F0000
36. CONST yellow = &HFFFFFF00
37. CONST blue = &HFF00008D
38.
39. DIM SHARED deck\$(1 TO LEN(dk)), player(1 TO nPlayers) AS PlayerType, deckIndex
40. DIM i, j, r, in, is\$, stillPlayer
41. SCREEN _NEWIMAGE(xmax, ymax, 32)
42. COLOR yellow, green
43. FOR i = 1 TO LEN(dk)
44.     deck\$(i) = MID\$(dk, i, 1)
45. initPlayers
46.     clearPlayers 'clears screen too                                                    start a round
47.     stillPlayer = 0 ' to check at end of round if there is still a player playing
48.
49.     IF player(3).Chips > 0 THEN '                                                        handle bets
50.         player(3).Bet = INT(player(3).Chips / 2 + .5)
51.     IF player(You).Chips > 0 THEN
52.         IF player(You).SetBet = 0 THEN
53.             cp 0, 10, "You have" + STR\$(player(4).Chips) + " chips to bet."
54.             LOCATE 11, 25: INPUT "Enter your bet > ", in
55.             IF in = 0 THEN EXIT DO
56.             IF in > player(You).Chips THEN player(You).Bet = player(You).Chips ELSE player(You).Bet = in
57.             player(You).Bet = player(You).SetBet
58.     FOR i = 1 TO 4 'make sure everyone is not betting more than amount of chips they have
59.         IF player(i).Bet > player(i).Chips THEN player(i).Bet = player(i).Chips
60.
61.     FOR i = LEN(dk) TO 2 STEP -1 '                                                                shuffle
62.         r = INT(RND * i) + 1
63.         SWAP deck\$(r), deck\$(i)
64.     deckIndex = 0
65.
66.     FOR j = 1 TO 2 '                                 everybody gets 2 cards if they still have chips
67.         FOR i = 1 TO nPlayers 'each Player is dealt 2 cards
68.             IF ((player(i).Chips > 0) AND (i <> Dealer)) OR (i = Dealer) THEN
70.                 showPlayer i
71.                 IF i = Dealer THEN
72.                     cp Dealer, 7, SPACE\$(18)
73.                     cp Dealer, 8, SPACE\$(18)
74.                     IF j = 1 THEN
75.                         cp Dealer, 7, "? "
76.                         cp Dealer, 8, "Total: ?"
77.                         cp Dealer, 7, SPACE\$(18)
78.                         cp Dealer, 7, "Hand: ? " + MID\$(player(Dealer).Hand, 2, 1)
79.                         cp Dealer, 8, "Total: ?"
80.                 IF i <> Dealer AND j = 2 AND player(i).Total = 21 THEN
81.                     cp i, 9, "Blackjack!"
82.                     player(i).Blackjack = -1
83.                 clsPlayer i
84.
85.     FOR i = 1 TO nPlayers - 1 '                    OK now handle the players until they stay or bust
86.         IF player(i).Chips > 0 THEN
87.             WHILE player(i).Total < 21
88.                 IF i = You THEN
89.                     cp i, 11, "<h>it <s>tay"
90.                     is\$ = INKEY\$
91.                     WHILE LEN(is\$) = 0: is\$ = INKEY\$: _LIMIT 60: WEND
92.                     IF is\$ = "h" THEN
94.                         cp i, 11, SPACE\$(18)
95.                         showPlayer i
96.                         cp i, 11, SPACE\$(18)
97.                     IF player(i).Total < 16 THEN '                               bots to hit on < 16
98.                         cp i, 11, "Hit me."
100.                         showPlayer i
101.                         EXIT WHILE 'stay
102.
103.     showPlayer Dealer '                                                                dealer's turn
104.     WHILE player(Dealer).Total < 17
105.         cp Dealer, 10, "(Takes a card)"
107.         showPlayer Dealer
108.         cp Dealer, 10, SPACE\$(18)
109.
110.     FOR i = 1 TO nPlayers - 1 '                                   name the winners and losers, payup
111.         IF player(i).Chips > 0 THEN
112.             IF player(i).Blackjack = 0 THEN
113.                 IF player(i).Total > 21 OR (player(i).Total <= player(Dealer).Total AND player(Dealer).Total < 22) THEN
114.                     cp i, 11, player(i).ID + " lost."
115.                     player(i).Chips = player(i).Chips - player(i).Bet
116.                     player(Dealer).Chips = player(Dealer).Chips + player(i).Bet
117.                     cp i, 11, player(i).ID + " won!"
118.                     player(i).Chips = player(i).Chips + player(i).Bet
119.                     player(Dealer).Chips = player(Dealer).Chips - player(i).Bet
120.                 cp i, 11, player(i).ID + " won!"
121.                 player(i).Chips = player(i).Chips + 2 * player(i).Bet
122.                 player(Dealer).Chips = player(Dealer).Chips - 2 * player(i).Bet
123.             IF player(i).Chips = 0 THEN cp i, 12, "Out of chips!" ELSE stillPlayer = -1
124.         _DELAY .5
125. LOOP UNTIL stillPlayer = 0
126. cp 0, 12, "Goodbye"
127.
128. SUB clearPlayers 'clear player data for new game
129.     DIM i
130.     FOR i = 1 TO nPlayers
131.         player(i).Hand = ""
132.         player(i).Ace = 0
133.         player(i).Total = 0
134.         player(i).Blackjack = 0
135.
136. SUB addCard (nPlayer) 'add a card to reciever's hand and retotal points according to aces present
137.     deckIndex = deckIndex + 1
138.     player(nPlayer).Hand = player(nPlayer).Hand + deck\$(deckIndex)
139.     IF deck\$(deckIndex) = "A" THEN player(nPlayer).Ace = -1
140.     player(nPlayer).Total = 0
141.     FOR i = 1 TO LEN(player(nPlayer).Hand)
142.         'IF INSTR(rank, MID\$(player(nPlayer).Hand, i, 1)) > 10 THEN cv = 10 ELSE cv = INSTR(rank, MID\$(player(nPlayer).Hand, i, 1))
143.         player(nPlayer).Total = player(nPlayer).Total + CardValue(MID\$(player(nPlayer).Hand, i, 1))
144.     IF player(nPlayer).Total < 12 AND player(nPlayer).Ace THEN player(nPlayer).Total = player(nPlayer).Total + 10
145.
146. SUB showPlayer (nPlayer)
147.     DIM i AS INTEGER, S\$
148.     clsPlayer nPlayer
149.     cp nPlayer, 1, player(nPlayer).ID
150.     cp nPlayer, 3, "Chips:" + STR\$(player(nPlayer).Chips)
151.     IF nPlayer <> Dealer THEN
152.         cp nPlayer, 4, "Bet:" + STR\$(player(nPlayer).Bet)
153.     FOR i = 1 TO LEN(player(nPlayer).Hand) 'with 12 aces could get very long hands
154.         IF LEN(player(nPlayer).Hand) * 2 < player(nPlayer).Win.W THEN
155.             S\$ = S\$ + MID\$(player(nPlayer).Hand, i, 1) + " "
156.             S\$ = S\$ + MID\$(player(nPlayer).Hand, i, 1)
157.     cp nPlayer, 6, "___Hand___"
158.     cp nPlayer, 7, S\$
159.     cp nPlayer, 8, "Total:" + STR\$(player(nPlayer).Total)
160.     IF player(nPlayer).Total > 21 THEN
161.         cp nPlayer, 9, "Busted!"
162.
163. SUB clsPlayer (nPlayer)
164.     DIM i, lastR
165.     COLOR player(nPlayer).Win.FC, player(nPlayer).Win.BC
166.     IF nPlayer = Dealer THEN
167.         lastR = player(nPlayer).Win.TopLeft.Y + player(nPlayer).Win.H - 1
168.         lastR = player(nPlayer).Win.TopLeft.Y + player(nPlayer).Win.H
169.     FOR i = player(nPlayer).Win.TopLeft.Y TO lastR
170.         LOCATE i, player(nPlayer).Win.TopLeft.X: PRINT SPACE\$(player(nPlayer).Win.W);
171.     COLOR yellow, green
172.
173. SUB cp (nPlayer, row, s AS STRING) 'center print a string on the given row
174.     IF nPlayer THEN
175.         COLOR player(nPlayer).Win.FC, player(nPlayer).Win.BC
176.         LOCATE player(nPlayer).Win.TopLeft.Y + row, player(nPlayer).Win.TopLeft.X + (player(nPlayer).Win.W - LEN(s)) / 2: PRINT s;
177.         COLOR yellow, green
178.         COLOR yellow, green
179.         LOCATE row, (80 - LEN(s)) / 2: PRINT s;
180.
181. SUB initPlayers 'the stuff that never changes
182.     DIM i, widt, height
183.
184.     'for printing in each players window area of screen = blackjack table
185.     widt = _WIDTH / (4 * 8)
186.     height = _HEIGHT / (2 * 16)
187.     FOR i = 1 TO nPlayers - 1
188.         player(i).Win.TopLeft.X = widt * (i - 1) + 2
189.         player(i).Win.TopLeft.Y = height - 1
190.         player(i).Win.W = widt - 2
191.         player(i).Win.H = height - 1
192.         player(i).Chips = 100
193.     player(Dealer).Win.TopLeft.X = (_WIDTH / 8 - widt) / 2 + 1
194.     player(Dealer).Win.TopLeft.Y = 1
195.     player(Dealer).Win.W = widt
196.     player(Dealer).Win.H = height - 2
197.     player(Dealer).Chips = -400 'the dealer has "lent" 100 chips to each player
198.
199.     player(1).Win.FC = brown: player(1).Win.BC = orange: player(1).ID = "Pete": player(1).SetBet = 5: player(1).Bet = 5
200.     player(2).Win.FC = drkRed: player(2).Win.BC = cyan: player(2).ID = "Qwerkey": player(2).SetBet = 15: player(2).Bet = 15
201.     player(3).Win.FC = white: player(3).Win.BC = mRed: player(3).ID = "Johnno": player(3).SetBet = 0
202.     player(4).Win.FC = ltCyan: player(4).Win.BC = blue: player(4).ID = "You"
203.     player(5).Win.FC = yellow: player(5).Win.BC = green: player(5).ID = "Dealer"
204.
205.     'ask the user / YOU what e prefers for betting
206.     cp 0, 8, "For this Blackjack Game, you start with 100 chips"
207.     cp 0, 9, "and can bet any amount of them for each game."
208.     cp 0, 10, "If you'd like to set your bet for each game now"
209.     cp 0, 11, "enter that amount otherwise enter 0 and get asked"
210.     cp 0, 12, "before each round at the table."
211.     LOCATE 13, 38: INPUT ""; i
212.     IF i < 0 THEN i = 0 'lets not do allot of checking and reasking
213.     IF i > 100 THEN i = 100
214.     player(You).SetBet = i
215.
216. FUNCTION CardValue (s\$)
217.     IF s\$ = "A" THEN
218.         CardValue = 1
219.     ELSEIF INSTR("23456789", s\$) > 0 THEN
220.         CardValue = VAL(s\$)
221.     ELSEIF INSTR("XJQK", s\$) > 0 THEN
222.         CardValue = 10
223.

QBExile

• Newbie
• Posts: 27
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."

bplus

• Forum Resident
• Posts: 6567
• What could possibly go wrong?
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 »

bplus

• Forum Resident
• Posts: 6567
• What could possibly go wrong?
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.     cname AS STRING
11.     value AS INTEGER '1 to 13
12.     suit AS INTEGER
13.     points AS INTEGER '1 to 10
14.
15. TYPE PlayerType
16.     ID AS STRING
17.     hand AS STRING
18.     cardY AS INTEGER
19.     Ace AS INTEGER
20.     Total AS INTEGER
21.
22. CONST player = 1
23. CONST dealer = 2
24.
25. DIM SHARED Deck(1 TO 52) AS CardType, Players(1 TO 2) AS PlayerType, deckIndex
26. DIM SHARED CardDeck AS LONG, round
27. Players(player).ID = "Player": Players(player).cardY = 420
28. Players(dealer).ID = "Dealer": Players(dealer).cardY = 100
29. DIM SHARED bet, chips
30. DIM i, card\$, setbet, loaded
31.
33. IF loaded = 0 THEN PRINT "PlayingCards.png failed to load, bye!": END
34.
35. initDeck
36.
37. chips = 100
38. cp 8, "For this Blackjack Game, you start with 100 chips"
39. cp 9, "and can bet any amount of them for each game."
40. cp 10, "If you'd like to set your bet for each game now"
41. cp 11, "enter that amount otherwise enter 0 "
42. cp 12, ""
43. INPUT ""; setbet
44.     IF setbet = 0 THEN
45.         cp 10, "You have" + STR\$(chips) + " chips to bet."
46.         LOCATE 11, 25: INPUT "(0 quits) Enter your bet > ", bet
47.         IF bet = 0 THEN EXIT DO
48.         IF bet > chips THEN bet = chips
49.         bet = setbet
50.     clearPlayers 'clears screen too
51.     shuffle
52.     deckIndex = 0
53.     FOR i = 1 TO 2 'each Player is dealt 2 cards
56.         round = round + 1
57.     'cp 7, Players(dealer).ID + " ? " + MID\$(Players(dealer).hand, 2, 1)
58.     playerShow dealer
59.     playerShow player
60.     IF Players(player).Total = 21 THEN
61.         chips = chips + 2 * bet
62.         cp 36, "BlackJack! You added" + STR\$(2 * bet) + " to your chips."
63.         GOTO BJskip
64.     WHILE Players(player).Total < 21
65.         cp 36, "Press h for Hit, any other to stay..."
66.         card\$ = INKEY\$
67.         WHILE LEN(card\$) = 0: card\$ = INKEY\$: _LIMIT 60: WEND
68.         IF card\$ = "h" THEN
70.             playerShow player
71.             cp 38, SPACE\$(50)
72.     round = round + 1 'now round > 2 so first card won't be hidden
73.     playerShow dealer
74.     WHILE Players(dealer).Total < 17
75.         cp 16, "Dealer takes a card."
77.         playerShow dealer
78.     IF (Players(player).Total > 21 AND Players(dealer).Total < 22) OR (Players(player).Total < Players(dealer).Total AND Players(dealer).Total < 22) THEN
79.         cp 38, "You lost."
80.         chips = chips - bet
81.     ELSEIF (Players(player).Total > Players(dealer).Total AND Players(player).Total < 22) OR (Players(player).Total < 22 AND Players(dealer).Total > 21) THEN
82.         cp 38, "You won!"
83.         chips = chips + bet
84.     ELSEIF (Players(player).Total = Players(dealer).Total) OR (Players(player).Total > 21 AND Players(dealer).Total > 21) THEN
85.         cp 38, "We tied..."
86.     BJskip:
87.     IF chips = 0 THEN cp 40, "Out of chips!"
88. LOOP UNTIL chips = 0
89. cp 5, "Goodbye"
90.
91. SUB clearPlayers
92.     DIM i
93.     FOR i = 1 TO 2
94.         Players(i).hand = ""
95.         Players(i).Ace = 0
96.         Players(i).Total = 0
97.     round = 0
98.
100.     DIM i, di
101.     deckIndex = deckIndex + 1
103.     IF Deck(deckIndex).value = 1 THEN Players(receiver).Ace = -1
104.     Players(receiver).Total = 0
105.     FOR i = 1 TO LEN(Players(receiver).hand) STEP 2
106.         di = VAL(MID\$(Players(receiver).hand, i, 2))
109.
110. FUNCTION d2\$ (num) 'we have to store 1 or 2 digit numbers in a string to represent hand that is buch of card indexes
111.     d2\$ = LEFT\$(_TRIM\$(STR\$(num)) + "  ", 2)
112.
113. SUB playerShow (shower)
114.     DIM i, xoff, di
115.
116.     IF shower = dealer THEN
117.         LINE (0, 0)-STEP(xmax, 319), &HFF000000, BF
118.         cp 2, "B+ BLACKJACK: If Dealer total < 16 then must hit else stays."
119.         cp 5, Players(shower).ID
120.         cp 15, "Total =" + STR\$(Players(shower).Total)
121.         IF Players(shower).Total > 21 THEN cp 16, "Busted!"
122.         LINE (0, 320)-STEP(xmax, 320), &HFF000000, BF
123.         cp 25, "Chips:" + STR\$(chips) + "   Betting:" + STR\$(bet) + " chips."
124.         cp 23, Players(shower).ID
125.         cp 35, "Total =" + STR\$(Players(shower).Total)
126.         IF Players(shower).Total > 21 THEN cp 36, "Busted!"
127.     xoff = (xmax - 43 * LEN(Players(shower).hand)) / 2
128.     FOR i = 1 TO LEN(Players(shower).hand) STEP 2
129.         di = VAL(MID\$(Players(shower).hand, i, 2))
130.         showCardDI xoff, Players(shower).cardY, di
131.         xoff = xoff + 86
132.     IF shower = dealer AND round <= 2 THEN 'hide first card
133.         xoff = (xmax - 43 * LEN(Players(shower).hand)) / 2
134.         ShowCard xoff, Players(dealer).cardY, 0, 4
135.         cp 15, " Total = ?? "
136.
137. SUB cp (row, s AS STRING)
138.     LOCATE row, (xmax / 8 - LEN(s)) / 2: PRINT s;
139.
140. SUB initDeck
141.     DIM i, s, v
142.     DIM suit\$(3)
143.     suit\$(0) = "Hearts": suit\$(1) = "Clubs": suit\$(2) = "Diamonds": suit\$(3) = "Spades"
144.     DIM v\$(1 TO 13)
145.     v\$(1) = "Ace": v\$(11) = "Jack": v\$(12) = "Queen": v\$(13) = "King"
146.     FOR i = 2 TO 10
147.         v\$(i) = _TRIM\$(STR\$(i))
148.     i = 0
149.     FOR s = 0 TO 3
150.         FOR v = 1 TO 13
151.             i = i + 1
152.             Deck(i).ID = i
153.             Deck(i).cname = v\$(v) + " of " + suit\$(s)
154.             Deck(i).value = v
155.             Deck(i).suit = s
156.             IF v < 11 THEN Deck(i).points = v ELSE Deck(i).points = 10
157.
158. SUB shuffle
159.     DIM i, r
160.     FOR i = 52 TO 2 STEP -1
161.         r = INT(i * RND) + 1
162.         SWAP Deck(i), Deck(r)
163.
164. SUB showCardDI (x AS INTEGER, y AS INTEGER, deckI AS INTEGER)
165.     ShowCard x, y, Deck(deckI).value - 1, Deck(deckI).suit
166.
167. 'at scale = 2 need screen space of 76 x 100 for one card
168. SUB ShowCard (ScrnX, ScrnY, imageCol, imageRow) 'screen x, y  h= horizontal  , v =vertical  image h, v
169.     DIM ch, cv, scale
170.     ch = 38 * imageCol + 1: cv = 51 * imageRow + 1: scale = 2 ' <  might want to change?
171.     _PUTIMAGE (ScrnX, ScrnY)-STEP(38 * scale, 50 * scale), CardDeck, 0, (ch, cv)-(ch + 37, cv + 50)
172.
174.     CardDeck = _LOADIMAGE("PlayingCards.png")
175.     IF CardDeck = -1 THEN EXIT FUNCTION
176.     _SOURCE CardDeck
177.     _CLEARCOLOR POINT(0, 0), CardDeck
178.     loadCardImages% = -1
179.
180.

Pete

• Forum Resident
• Posts: 2554
• 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

bplus

• Forum Resident
• Posts: 6567
• What could possibly go wrong?
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 »

johnno56

• Forum Resident
• Posts: 1014
• 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.

bplus

• Forum Resident
• Posts: 6567
• What could possibly go wrong?
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! :-))