0

I have trouble displaying the cards that have been drawn in hand. I can see the cards in text form in "All Output" but they don't show visually. An example of a card's name is "club04", "spades06", "heart13", etc. 11-13 is for Jacks to Kings.

This is part of ViewController.m:

-(void) showHand:(Hand *)hand {
    int ypos = 40;

    for (int i = 0; i < [hand countCards]; i++) {
        Card *card = [hand getCardAtIndex:i];

        // UIImage  *cardImage = [UIImage imageNamed:@"heart08.png"];
        UIImage  *cardImage = [UIImage  imageNamed:[card filename]];

        UIImageView *imageView = [[UIImageView alloc] initWithImage:cardImage];
        CGRect arect = CGRectMake((i * 40) + 20, ypos, 71, 96);
        imageView.frame = arect;

        [self.view addSubview:imageView];
    }
}
@end

This is Hand.h

#import <Foundation/Foundation.h>

@class Card;

@interface Hand : NSObject {

}

@property NSMutableArray *cardsInHand;

-(void) addCard:(Card *)card;
-(NSInteger) getPipValue;
-(NSInteger) countCards;
-(Card *) getCardAtIndex:(NSInteger) index;



@end

This is Hand.m

#import "Hand.h"
#import "Card.h"

@implementation Hand

- (id) init {
    if (self = [super init]){
        self.cardsInHand = [[NSMutableArray alloc] initWithCapacity:6];
    }
    return (self);
}

- (NSInteger)countCards {
    return ([self.cardsInHand count]);
}

- (void)addCard:(Card *)card {
    [self.cardsInHand addObject:card];
}

- (NSInteger)getPipValue {
    NSInteger aValue = 0;
    for (Card *tCard in self.cardsInHand) {
        aValue = aValue + [tCard pipValue];
    }
    return aValue;
}

- (Card *)getCardAtIndex:(NSInteger)index {
    return ([self.cardsInHand objectAtIndex:index]);
}

- (NSString *)description {
    return [NSString stringWithFormat:@"cards in hand : %@", self.cardsInHand];
}

@end

Funny thing is that when I type manually a single card I get to see the cards. I just see 6 times the same card.

But "All Output" displays what I need to see. Example:

2013-04-21 19:18:41.025 blackjack[1395:c07] Dealer:cards in hand : (
    "Club  5 (pipValue = 0)",
    "Spade  Jack (pipValue = 1)",
    "Club  8 (pipValue = 0)",
    "Spade  Queen (pipValue = 1)",
    "Diamond  King (pipValue = 1)",
    "Heart  7 (pipValue = 0)"
)

this is the part that puts the cards in the hands and table:

-(id) init {
if ((self = [super init])){
    _deck = [[Deck alloc] init];
    _playerHand = [[Hand alloc] init];
    _dealerHand = [[Hand alloc] init];
    _table = [[Table alloc] init];
}
return (self);
}

