;---------------------------------------------------> ;+ ;NAME: ; BLACKJACK ; ;PURPOSE: ; Play the Black Jack card game. ; ;CALLING SEQUENCE: ; BLACKJACK ; ;REQUIRED INPUTS: ; none ; ;KEYWORDS: ; none ; ;OUTPUTS: ; none ; ;EXAMPLE: ; IDL> blackjack ; IDL begins the game engine. Follow the command line prompts. ;HISTORY: ; Written - Paul Higgins - 17 January 2008 ;- ;---------------------------------------------------> ;--<< Generates a random integer between 1 and 13. function blackjack_gencard card=round(randomu(seed)*12)+1 return, card end ;---------------------------------------------------------------> ;--<< Checks to see if there is an Ace in the hand. (Aces are 1.) function blackjack_acecheck, cards wace=where(cards eq 1) if wace[0] ne -1 then hasace=1 else hasace=0 return, hasace end ;---------------------------------------------------------------> ;--<< Returns the name (a string) of the card, where the card is ;--<< an integer between 1 and 13. function blackjack_whichcard, card case 1 of card eq 1: cardname = 'Ace' card lt 11 and card gt 1: cardname = strcompress(card,/remo) card eq 11: cardname = 'Jack' card eq 12: cardname = 'Queen' card eq 13: cardname = 'King' endcase return, cardname end ;---------------------------------------------------------------> ;--<< Assigns an integer value to each card based on its index (1->13) function blackjack_cardval, card case 1 of card eq 1: cardval = 1 card gt 10 and card lt 14: cardval = 10 card gt 1 and card lt 11: cardval = fix(card) endcase return, cardval end ;---------------------------------------------------------------> ;--<< Sum the values of the cards in the hand. Accounts for Aces (1 or 11). function blackjack_sumcards, cards numcards = n_elements(cards) sum = 0 for i = 0,numcards-1 do begin sum = sum+blackjack_cardval(cards[i]) endfor haveace=blackjack_acecheck(cards) if haveace then begin sum=sum+10 if sum gt 21 then sum=sum-10 endif return, sum end ;---------------------------------------------------------------> ;--<< Lists the cards in a hand. If listing the dealers hand, one ;--<< card is left face down. But, if ENDCOUNT is TRUE then it ;--<< lists all of the cards. pro blackjack_listcards, cards, you=you, endcount=endcount if keyword_set(you) then msg='You have ' else msg='Dealer has ' numcards = n_elements(cards) startshow=0 if not keyword_set(endcount) and not keyword_set(you) then startshow=1 print, msg+strcompress(n_elements(cards),/remo)+' cards:' if not keyword_set(endcount) and not keyword_set(you) then print,'??' for i=startshow,numcards-1 do begin print, blackjack_whichcard(cards[i]) endfor print,' ' wait,1 return end ;---------------------------------------------------------------> ;--<< The dealers AI, so to speak. It uses IMPULSE, a random integer ;--<< between 1 and 3 to determine how risky the AI is. Then based ;--<< on that, and the sum of the hand, the dealer either HITs or not. function blackjack_dealerhit, sum impulse = round(randomu(seed)*2)+1 dhit=0 if sum lt 12 then dhit=1 if sum gt 11 and sum lt 15 and impulse gt 1 then dhit=1 if sum gt 14 and sum lt 19 and impulse gt 2 then dhit=1 return, dhit end ;---------------------------------------------------------------> ;--<< The Black Jack program. it is essentially 2 while loops. The ;--<< outer contains the "rounds" of gameplay. If you run out of money ;--<< or choose to quit, it BREAKS the loop. The inner loop contains ;--<< the "turns" of a round. You break out of the loop by deciding to ;--<< HOLD, CALL, or you or the dealer BUSTs. pro blackjack chips = 0 loadgame='' savedgame='' print,' ' print,'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX' print,' ' print,'Interactive Data BlackJack Version 1.0 - By: Paul Higgins' print,' ' print,'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX' print,' ' print,'Would you like to load a saved game? (Y/N)' read,loadgame print,' ' if strlowcase(loadgame) eq 'y' then begin savedgame=findfile('blackjack_savegame.sav') if savedgame eq '' then print,'No saved game found!' endif print,' ' ;--<< If no savedgame exists, player is prompted for initial bankroll. if savedgame ne '' then restore,savedgame else begin print, "Welcome to the table, how many chips ($) would you like?" print, ' ' print, 'Enter ammount..." read, chips chips = round(double(chips)) if chips eq 0 then chips=1 endelse ;--<< Initialize game switches. gameover = 0 bet=0D round=1 ;BEGINNING OF THE PLAYING SEQUENCE-----------------------> ;Outer WHILE loop. while not gameover do begin print,' ' print,'ROUND #'+strcompress(round,/remo)+'----------------------------->' print,' ' wait,1 ;--<< Prompts player for bet. print, 'You have: $'+strcompress(chips,/remo)+'.' print, 'Place your bet...' read,bet bet = round(bet) if bet eq 0 then bet=1 ;--<< Check to see if player can afford bet. while bet gt chips do begin print, 'I dont think so, man! Bet what youve got...' print, 'Place your bet...' read,bet endwhile print, 'You have bet $'+strcompress(bet,/remo)+'.' print, ' ' print, 'Dealing...' print,' ' ;--<< Generate initial hands for player and dealer. ucards=[blackjack_gencard(), blackjack_gencard()] hcards=[blackjack_gencard(), blackjack_gencard()] ;--<< Initialize game switches for inner WHILE loop. hit = 1 hhit = 1 loser=0 turn=1 hbust=0 draw=0 blackjack_listcards, ucards, /you print,' ' blackjack_listcards, hcards ;--<< Begin inner WHILE loop. Breaks if both players HOLD. while hit eq 1 or hhit eq 1 do begin print,' ' print,'TURN #'+strcompress(turn,/remo)+'----------------------------->' print,' ' wait,1 ;--<< How many cards does each player have? numucard = n_elements(ucards) numhcard = n_elements(hcards) ;--<< What is sum of card values? utotal=blackjack_sumcards(ucards) htotal=blackjack_sumcards(hcards) ;--<< Handles player HITing. Prompts for HIT, then if true, adds card. if hit eq 1 then begin print,' ' print, 'Would you like to hit? (Y/N) hityn='' read,hityn if strlowcase(hityn) eq 'y' then begin print,' ' print,'Hit me!!' wait,1 ucards=[ucards,blackjack_gencard()] utotal=blackjack_sumcards(ucards) hit=1 print,' ' blackjack_listcards, ucards, /you endif else hit=0 endif ;--<< Check to see if player busts. if utotal gt 21 then begin loser=1 print, 'You BUST!' print,' ' wait,1 blackjack_listcards, ucards,/you blackjack_listcards, hcards,/endcount break endif if hhit eq 1 then begin print,'Dealer is thinking...' print,' ' wait,1 if n_elements(hhit) eq 1 then begin ;--<< Runs Dealer AI. Dealer has TRUE or FALSE answer. hhit=blackjack_dealerhit(htotal) if hhit then print,'Dealer HITS.' else $ print, 'Dealer holds.' print,' ' wait,1 endif ;--<< Adds card to dealer hand. if hhit then begin hcards=[hcards,blackjack_gencard()] blackjack_listcards, hcards endif endif ;--<< Checks to see if Dealer Busts. htotal=blackjack_sumcards(hcards) if htotal gt 21 then begin print, 'Dealer BUSTS!' print,' ' wait,1 blackjack_listcards, ucards,/you blackjack_listcards, hcards,/endcount hbust=1 break endif ;--<< If both players HOLD, the loop ends. if hhit eq 0 and hit eq 0 then begin print,' ' print,'CALL!!' print,' ' wait,1 blackjack_listcards, ucards,/you blackjack_listcards, hcards,/endcount endif turn=turn+1 endwhile ;--<< Determine if Win, Lose, or Draw. if loser eq 0 and hbust eq 0 then begin utotal=blackjack_sumcards(ucards) htotal=blackjack_sumcards(hcards) ;--<< Compares Number and Value of cards in each hand, and the highest card. if utotal lt htotal then loser=1 if utotal eq htotal then begin if n_elements(ucards) gt n_elements(hcards) then loser=1 if n_elements(ucards) eq n_elements(hcards) then begin if max(ucards) lt max(hcards) then loser=1 if max(ucards) eq max(hcards) then draw=1 endif endif endif ;--<< Check for BlackJack hand. blkjhand=0 if htotal eq 21 and n_elements(hcards) eq 2 then blkjhand=1 if utotal eq 21 and n_elements(ucards) eq 2 then blkjhand=1 if blkjhand eq 1 then begin print,'BLACKJACK!' print,' ' endif ;--<< Handle Winning Losing and Drawing with logic statements. if loser then print, 'You LOSE!!' if draw then print, 'DRAW!' else begin if not loser then print, 'You WIN!' endelse ;--<< Gain money if you win, decrease if you Lose, do nothing if you Draw. if not draw then begin if loser then chips = chips-bet else chips=chips+bet endif ;--<< Reset all of the switches at end of Round. loser=0 hit=1 hhit=1 draw=0 print, 'You have: $'+strcompress(chips,/remo)+'.' if chips le 0 then begin print,' ' print, 'Youre outa cash!' print,' ' print, 'Game Over...' print,' ' return endif print, 'Play again? (Y/N)' playagain='' read,playagain if strlowcase(playagain) eq 'n' then gameover=1 round=round+1 endwhile ;END OF THE PLAYING SEQUENCE-----------------------> print, 'Chicken!!' print, ' ' print, 'Would you like to SAVE your game? (Y/N)' savegame='' read, savegame if strlowcase(savegame) eq 'y' then begin save,chips,filename='blackjack_savegame.sav' print,' ' print,'Game saved as: '+'blackjack_savegame.sav in:' spawn,'pwd',pwdmsg print,pwdmsg endif print,' ' print, 'Better luck next time!' end ;--------------------------------------------------------------->