Microsoft Small Basic

Program Listing: HTX435
cardsRemaining = 52 'initVariable
start:
TextWindow.Write("Deal a card? y ")
beginProgram = TextWindow.Read()

If beginProgram = "y" Then
CreateDeck()
ShuffleDeck() ' this is the bug, shuffles 1st run but wont start next run
deal = "y"
While deal = "y" And cardsRemaining > 1
DealCard()
CalculateRunningStats()
ShowStats()
cardsRemaining = Array.GetItemCount(card) '-1 so don't deal last card
If cardsRemaining > 1 Then
TextWindow.Write("Deal another card? y ")
deal = Text.ConvertToLowerCase(TextWindow.Read())
Else
TextWindow.Write("Deal last card? y ")
deal = Text.ConvertToLowerCase(TextWindow.Read())
DealCard()
EndIf
EndWhile
TextWindow.Write("this should = 52: ")
TextWindow.WriteLine(52-Array.GetItemCount(card))
CalculateRunningStats()
CalculateFinalStats()
ShowStats()
TextWindow.Write("Play Again? y ")
beginProgram = Text.ConvertToLowerCase(TextWindow.Read())
Else
TextWindow.WriteLine("the end")
TextWindow.Read()
Program.End()
EndIf
Goto start

Sub CreateDeck
For i= 1 To 13 'create full deck
card[i*4-3] = "bS" + i
card[i*4-2] = "bC" + i
card[i*4-1] = "rH" + i
card[i*4-0] = "rD" + i
EndFor
EndSub

Sub ShuffleDeck
i=1
While i <= Array.GetItemCount(card)
rand = Math.GetRandomNumber(Array.GetItemCount(card))
If Array.ContainsValue(val, rand) = "False" Then
val[i] = rand
shuffled[i] = card[val[i]]
i = i+1
EndIf
EndWhile
TextWindow.WriteLine(shuffled)
EndSub

Sub DealCard 'pick card and then get indices in the form of a new array i.e. (w/o the deleted value)
dealIndex = Math.GetRandomNumber(Array.GetItemCount(card))
deltCard[53-Array.GetItemCount(card)] = card[dealIndex]
card[dealIndex] = "" 'remove card from Card array
newDeck = Array.GetAllIndices(card) 'only gets indices with a value, not indices whose value is deleted
For j=1 To Array.GetItemCount(newDeck)
newDeck[j] = card[newDeck[j]] 'assigns value to the newArray
EndFor
card = newDeck
EndSub

Sub CalculateRunningStats 'WIP is ok
'TextWindow.Clear()
If Array.GetItemCount(card) = 51 Then
blackCount = 0 'reset for showStats sub
redCount = 0
EndIf
For s = 1 To 52-Array.GetItemCount(card)
TextWindow.Write("deltCard "+ s +" ")
TextWindow.WriteLine(deltCard[s])
EndFor
If Text.StartsWith(deltCard[52-Array.GetItemCount(card)], "b") = "True" Then
black = 1
red = 0
Else
black = 0
red = 1
EndIf
blackCount = blackCount + black
redCount = redCount + red
chanceOfBlack = (26-blackCount)/Array.GetItemCount(card)
chanceOfRed = (26-redCount)/Array.GetItemCount(card)
EndSub

Sub ShowStats
TextWindow.WriteLine("blackCount = " + blackCount)
TextWindow.WriteLine("redCount = " + redCount)
TextWindow.Write("Probabilty next card is Black is: " +chanceOfBlack)
TextWindow.WriteLine(" Or Red is: " +chanceOfRed)
EndSub

Sub CalculateFinalStats
If Array.GetItemCount(card) = 0 Then
TextWindow.WriteLine("there's NO cards left - final stats")
EndIf
EndSub