-(void)setup
{
//deal cards
[_playerHand addCard:[_deck drawCard]];
[_dealerHand addCard:[_deck drawCard]];
[_playerHand addCard:[_deck drawCard]];
[_dealerHand addCard:[_deck drawCard]];
[_playerHand addCard:[_deck drawCard]];
[_dealerHand addCard:[_deck drawCard]];
[_playerHand addCard:[_deck drawCard]];
[_dealerHand addCard:[_deck drawCard]];
[_playerHand addCard:[_deck drawCard]];
[_dealerHand addCard:[_deck drawCard]];
[_playerHand addCard:[_deck drawCard]];
[_dealerHand addCard:[_deck drawCard]];
[_table addCard:[_deck drawCard]];
[_table addCard:[_deck drawCard]];
[_table addCard:[_deck drawCard]];
[_table addCard:[_deck drawCard]];

this is card.m:

#import "Card.h"

@implementation Card

@synthesize suit = _suit, cardNumber=_cardNumber;

-(id) initWithCardNumber:(NSInteger)aCardNumber suit:(Suit)aSuit {
if (self = [super init])
{
    _suit = aSuit;
    _cardNumber = aCardNumber;

}
return self;
}

/*
 ** pipValue will return the pipValue of a card.
 **          2 to 9 will return 0.
 **          an ace will always return 1
 **          Jack, Queen and King will always return 1.
 **          2 of Clubs will return 1
 **          10 of diamonds will return 2
 */
-(NSInteger) pipValue {

if (_suit==Diamonds && _cardNumber==10)
    return (2);
else if (_cardNumber>=10)
    return (1);
else if (_cardNumber==1)
    return (1);
else if (_suit==Clubs && _cardNumber==2)
    return (1);
else
    return (0);

}

/*
 ** private method
 ** returns the suit as a String
 */
-(NSString *) suitAsString{
switch (_suit) {
    case Hearts:
        return @"Heart ";
        break;
    case Spades:
        return @"Spade ";
        break;
    case Diamonds:
        return @"Diamond ";
        break;
    case Clubs:
        return @"Club ";
        break;
    default:
        return nil;
        break;
}
}

/*
 ** private method
 ** returns the cardNumber as a String
 */
-(NSString *) cardNumberAsString{
switch (_cardNumber) {
    case 1:
        return @"Ace";
        break;
    case 11:
        return @"Jack";
        break;
    case 12:
        return @"Queen";
        break;
    case 13:
        return @"King";
        break;
    default:
        return [NSString stringWithFormat:@"%d", _cardNumber];
        break;
}
}

-(NSString *) filename{
return ([NSString stringWithFormat:@"%@%02d.png", [self suitAsString], [self cardNumber]]);
}

-(NSString *) description {
return [NSString stringWithFormat:@"%@ %@ (pipValue = %d)", [self suitAsString], [self cardNumberAsString], [self pipValue]];
}


@end

and also deck.m:

#import "Deck.h"
#import "Card.h"

@implementation Deck

-(id) init {
if (self = [super init])
{
    cards = [[NSMutableArray alloc] init];

    for (int suit = 0; suit <= 3; suit++)
    {
        for (int cardNumber = 1; cardNumber <= 13; cardNumber++)
        {
            [cards addObject:[[Card alloc] initWithCardNumber:cardNumber suit:suit]];
        }
    }

    [self shuffle];
}
return self;
}

-(Card *) drawCard {
if ([cards count]>0)
{
    Card* tCard = [cards lastObject];
    [cards removeLastObject];
    return tCard;
}
return nil;
}

-(void) shuffle {
NSUInteger count = [cards count];
for (NSUInteger i = 0; i < count; ++i) {
    // Select a random element between i and end of array to swap with.
    int nElements = count - i;
    int n = (arc4random() % nElements) + i;
    [cards exchangeObjectAtIndex:i withObjectAtIndex:n];
}
}

- (NSString *)description{
return [NSString stringWithFormat:@"Deck : %@", cards];
}

@end

2 Answers2

0

if @"heart08.png" works and [card filename] doesn't then you should start by printing out what [card filename] is returning.

And what is "All output"?

Craig
  • 8,093
  • 8
  • 42
  • 74
  • console has a selection; All Output, Debugger Output and Target output. I guess OP is referring to that. He means console :) – Bartu Apr 21 '13 at 20:26
  • Ah, I thought All Output was something you invented. By print out I mean log. Try this: `NSLog(@"File name for card %d is %@),i, [card filename]);` just before you set `cardImage = ....` – Craig Apr 22 '13 at 00:33
  • hmmm it prints out: 2013-04-22 12:57:44.271 blackjack[371:c07] File name for card 24242 is (null) (lldb) – Gus Daniel Apr 22 '13 at 09:58
  • So that tells me that you have 24242 cards (which sounds very odd) and that your card's filename has not been set. Since your `filename` function looks like it should at least return ".png" I have to guess your hand has no actual cards. Can you share your code for populating the `hand`? – Craig Apr 22 '13 at 10:49
  • I mean the part of your code that puts the cards into the hand. – Craig Apr 22 '13 at 11:14
  • You are still missing critical information. The point of my requests is to work out why `[card filename]` is returning null. To do that we need to see how `card` is created. What you've shown is that something that comes out of `[_deck drawCard]` goes into `[_dealerhand addCard:card]`. But what is in drawCard? how is the original `card` created? Have you done any testing to check that `drawCard` actually returns a valid card? You could comment out most of `setup`, called `drawCard` once and check what the `filename` is. – Craig Apr 22 '13 at 20:23
  • One problem you're going to have is the `filename` method looks like it would return "Heart 08.png" not "heart08.png" and "Heart 13.png" not "heartKing.png" which you might be expecting. After that it's up to your debugging skills. First check if the deck is initialised correctly, does it have all the cards. Then check if drawCard is returning a valid card, then check if a valid card returns the filename you expect. – Craig Apr 23 '13 at 08:13
0

In your code i think you are using a random function this is the problem of the random function. It works like a pool and use some time interval if it gets the same the it will show the same result. you should pass the current time's mili section to get the random value n this prob will be solved

Divyam shukla
  • 2,038
  • 14
  • 18
  • I've looked over that code a few times, I can't see the random function. Where is it? – Craig Apr 22 '13 at 07:07
  • the random is in Deck.m **`-(void) shuffle { NSUInteger count = [cards count]; for (NSUInteger i = 0; i < count; ++i) { // Select a random element between i and end of array to swap with. int nElements = count - i; int n = (arc4random() % nElements) + i; [cards exchangeObjectAtIndex:i withObjectAtIndex:n]; } }`** – Gus Daniel Apr 22 '13 at 11:08