|
Post by bluatigro on Jun 4, 2019 9:41:29 GMT
dim deck$( 52 ) global rank$ : rank$ = "A23456789XJQK" global suit$ : suit$ = "CDHS" dim player$( 10 ) ''hand for player dim ai$( 10 ) ''hand for computer global dtel , htel , aitel ''create deck for r = 0 to 12 for s = 0 to 3 rk$ = mid$( rank$ , r + 1 , 1 ) st$ = mid$( suit$ , s + 1 , 1 ) deck$( r * 4 + s ) = st$ + rk$ next s next r ''shufle deck for i = 0 to 51 dice = int( rnd(0) * 52 ) h$ = deck$( dice ) deck$( dice ) = deck$( i ) deck$( i ) = h$ next i print "welkome by black jack :" print "a game in you try to get 21 ." print "you lose if the AI gets 21 to ." print "you lose if you get more then 21 ." print "you win if you get more then the AI ." call human call ai a = ai.total() h = human.total() if h = 21 then if a = 21 then print "you lose ." else print "you win ." end if else if h > 21 then print "you lose ." else if h > a then print "you win !!" else print "you lose ." end if end if end if end sub human htel = 0 do do print "human hand : " ; for i = 1 to htel print human$( i ) ; " " ; next i print input "do you want a card ? [ 'y' or 'n' ] : " ; in$ in$ = lower$( in$ ) loop while in$ <> "y" and in$ <> "n" if in$ = "y" then human$( htel ) = deck$( dtel ) htel = htel + 1 dtel = dtel + 1 end if loop while in$ = "y" end sub sub ai aitel = 0 do ai$( aitel ) = deck$( dtel ) total = ai.total() aitel = aitel + 1 dtel = dtel + 1 loop while total < 15 print "AI hand : " ; for i = 0 to aitel print ai$( i ) ; " " ; next i print end sub function human.total() total = 0 for i = 0 to htel total = total + card.value( human$( i ) ) next i isAce = 0 for i = 0 to htel if right$( human$( i ) , 1 ) = "A" then isAce = 1 end if next i if isAce then if total < 11 then total = total + 10 end if end if human.total = total end function function ai.total() total = 0 for i = 0 to htel total = total + card.value( ai$( i ) ) next i isAce = 0 for i = 0 to htel if right$( ai$( i ) , 1 ) = "A" then isAce = 1 end if next i if isAce then if total < 11 then total = total + 10 end if end if ai.total = total end function function card.value( card$ ) q = instr( rank$ , right$( card$ , 1 ) ) if q > 10 then q = 10 end if card.value = q end function
|
|
|
Post by B+ on Jun 5, 2019 0:07:26 GMT
When does 15 beat 20?
|
|
|
Post by B+ on Jun 5, 2019 4:05:34 GMT
Fixed, it wasn't showing the first card(0):
dim deck$( 52 ) global rank$ : rank$ = "A23456789XJQK" global suit$ : suit$ = "CDHS" dim player$( 10 ) ''hand for player dim ai$( 10 ) ''hand for computer global dtel , htel , aitel ''create deck for r = 0 to 12 for s = 0 to 3 rk$ = mid$( rank$ , r + 1 , 1 ) st$ = mid$( suit$ , s + 1 , 1 ) deck$( r * 4 + s ) = st$ + rk$ next s next r ''shufle deck for i = 0 to 51 dice = int( rnd(0) * 52 ) h$ = deck$( dice ) deck$( dice ) = deck$( i ) deck$( i ) = h$ next i print "welkome by black jack :" print "a game in you try to get 21 ." print "you lose if the AI gets 21 to ." print "you lose if you get more then 21 ." print "you win if you get more then the AI ." call human call ai a = ai.total() h = human.total() if h = 21 then if a = 21 then print "you lose ." else print "you win ." end if else if h > 21 then print "you lose ." else if h > a then print "you win !!" else print "you lose ." end if end if end if end sub human htel = 0 do do print "human hand : " ; for i = 0 to htel '<<<<<<<<<<<<<<<<<<<<<< fix for i = 1 to i = 0 print human$( i ) ; " " ; next i print print "Human's Total = ";human.total() print input "do you want a card ? [ 'y' or 'n' ] : " ; in$ in$ = lower$( in$ ) loop while in$ <> "y" and in$ <> "n" if in$ = "y" then human$( htel ) = deck$( dtel ) htel = htel + 1 dtel = dtel + 1 end if loop while in$ = "y" end sub sub ai aitel = 0 do ai$( aitel ) = deck$( dtel ) total = ai.total() aitel = aitel + 1 dtel = dtel + 1 loop while total < 15 print "AI hand : " ; for i = 0 to aitel print ai$( i ) ; " " ; next i print end sub function human.total() total = 0 for i = 0 to htel total = total + card.value( human$( i ) ) next i isAce = 0 for i = 0 to htel if right$( human$( i ) , 1 ) = "A" then isAce = 1 end if next i if isAce then if total < 11 then total = total + 10 end if end if human.total = total end function function ai.total() total = 0 for i = 0 to htel total = total + card.value( ai$( i ) ) next i isAce = 0 for i = 0 to htel if right$( ai$( i ) , 1 ) = "A" then isAce = 1 end if next i if isAce then if total < 11 then total = total + 10 end if end if ai.total = total end function function card.value( card$ ) q = instr( rank$ , right$( card$ , 1 ) ) if q > 10 then q = 10 end if card.value = q end function
|
|
|
Post by B+ on Jun 5, 2019 5:04:30 GMT
Fixed up a play again version:
dim deck$( 52 ), ai$(15), human$(15) global rank$ : rank$ = "A23456789XJQK" global suit$ : suit$ = "CDHS" global dtel , htel , aitel
''create deck for r = 0 to 12 for s = 0 to 3 rk$ = mid$( rank$ , r + 1 , 1 ) st$ = mid$( suit$ , s + 1 , 1 ) deck$( r * 4 + s ) = st$ + rk$ next s next r
[restart] cls ''shufle deck for i = 0 to 51 dice = int( rnd(0) * 52 ) h$ = deck$( dice ) deck$( dice ) = deck$( i ) deck$( i ) = h$ next i print "welkome by black jack :" print "a game in you try to get 21 ." print "you lose if the AI gets 21 to ." print "you lose if you get more then 21 ." print "you win if you get more then the AI ." print scan call human call ai a = ai.total() h = human.total() if h <= 21 and a <= 21 then if a >= h then print "you lose ." else print "you win ." end if else if a > 21 and h <= 21 then print "You win!" else print "you lose ." end if end if print input "Play again? Enter y for yes..."; playAgain$ if playAgain$ <> "y" then print "Goodbye!":end goto [restart]
sub human htel = 0 : dtel = 0 for i = 0 to 15: human$( i ) = "" :next for i = 1 to 2 human$( htel ) = deck$( dtel ) htel = htel + 1 dtel = dtel + 1 next do print "human hand : " ; for i = 0 to htel '<<<<<<<<<<<<<<<<<<<<<< fix for i = 1 to i = 0 print human$( i ) ; " " ; next i print print "Human's Total = ";human.total() print input "do you want a card ? [ 'y' or 'n' ] : " ; in$ in$ = lower$( in$ ) if in$ = "y" then human$( htel ) = deck$( dtel ) htel = htel + 1 dtel = dtel + 1 end if loop while in$ = "y" end sub
sub ai h = human.total() if h < 22 then aitel = 0 for i = 0 to 15: ai$( i ) = "" :next for i = 1 to 2 ai$( aitel ) = deck$( dtel ) aitel = aitel + 1 dtel = dtel + 1 next while ai.total() < h and ai.total() < 22 ai$( aitel ) = deck$( dtel ) aitel = aitel + 1 dtel = dtel + 1 wend print "AI hand : " ; for i = 0 to aitel print ai$( i ) ; " " ; next i print print "AI's total = ";ai.total() print end if end sub
function human.total() total = 0 for i = 0 to htel total = total + card.value( human$( i ) ) next i isAce = 0 for i = 0 to htel if right$( human$( i ) , 1 ) = "A" then isAce = 1 end if next i if isAce then if total < 11 then total = total + 10 end if end if human.total = total end function
function ai.total() total = 0 for i = 0 to htel total = total + card.value( ai$( i ) ) next i isAce = 0 for i = 0 to htel if right$( ai$( i ) , 1 ) = "A" then isAce = 1 end if next i if isAce then if total < 11 then total = total + 10 end if end if ai.total = total end function
function card.value( card$ ) q = instr( rank$ , right$( card$ , 1 ) ) if q > 10 then q = 10 end if card.value = q end function
|
|
|
Post by B+ on Jun 5, 2019 5:18:13 GMT
If you want a LARGE PRINT version and play for points then:
'LARGE PRINT 21 GAME.txt for JB (B+=MGA) 2017-10-01 ' from: 21 new start.txt for JB [B+=MGA] 2016-03-25 ' with LARGE PRINT mod inspired by Don Johnson problem post
global xmax, ymax 'these two you can easily reset to your needs 'set these to screen width = xmax, screen height = ymax, that you want
xmax = 1000 : ymax = 700 '<<<<<< set this as you need or from plug-in notes
global cellW, cellH 'do not mess with cellW and cellH globals for printing cellW = 14 'pixels wide for characters cellH = 28 'pixels high for characters
global maxRow, maxCol 'and then these are calclated from above globals maxCol = int(xmax / cellW) 'these control printing characters maxRow = int(ymax / cellH)
global lastC, lastR 'for loc8 (locate), pl (print a line), lp (locate and print) lastC = 1 : lastR = 1
'key events update globals with latest info global inkee$, h$
h$ = "#gr"
nomainwin
WindowWidth = xmax + 8 WindowHeight = ymax + 32 UpperLeftX = (DisplayWidth-WindowWidth) / 2 UpperLeftY = (DisplayHeight-WindowHeight) / 2
open " LARGE PRINT GAME OF 21" for graphics_nsb_nf as #gr '<, == change for plug in modules #gr "trapclose quit"
'fonts that don't work arial, tahoma, verdana 'fonts that work '#gr "font courier_new 10 20" '#gr "font consolas 10 20" #gr "font dejavu_sans_mono ";cellW;" ";cellH
'#gr "home" '< check drawing area '#gr "posxy w2 h2" '<<<<<<<<<<<<<<<<<< 'notice "Screen Drawing Check";chr$(13);"Size:" + chr$(13) + "Width (w2*2) = ";w2*2;", Height (h2*2) = ";h2*2
#gr "setfocus" #gr "when characterInput charIn" #gr "down"
'=================================================== plug-in main modules here
DIM Deck(52), Player$(11) 'prep deck for shuffles FOR i = 1 TO 52 Deck(i) = i NEXT
GLOBAL deckindex, playerindex, points, ace, playertotal, pointsrisk points = 100
DO call getReady call getPointsRisk call drawcard call drawcard call playhand call dealer LOOP UNTIL points = 0 call clear 0, 0, 0 #gr "color white" call cp 10, "Game Over" call pause 2500 call quit h$
SUB getReady ' shuffle deck before each round with simple and tested routine FOR i = 52 TO 2 step -1 r = INT(RND(1) * i) + 1 t = Deck(i) Deck(i) = Deck(r) Deck(r) = t NEXT ' restart these for next round DIM Player$(11) playerindex = 1 : deckindex = 1 playertotal = 0 : pointsrisk = 0 : ace = 0 END SUB
SUB getPointsRisk call clear 0, rnd(0) * 100 + 155, rnd(0)*100 + 155 call cp 4, "You have ";str$(points);" points available to risk:" call loc8 18, 8 call inp " (0 = quit) Enter point risk > ", pointsrisk$ pointsrisk = val(pointsrisk$) IF pointsrisk <= 0 THEN call cp 10, "OK, goodbye" call pause 2500 call quit h$ ELSE IF pointsrisk > points THEN pointsrisk = points END IF END SUB
SUB drawcard value = Deck(deckindex) mod 13 IF value = 1 THEN IF ace = 0 OR ace = 1 THEN IF playertotal < 11 THEN ace = 11 ELSE ace = 1 playertotal = playertotal + ace ELSE playertotal = playertotal + 1 END IF ELSE IF value > 1 AND value < 10 THEN playertotal = playertotal + value ELSE playertotal = playertotal + 10 END IF END IF IF playertotal > 21 AND ace = 11 THEN playertotal = playertotal - 10 : ace = 1 Player$(playerindex) = Cardname$(value) deckindex = deckindex + 1 : playerindex = playerindex + 1 END SUB
SUB playhand g = rnd(0) * 100 + 155 : b = rnd(0) * 100 + 155 WHILE 1 call clear 0, g, b call cp 2, "TWENTYONE";" Points: ";str$(points);" risking ";str$(pointsrisk) s$ = "Player's cards: " FOR i=1 TO playerindex-1 s$ = s$ + " " + Player$(i) NEXT call cp 6, s$ call cp 8, "Card total at present is " + str$(playertotal) if playertotal >= 21 then exit while s$ = "Enter 1 for another card, 2 to stay " IF ace = 11 and playertotal <> 21 THEN s$ = s$ + "or 3 to change ace value to one " call cp 10, s$ DO call loc8 35, 12 call inp "", choice$ choice = val(choice$) LOOP UNTIL choice = 1 OR choice = 2 OR (choice = 3 AND ace = 11) IF choice = 1 THEN CALL drawcard ELSE IF ace = 11 AND choice = 3 THEN playertotal = playertotal - 10 : ace = 1 ELSE EXIT WHILE END IF END IF WEND END SUB
SUB dealer s$ = "Dealer's cards: " if playertotal < 22 then enough = playertotal else enough = 12 WHILE dealtotal <= enough s$ = s$ + " " + Cardname$(Deck(deckindex)) dcard = Deck(deckindex) MOD 13 deckindex = deckindex + 1 IF dcard = 1 THEN 'deal with ace IF dealtotal < 11 THEN 'go for or make 21 dealtotal = dealtotal + 11 : dealace = 11 ELSE dealtotal = dealtotal + 1 END IF ELSE IF dcard > 1 AND dcard < 10 THEN dealtotal = dealtotal + dcard ELSE dealtotal = dealtotal + 10 END IF END IF IF dealtotal > 21 AND dealace = 11 THEN dealtotal = dealtotal - 10 dealace = 1 END IF wend call cp 16, s$ call cp 18, "The dealer has a total of " +str$(dealtotal) + "." IF (playertotal > dealtotal AND playertotal <= 21) OR (playertotal <= 21 AND dealtotal > 21) THEN points = points + pointsrisk call cp 21, "You won " + str$(pointsrisk) + " points!" ELSE IF (dealtotal > playertotal AND dealtotal <= 21) OR (playertotal > 21 AND dealtotal <= 21) THEN points = points - pointsrisk call cp 21, "You lost " + str$(pointsrisk) + " points." ELSE call cp 21, "You tied." END IF END IF call loc8 23, 24 call inp "Press enter to continue... ", temp$ END SUB
FUNCTION Cardname$(avalue) SELECT CASE avalue mod 13 CASE 1 cn$ = "Ace" CASE 11 cn$ = "Jack" CASE 12 cn$ = "Queen" CASE 0 cn$ = "King" CASE 2, 3, 4, 5, 6, 7, 8, 9,10 cn$ = str$(avalue mod 13) END SELECT Cardname$ = cn$ END FUNCTION
wait ' end plug-in section ========================== '========================== subset of DE procedures
sub charIn hdl$, c$ inkee$ = c$ end sub
sub quit hdl$ timer 0 close #gr end end sub
sub clear r, g, b 'clear screen to new RGB color and set backcolor ' and set up so pl (print line) will start at line 1, cell column 1) #gr "fill ";r;" ";g;" ";b #gr "backcolor ";r;" ";g;" ";b lastC = 1 : lastR = 1 end sub
sub loc8 x, y 'locate xColumnCell, yRowCell for printing if 0 < x and x < maxCol + 1 and 0 < y and y < maxRow + 1 then lastC = x lastR = y end if end sub
sub pl mess$ 'print line (feed) startR = lastR for i = 1 to len(mess$) scan call lp lastC, lastR, mid$(mess$, i, 1) if lastR <> startR then exit for next lastC = 1 lastR = startR + 1 if lastR > maxRow then lastR = maxRow 'yuck! end sub
sub lp x, y, mess$ 'locate x, y : print mess$ lp = locate and print 'if locate = x col and y row then and top left corner locates as 1, 1 c = x - 1: r = y if 0 < x and x < maxCol + 1 and 0 < y and y < maxRow + 1 then #gr "place ";c * cellW;" ";r * cellH - 4 #gr "|";mess$ lastC = x + len(mess$) if lastC > maxCol then lastC = 1 : lastR = lastR + 1 if lastR > maxRow then lastR = maxRow 'yuck! end if end sub
sub cp y,cpText$ 'cp Center Print on line y the cpText$ call lp int((maxCol - len(cpText$))/2 + 1.5), y, cpText$ lastC = 1 : lastR = y + 1 end sub
sub inp prmpt$, byref var$ 'input 'prints prompt at lastC, lastR and leaves lastC = 1 lastR = pRow + 1
inkee$ = "" 'clear last key (new fix for DE5) call lp lastC, lastR, prmpt$;"{" 'this will update lastR and lastC to the starting point of input variable pRow = lastR : pCol = lastC 'save these for redrawing var call lp pCol, pRow, "}" OK$ = "ABCDEFGHIJKLMNOPQRSTUVWXYZ abcdefghijklmnopqrstuvwxyz" OK$ = OK$+ chr$(8)+ chr$(27) + chr$(13) + "1234567890!@#$%^&*()_-+={}[]|\:;'<,>.?/" do scan if instr(OK$, inkee$) then if inkee$ = Chr$(8) then if t$ <> "" then if Len(t$)=1 then t$="" else t$=Left$(t$,Len(t$)-1) end if else if inkee$=Chr$(13) or inkee$=Chr$(27) then 'new D5, I was expecting nothing in return for my esc if inkee$ = chr$(27) then t$ = "" exit do else t$=t$;inkee$ end if end if call lp pCol, pRow, t$;"} " inkee$ = "" end if loop until done var$ = t$ lastC = 1 : lastR = pRow + 1 end sub
sub at xPix, yPix, char$ 'print a string at pixel x, y This pin point locating. #gr "place ";xPix;" ";yPix #gr "|";char$ end sub
sub pause mil 'tsh version has scan built-in t0 = time$("ms") while time$("ms") < t0 + mil : scan : wend end sub
|
|
|
Post by B+ on Jun 5, 2019 17:53:57 GMT
I was curious how few lines I might get a decent, readable Blackjack game coded. Testing my fix of blu's code with try again I ran into errors occasionally, see if anyone hits errors with this code at over 50 lines less and tracking points:
'minimum BlackJack.txt B+ started from Bluatigro Blackjack fix B+ mod at JB 2019-06-05 global rank$, deckIndex, playerTotal, dealerTotal rank$ = "A23456789XJQK" DIM deck$(51) FOR i = 0 TO 51 'create deck deck$(i) = MID$(rank$ + rank$ + rank$ + rank$, i + 1, 1) NEXT points = 100 WHILE points 'the main loop manages the points CLS ' PRINT "For Blackjack, you have "; points; " points to risk." INPUT "Enter how many you wish to risk (0 quits) > "; risk IF risk <= 0 THEN EXIT WHILE IF risk > points THEN risk = points FOR i = 51 TO 1 STEP -1 r = INT((i + 1) * RND(0)) t$ = deck$(i) deck$(i) = deck$(r) deck$(r) = t$ NEXT call playHand 1 'player call playHand 0 'dealer IF playerTotal > 21 OR (playerTotal <= dealerTotal AND dealerTotal < 22) THEN PRINT "You loose.": points = points - risk ELSE PRINT "You win!": points = points + risk END IF PRINT IF points THEN DO playAgain$ = "" INPUT "Play again? Enter y for yes, n for no..."; playAgain$ LOOP UNTIL playAgain$ = "y" OR playAgain$ = "n" IF playAgain$ = "n" THEN EXIT WHILE ELSE PRINT "Out of Points!" END IF WEND PRINT "Goodbye!": END
SUB playHand TFplayer DIM hand$(12) index = 0 IF TFplayer THEN deckIndex = 0 ELSE IF playerTotal > 21 THEN EXIT SUB 'player lost already END IF FOR i = 1 TO 2 'deal first 2 cards hand$(index) = deck$(deckIndex) index = index + 1 deckIndex = deckIndex + 1 NEXT DO IF TFplayer THEN PRINT "Player's hand: "; ELSE PRINT "Dealer's hand: "; total = 0 FOR i = 0 TO index - 1 PRINT hand$(i); " "; total = total + cardValue(hand$(i)) IF hand$(i) = "A" THEN ace = -1 NEXT IF ace AND total < 12 THEN total = total + 10 PRINT IF TFplayer THEN PRINT "Player's total = "; ELSE PRINT "Dealer's total = "; PRINT total IF TFplayer AND total < 21 THEN INPUT "Do you want another card ? (y for yes) "; inp$ IF inp$ = "y" THEN addCard = -1 ELSE addCard = 0 ELSE IF total < 21 AND total < playerTotal THEN addCard = -1 ELSE addCard = 0 END IF IF addCard THEN hand$(index) = deck$(deckIndex) total = total + cardValue(deck$(deckIndex)) index = index + 1 deckIndex = deckIndex + 1 ELSE IF TFplayer THEN playerTotal = total ELSE dealerTotal = total EXIT DO END IF LOOP until 1 = 0 PRINT END SUB
FUNCTION cardValue (card$) ' I like bluatigro's method of getting card value IF INSTR(rank$, card$) > 10 THEN cardValue = 10 ELSE cardValue = INSTR(rank$, card$) END FUNCTION
|
|
|
Post by B+ on Jun 5, 2019 20:12:05 GMT
OK something special has got to happen if the player gets Blackjack! This is not 21 anymore (dealer wins ties in Blackjack.) How about getting twice what you risked: 'minimum BlackJack.txt B+ started from Bluatigro Blackjack fix B+ mod at JB 2019-06-05 global rank$, deckIndex, playerTotal, dealerTotal, BJflag rank$ = "A23456789XJQK" DIM deck$(51) FOR i = 0 TO 51 'create deck deck$(i) = MID$(rank$ + rank$ + rank$ + rank$, i + 1, 1) NEXT points = 100 WHILE points 'the main loop manages the points CLS ' PRINT "For Blackjack, you have "; points; " points to risk." INPUT "Enter how many you wish to risk (0 quits) > "; risk IF risk <= 0 THEN EXIT WHILE IF risk > points THEN risk = points FOR i = 51 TO 1 STEP -1 r = INT((i + 1) * RND(0)) t$ = deck$(i) deck$(i) = deck$(r) deck$(r) = t$ NEXT ''test BlackJack code 'deck$(0) = "A" 'deck$(1) = "J" call playHand 1 'player IF BJflag THEN BJflag = 0 points = points + 2 * risk PRINT "BlackJack! You win twice your risk." GOTO [BJskip] END IF call playHand 0 'dealer IF playerTotal > 21 OR (playerTotal <= dealerTotal AND dealerTotal < 22) THEN PRINT "You loose.": points = points - risk ELSE PRINT "You win!": points = points + risk END IF [BJskip] PRINT IF points THEN DO playAgain$ = "" INPUT "Play again? Enter y for yes, n for no..."; playAgain$ LOOP UNTIL playAgain$ = "y" OR playAgain$ = "n" IF playAgain$ = "n" THEN EXIT WHILE ELSE PRINT "Out of Points!" END IF WEND PRINT "Goodbye!": END
SUB playHand TFplayer DIM hand$(12) index = 0 IF TFplayer THEN deckIndex = 0 ELSE IF playerTotal > 21 THEN EXIT SUB 'player lost already END IF FOR i = 1 TO 2 'deal first 2 cards hand$(index) = deck$(deckIndex) index = index + 1 deckIndex = deckIndex + 1 NEXT DO IF TFplayer THEN PRINT "Player's hand: "; ELSE PRINT "Dealer's hand: "; total = 0 FOR i = 0 TO index - 1 PRINT hand$(i); " "; total = total + cardValue(hand$(i)) IF hand$(i) = "A" THEN ace = -1 NEXT IF ace AND total < 12 THEN total = total + 10 PRINT IF TFplayer THEN PRINT "Player's total = "; ELSE PRINT "Dealer's total = "; PRINT total IF TFplayer AND total < 21 THEN INPUT "Do you want another card ? (y for yes) "; inp$ IF inp$ = "y" THEN addCard = -1 ELSE addCard = 0 ELSE IF total < 21 AND total < playerTotal THEN addCard = -1 ELSE addCard = 0 END IF IF addCard THEN hand$(index) = deck$(deckIndex) total = total + cardValue(deck$(deckIndex)) index = index + 1 deckIndex = deckIndex + 1 ELSE IF TFplayer AND total = 21 AND index = 2 THEN BJflag = -1: EXIT SUB IF TFplayer THEN playerTotal = total ELSE dealerTotal = total EXIT DO END IF LOOP until 1 = 0 PRINT END SUB
FUNCTION cardValue (card$) ' I like bluatigro's method of getting card value IF INSTR(rank$, card$) > 10 THEN cardValue = 10 ELSE cardValue = INSTR(rank$, card$) END FUNCTION
|
|
|
Post by B+ on Jun 6, 2019 6:22:03 GMT
Cut another 20 lines, seems to be running error free and one less pesky prompt to play again:
' BlackJack mini2 'B+ 2019-06-06 see notes below code
global rank$, deckIndex, playerTotal, dealerTotal, BJflag rank$ = "A23456789XJQK" DIM deck$(51) FOR i = 0 TO 51 'create deck deck$(i) = MID$(rank$ + rank$ + rank$ + rank$, i + 1, 1) NEXT points = 100 WHILE points 'the main loop manages the points PRINT "For Blackjack, you have "; points; " points to risk." INPUT "Enter how many you wish to risk (0 quits), risking > "; risk IF risk <= 0 THEN EXIT WHILE IF risk > points THEN risk = points FOR i = 51 TO 1 STEP -1 r = INT((i + 1) * RND(0)) t$ = deck$(i) deck$(i) = deck$(r) deck$(r) = t$ NEXT PRINT call playHand 1 'player IF BJflag THEN BJflag = 0 points = points + 2 * risk PRINT "BlackJack! You win ";2 * risk;", twice your risk." GOTO [BJskip] END IF call playHand 0 'dealer IF playerTotal > 21 OR (playerTotal <= dealerTotal AND dealerTotal < 22) THEN PRINT "You loose.": points = points - risk ELSE PRINT "You win!": points = points + risk END IF [BJskip] PRINT IF points = 0 THEN PRINT "Out of Points!" WEND PRINT "Goodbye"
SUB playHand TFplayer IF TFplayer THEN deckIndex = 0 ELSE IF playerTotal > 21 THEN EXIT SUB 'player lost already END IF FOR i = 1 TO 2 'deal first 2 cards hand$ = hand$ + deck$(deckIndex) deckIndex = deckIndex + 1 NEXT DO IF TFplayer THEN PRINT "Player's hand: "; ELSE PRINT "Dealer's hand: "; total = 0 FOR i = 1 TO LEN(hand$) PRINT MID$(hand$, i, 1); " "; IF INSTR(rank$, MID$(hand$, i, 1)) > 10 THEN cardValue = 10 ELSE cardValue = INSTR(rank$, MID$(hand$, i, 1)) total = total + cardValue IF MID$(hand$, i, 1) = "A" THEN ace = -1 NEXT IF ace AND total < 12 THEN total = total + 10 PRINT " total = "; total IF TFplayer AND total < 21 THEN INPUT "Another card ? (y for yes) "; inp$ IF inp$ = "y" THEN addCard = -1 ELSE addCard = 0 ELSE IF TFplayer AND total = 21 AND LEN(hand$) = 2 THEN BJflag = -1: EXIT SUB IF total < 21 AND total < playerTotal THEN addCard = -1 ELSE addCard = 0 END IF IF addCard THEN hand$ = hand$ + deck$(deckIndex) deckIndex = deckIndex + 1 ELSE IF TFplayer THEN playerTotal = total ELSE dealerTotal = total EXIT DO END IF LOOP WHILE 1 PRINT END SUB
'Code notes: 'started from Bluatigro Blackjack fix B+ mod at JB 2019-06-05 '2019-06-05 Blackjack mimimum, (98 lines) adds code for Player Blackjack paying double risk. '2019-06-06 cut more than 20 lines (to 78) from Blackjack mimimum ' I only use the oneliner CardValue function once so cut and paste one liner to where I use it. 'If I can get rid of hand$() and just use string, I can loose the index for it too and use mid$ and len. 'If I get rid of CLS, I don't have to prompt for playAgain, because can quit at risk declaration time.
' Blackjack Notes for this game: ' "Cards": ' Suits are not displayed. ' X counts as 10 as do J, Q, K designate Jack, Queen, King. ' Ace designated by A is 1 or 11.
' Rules at this "house": ' Since this is one on one, Player against Dealer, Player gets all his cards first and plays out hand. ' If Player is not busted, dealer will play to match or beat Player's hand or bust. ' With ties Dealer wins but a Player's Blackjack will immediately pay double.
|
|
|
Post by tailoredreaction on Nov 29, 2020 18:08:31 GMT
B+,
I wonder if you could help me understand what is going on in this FOR NEXT loop you've created. I am making a similar game where instead of cards I am using 64 boxes of 16 different colors used 4 times. I do not know what this line is doing ...
deck$(i) = MID$(rank$ + rank$ + rank$ + rank$, i + 1, 1)
I think I get what the i + 1, and the 1 part are doing. But I am unsure what the rank$ + rank$ + rank$ + rank$, part is doing. I think I know what the end result is, - the program goes through the rank$ four times to extract a value - but I just don't get why/how that happens. Can you shed a little light on that for me, in plain English. ie, don't assume too much of what I might already know. :-) Comparatively, I know very little.
|
|
|
Post by B+ on Nov 30, 2020 5:53:23 GMT
Hi tailoredreaction , Let's see what rank was: rank$ = "A23456789XJQK" That's one suit or 1/4th of the card deck without suits because in Blackjack you don't really need suits, you just need the rank of the cards. So A = Ace that is 1 or 11, 2-9 are normal number values X represents 10 in Roman numerals, JQK = Jack, Queen, King and valued at 10 like X. I used X because I wanted to keep it all 1 digit or character for simpler code game. OK so 4 of these ranks strung together is the whole deck of cards (taking each letter or digit one at a time): rank$ + rank$ + rank$ + rank$ = "A23456789XJQKA23456789XJQKA23456789XJQKA23456789XJQK" Did you know you can use the + sign to concatenate strings eg, "abc" + "def" + "xyz" = "abcdefxyz" MID$(string$, x, y) pulls section of the string from the whole string$ variable, starting at x for a length of y. So deck$(i) = .... is just loading the deck$() array with single letters, y = 1, the length of the pull. I was using i + 1 in MID$ and i for Deck$ because Deck$ was base 0 and MID$() is always base 1 ie Deck$(0) = MID$( ..., 0+1, ...)
|
|
|
Post by tailoredreaction on Nov 30, 2020 17:35:45 GMT
B+,
This is so helpful. Thank you. After I posted yesterday I started using your shuffling method and had ran it many many times. At some point I remember thinking to myself that it must go back to the start of the string once it reaches the end. It never dawned on me that the rank$ + rank$ + rank$ + rank$ was telling it to join the string together 4 times to be, in essence, one continuous string.
Now that I have the shuffling part down, I have run into a new problem that PROBABLY has something to do with the base1 and base0 thing you've mentioned. I've created an 8 x 8 grid of boxes that are filled with 16 colours (4 of each) randomly. It is essential that it creates just 4 of each, no more no less.
So far it makes 14 x 4, 1 x 3 and the other 1 x 5. The "5" are always "white". White, of course, is the default. So I am thinking I have somehow left out randomly filling in one box, and JB has given it the default colour.
I will spend the day on it if that's what it takes. If the solution still alludes me, I will post the dilemma here tomorrow.
|
|
|
Post by B+ on Nov 30, 2020 20:12:53 GMT
@tailored
Well if you are unfamiliar with getting random integer between x and y (inclusive) then here is a tip:
for i = 1 to 20 'just test the function print randI(5, 1) next
FUNCTION randI(n1, n2) IF n1 > n2 THEN low = n2: hi = n1 ELSE low = n1: hi = n2 randI = INT(RND(0) * (hi - low + 1)) + low END FUNCTION
or random Integer from 0 to x (inclusive) is = INT(RND(0) * (x + 1)) ' most newbies forget the + 1 but INT rounds down! or random Integer from 1 to x (inclusive) is = INT(RND(0) * x) + 1 ' ditto
BTW I like your Avatar, I just realized I could that with my Driver's License but then again enough people are scared to post here ;-))
BTW tsh73 gets credit for pointing me to Fisher-Yates shuffle method, most efficient known to computer scientists. That's another one where the difference between i and i +1 is vital to proper working of algorithm.
Another tip or point (I am shooting in dark your code problem, I can do better with code) Just Basic arrays are 0 based always. If you like 1 based arrays you can handle nItems as DIM myArray(nItems) and just make assignments 1 to nItems and ignore myArray(0) element. I recommend for newbies instead of fiddling with 0 to (nItems -1) arrays so that you don't "waste" any reserved space with DIM statement.
|
|
|
Post by tailoredreaction on Nov 30, 2020 23:54:37 GMT
B+,
Quite clever! :-)
I resolved my issue a few minutes after I posted my last thread. But man, it was TWO problems giving a similar output so it just seemed like a single problem. To compound it, sometimes the problem wouldn't even arise. But luckily for me, that only happened after I found the first issue.
I drew an 8 x 8 grid of boxes all right up snug against the other. I thought I did anyway. One box was missing so after I added the 16 colours randomly, it always showed as a box when in fact it was a gap between the other boxes. And of course it was white, like the background of the window. Easy to figure out once I jotted down the location of the five white boxes and quickly notice one was always white. Checked my list and sure enough there were only 63 boxes listed.
Fixed that and ran my program again.
It gave me 5 blue boxes instead of 4, and one other set of 3 of a random colour each time. This time I was able to see right off the bat that the box I'd drawn in the upper left corner was always coming back as that extra blue one. Its colour is a string variable and depends on what the card shuffler does with my "deck" of 64 "cards" of 16 different colours. You could probably guess that the first box (graphicbox1) carries the colour square$(0). Since the shuffler didn't make me a new square$(0) it just filled in blue, the colour I have set them all to, to start the game.
Your program doesn't need to reassign that last card. You can't shuffle one card. It just is what it is. My program creates values from deck$ "ABCDEFGHIJKLMNOP" - like yours - but mine reassigns each output inside the second loop eg. if deck$(i) = "A" then square$(i) = " green" etc for B, C, D ... to P. Since there is no i = 0 in the shuffle loop, my top lefthand box was filled blue.
|
|
|
Post by B+ on Dec 1, 2020 0:48:57 GMT
Yes, it's been 6 years since coming back to Basic and I am as blunderful as ever! You should see the lu-lu's for today!
|
|