I programmed BlackJack, which is also known as 21 or 17 + 4, on a Ti 84
In BlackJack you draw cards and you are not allowed to get over 21 or you have lost. You may say stop, I dont want draw any cards anymore, e.g. when you are at 20.
My program does not just generate Random numbers, it has a real deck, and shuffles it, and when you did draw e.g. the Queen of spades, you cannot draw it anymore again, because it becomes deleted from the deck. Here is the code, I tried to do it easily understandable, I also added comments:
PROGRAM:BLACKJ
:ClrHome
:Disp “Initialising...
:Disp “Pls wait a
:Disp “Second
:“ 2 3 4 5 6 7 8 9 10 JackQueen King Ass→Str1 //each section has 5 letters, if notI added therestwith spaces
:seq(X/4,X,8,59→L1 //creates the deck
:dim(L1→dim(L2
:randIntNoRep(1,dim(L1→L2
:SortA(L2,L1
:randInt(10,25→G //Gives the oppenent a score
:ClrHome
:Disp “Opponent has
:Disp G
:Pause “Points
:0→A
:Lbl 1
:L1(1→B
:A+iPart(B→A //updates the score
:ClrHome
:Disp “You draw a
:iPart(B //the iParts contain the number...
:Output(3,11,sub(Str1,1+(Ans-2)*5,5 //takes out the cardname of Str1
:Disp “of
:32*fPart(B //the fPart contains the type
:Disp sub(“Spades Hearts Clubs Diamonds“,Ans+1,8 //each section=8
:Pause
:ClrHome
:Disp “Your total
:Disp “Score is
:Pause A
:ΔList(cumSum(L1→L1 //deletes the current card from the deck
:If A>20:Goto 2
:Menu(“Draw a card?“,“Sure“,1,“No, thanks“,2
:Lbl 2
:ClrHome
:If G>21
:0→G
:If A>21 or A<G:Then
:Disp “You lost
:Else
:Disp sub(“You won! Only a tie“,1+10(A=G),10
:End
:Pause
Has anyone improvements? Or if you find any bugs, just say it.
Help is highly appreciated.
Edit: I just again found the page, that I got inspired from. You may understand the code better with this, if you dont understand the code yet http: //tibasicdev.wikidot. com/deck-of-cards
Wake me, when you need me.