I’m attempting to represent the basic strategy of a blackjack game as a map with integer keys whose values are a fixed length array of strings.
The keys represent the value of the player’s hand, the array index represents the value of the dealers up card (hence the fixed length array of size 10 corresponding to card values 2-11). The string value in the array at the position corresponding to the dealer’s up card contains the ideal play (stay, hit, split, double).
IE) player hand value is a hard 8, dealer up card is a 2. Basic strategy says the player should hit. To determine this using my map, I would get the array whose key is 8(player hand value = 8) and then looking at the string in array index 0 (Dealer up card = 2).
I’ve attempted to define it this way:
val hardHandBasicStrategy = collection.mutable.Map[Int,Array[String](10)]
but Scala doesn’t seem to like this…
Please help me understand what I’ve done wrong, and/or suggest a way to make it work.
Scala doesn’t have a type that represents arrays of a fixed size. You can either simply use arrays of size ten–this is what is normally done–or, if you want stronger guarantees that it really is size ten, you can construct a ten-long-array-class yourself:
Example:
If you need the the fixed-length array to take a parameter that determines the length, then you should
You won’t have all the collections goodies unless you reimplement them, but then again you don’t want most of the goodies, since most of them can change the length of the array.