Struggling with blackjack / dictionary help

So my code here is for the very beginning of a blackjack drawing system. I’m currently struggling with when the random key is grabbed, the related value will also be grabbed and added to a separate variable. The drawing works fine, but I need to make the value of each card work. Is there a way to grab the same random key and the corresponding value at once?

There's two options here that you could consider.

First, App Inventor will treat a dictionary passed to a list block as a list of pairs where the first element in the pair is the key and the second element is the value. In the case, you would say pick a random item get global Rank and then that would give you a random (key, value) pair you can unpack with the select item in list block.

Alternatively, once you have picked a random key, use the get value for key in dictionary block to get the corresponding value to the randomly chosen key.

You already have the random key, put it to a variable before making the label.

You will need to use list of cards for the deck (boot) and for the player hand.

Search this board for 'shuffle'

Also seek FAQ lists

I thought about using the “get value for key in dictionary” block, but I had the problem that I couldn't use the same random item in the list for the text and for the score. How would I make it to where it grabs the value for the random key used prior instead of a new random key?

Looking at your DrawButton, I see you do a text join of the name and suit of the card when you randomize it.

I assume you will add it to the list of cards in the player's hand.

When you encounter that card (piece of text) later, when adding up the points in the player's hand (list), you can extract the name of the card ("Ace", King",...) from the text by passing it through a "split at ' of ' " block. That will return a list of length 2, with the card name in item 1 and the suit in item 2.

You can then do the lookup in the dictionary using item 1.

In case you have not done so yet, see the intro topics in

I changed my code to better fit my new vision for the drawing system, but for the split text, how do I make it to where instead of it splitting a specific card (King of Diamonds is a placeholder) it splits the last card added to the player’s cards. The rest works though, thank you.

You left out the step where you look up the card value in the dictionary after getting the card name.

This is a good place to use a value procedure to consolidate the logic for calculating the points of a card.

To get the total score of a hand (list) clear a subtotal variable to 0 then loop through the list with a For Each Item in List block, adding that score to the subtotal.

Please read chapters 19, 20, 21 of

There are two ways to do this...

Use an extra variable called latestCard for the latest card before adding it to the list, and run that through the scoring logic and add it to a running subtotal

Alternatively, rescore the player hand with a for each loop as described in an earlier post. This is simpler if you do it in another value procedure handScore.

For sample blocks, export and post an aia file.

Also see

The first two advanced list blocks are good for your scoring problem .

The Length of List block is good for indexing the item at the end of a list.

How are you currently storing the card names and values in the dictionary? If the key and value are paired, wouldn’t saving the randomly selected key first and then using it to retrieve the value be the simplest approach? :thinking:

I would regard that approach as premature optimization.

It imposes an extra burden on the developer to maintain the value of extra variables during the process of building a hand.

I prefer a decision hiding approach, where decisions are hidden in procedures like

Value of a card

Value of a hand (sum of card values)

A more "technically correct" approach would really be to store the cards internally as their own data structure and then render them into the UI rather than working backward from the written representation to the data representation. For example, a card might be the pair (4, "H") and then a hand is a list of those pairs. Depending on whether the order is important, it might be easier to push the items onto the front of the list so that the newest card in the hand is always the first element, treating it more like a stack than a list. To update your UI, you would introduce a procedure that would take a pair and use the reverse of the dictionary you've built to turn its number (4) into the written word "Four" and "H" into "Hearts" and join the strings together. This internal representation is particularly valuable if you ever want to internationalize the app later. How would your algorithm as written have to change to show the app in another language like Spanish or Chinese? Having the model of the world separate from how it is presented to the end user usually makes this much easier.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.