Learn Courses My Dashboard

Compare values of keys from 2 different dictionaries

I am challenging myself to recreate the war cards game to include a full deck of cards. I want the game to compare random cards and decide which has a higher value.

For an example, here are partial dictionaries for each player:

var playerDeck = [“card 1”:2, “card 2”:2, “card 5”:3, “card 6”:3, “card 9”:4, “card 10”:4, “card 13”:5]

var challengerDeck = [“card 3”:2, “card 4”:2, “card 7”:3, “card 8”:3, “card 11”:4, “card 12”:4, “card 15”:5]

Suppose the random card function chooses “card 5” and “card 15”. How would I write code to compare that? Everything I’ve found on online is about comparing the entirety of both dictionaries. I just want to compare the values of those two individual keys. I have tried using the code with " >", but Xcode does not like it.

Surprisingly, I have everything else figured out. This only thing I can’t get, and, of course, it’s the key to making the game work. But it seems like a really good thing to know.

Any help would be appreciated.

Use that index to access each element of your dictionaries, save that value to a variable

And then compare those two values

Personally I would’ve gone about this in a different way. You can use an enum for your deck of cards with associated values

enum Cards {
   case card1 = 1
   case card2 = 2

So card1 Is the object and 1 is the associate value that you’ll be using to compare the cards. In doing this though I think you need to have the enum conform to Equatable.

But either way do whatever works!

This kinda worked. I wrote the enum as you did, I got an error message. (see screen shot)

So I wrote a switch statement. That seemed to work until I got another error message about expected declaration. (see second screen shot) I looked on line, and tried all the solutions I found, but they only caused more errors.

On the plus side, this is the only error I have now. Also I a now an expert on adding screenshots and videos to my messages. :grinning: And I now know how to zip a file! :zipper_mouth_face:

I am going out of town. So there is no hurry on an answer here. Well actually there never is a hurry. I’ll be tackling this again on Monday.

have a wonder weekend, Mikaela. and again, thanks so much for your help.


Ah but where are the screen shots?

1 Like

OOPS! I guess mi forgot because I got caught up in praising myself. Here ar the screenshots.

Comparing the values of dictionaries is very similar to comparing values in arrays. This is what I came up to (I´m a beginner here, so this might not be the best solution, but it does what Diane53 was asking). Also look at Apple Developer Documentation

There is no need to have different variable names for the cards in the two dictionaries, this is just for showing where the names come from in the comparison.

@Diane53 In your second to last screenshot, where you set up arrays of cards, the naming is inconsistent. Sometimes it’s “card27” without a space between “card” and the number and sometimes it’s “card 28” with a space between “card” and the number. This might get you into trouble for your variable “card 48” but your “case card48”, which is not the same.

@Diane53, the first error—about the raw value— can be solved by putting : Int after your cards declaration. So: enum cards: Int {. This tells the compiler that each member of this enum is backed by an integer value.

(Side note: Type names should be capitalized, so it should be Cards instead of cards.)

The second error—“expected declaration”— is because you can’t have a switch statement at the root level of a class like that. It has to be contained within a method of the class.

The problem is not with the card names, but thanks. They need their own names so they cam be removed from one dictionary and appended to another. I actually have that solved.

Thanks for trying though.

Oh, thanks. I will capitalize cards, but what type of function would I write. Isn’t switch a function? Well kind of? I tried to use init, but Xcode didn’t like that.

I know. I said this inconsistency in naming your variables might get you into trouble at a later stage in your project :wink:

switch is a conditional control flow statement, not a function. It is used within a function to compare one value against some kind of pattern and take different actions depending upon the result of the comparison.

What type of function you put it in depends entirely on what the purpose of the switch statement is. You don’t show your entire code here so we can’t see the whole switch or what it is you are trying to do with it, so it’s hard to say what the function you should put it in should be like. You need to think about what it is you were trying to accomplish with that switch and then figure out the function from that.

Thanks, Rooster.What I’m trying to do is to find a way to assign a value to the cards in the array without using a dictionary. I need to compare the values during the game to see who wins the round. The winner will get 1 point and take the loser’s card. I have that part figured out, but it is dependent on the card values.

Here is what happens when I try to assign the switch statement to function.