Alan Fletcher
Alan Fletcher

Reputation: 283

NSMutableDictionary and NSMutableArray

Hello I am making a card game for ios, It is the kings drinking game,

To do this I use the following code to fill a dictionary

 cardValues = [[NSMutableArray alloc] init];



for (card1 = 1; card1 <= 13; card1++) {

    card2 = card1;

    [cardValues addObject:[NSNumber numberWithInt:card2]];
    [cardValues addObject:[NSNumber numberWithInt:card2]];
    [cardValues addObject:[NSNumber numberWithInt:card2]];
    [cardValues addObject:[NSNumber numberWithInt:card2]];
}
 dict = [[NSDictionary alloc] initWithObjects:cardValues forKeys:cardKeys];

cardvalues is a NSMutableArray that goes through 1 to 13, 4 times for the 4 suits of cards

cardkeys is a NSArray that is filled with the names of the the 52 cards e.g.

  cardKeys = [[NSArray alloc] initWithObjects:
@"clubace",
@"diamondace",
@"heartace",
@"spadeace",

@"club2",
@"diamond2",
@"heart2",
@"spade2",

and so on through the array.

I am having a problem getting the cardvalues matched to numbers for displaying the rule that follows that card being drawn

For example if the 5 of hearts is drawn I will display a message drink for 5 seconds, the same for any other 5

I was hoping that somone could tell me how i would get this done.

Any help is appreciated

Thanking You

Upvotes: 0

Views: 281

Answers (1)

dreamlax
dreamlax

Reputation: 95395

I think that a better approach would be to have a PlayingCard class that contains both the suit and the value of the card. You can then have a single array containing 52 PlayingCard objects. You can pull any of these 52 PlayingCard objects at random from an array (and/or remove them from the array). The PlayingCard class could also have an UIImage property that you could use to draw the card to the screen.

If you find yourself storing related data in two or more separate containers, chances are you will be better off encapsulating that data in a class and using a single container to store instances of your class.

Upvotes: 2

Related Questions