Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Mockup & CodeGen, iPhone & iPad
($9.99)

Make your own iPhone apps
and run them live!
(free)

Manu
($0.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 03-13-2009, 12:14 AM   #1 (permalink)
Registered Member
 
Join Date: Mar 2009
Posts: 174
Default deck of cards class... no errors.. review?

here is my card and deck class... it is working in simulator... I am c# programmer... is there anything that might blow up a real iphone or get rejected by app store???

thanks... code follows..
Code:
//
//  card.m
// 
//
//  Created by Admin on 2/10/09.
//  Copyright 2009 __MyCompanyName__. All rights reserved.
//

#import "card.h"


@implementation card
-(void) setRank: (int) n {
    rank = n;
}
-(int)rank{
	return rank;
}
-(void) setSuit: (NSString *) s {
    suit = s;
}
-(NSString *)suit{
	return suit;
}

@end


--------

//
//  deck.m
// 
//
//  Created by Admin on 2/10/09.
//  Copyright 2009 __MyCompanyName__. All rights reserved.
//

#import "deck.h"


@implementation deck
-(id) init {
    self = [super init];
	maincardlist =  [[NSMutableArray alloc] init]; 
	cardlist = [[NSMutableArray alloc] init]; 
	shuffledlist = [[NSMutableArray alloc] init]; 
	return self;
}
-(void) setupDeck{
	int i;
	
	for(i=1; i < 14; i++)
	{
		card *c;
		c = [[card alloc]init];
		[c setRank:i];
		[c setSuit:(NSString *)"d"];
		[cardlist addObject:c];     
	}
	for(i=1; i < 14; i++)
	{
		card *c;
		c = [[card alloc]init];
		[c setRank:i];
		[c setSuit:(NSString *)"h"];
		[cardlist addObject:c];     
	}
	for(i=1; i < 14; i++)
	{
		card *c;
		c = [[card alloc]init];
		[c setRank:i];
		[c setSuit:(NSString *)"c"];
		[cardlist addObject:c];     
	}
	for(i=1; i < 14; i++)
	{
		card *c;
		c = [[card alloc]init];
		[c setRank:i];
		[c setSuit:(NSString *)"s"];
		[cardlist addObject:c];     
	} 
	int numcardsleft = 52;
	card *c;
	int x;
	for(x=0;x<52;x++)
	{
		c = [cardlist objectAtIndex:x];   
		[maincardlist addObject:c];
	}
	
	while(numcardsleft>1)
	{
		x =  random() % (numcardsleft); 
				
		c = [cardlist objectAtIndex:x];   
		[shuffledlist addObject:c];
		[cardlist removeObjectAtIndex:x];
		numcardsleft--;
	}
		c = [cardlist lastObject];
		[cardlist removeLastObject];
		[shuffledlist addObject:c];

	c = [shuffledlist lastObject];
	[shuffledlist removeLastObject];
	[self setCurrentCard:c];

}

-(void) shuffleDeck{
	[cardlist release];
	cardlist = [[NSMutableArray alloc] init]; 
	int numcardsleft = 52;
	card *c;
	int x;
	for(x=0;x<52;x++)
	{
		c = [maincardlist objectAtIndex:x];   
		[cardlist addObject:c];
	}
	[shuffledlist release];
	shuffledlist = [[NSMutableArray alloc] init]; 
	
	while(numcardsleft>1)
	{
		x =  random() % (numcardsleft); 
		
		c = [cardlist objectAtIndex:x];   
		[shuffledlist addObject:c];
		[cardlist removeObjectAtIndex:x];
		numcardsleft--;
	}
	c = [cardlist lastObject];
	[cardlist removeLastObject];
	[shuffledlist addObject:c];
	
	c = [shuffledlist lastObject];
	[shuffledlist removeLastObject];
	[self setCurrentCard:c];
	
}
-(void)gotoNextCard{
	[self setCurrentCard:[shuffledlist lastObject]];
	[shuffledlist removeLastObject];	
}

-(void) setCurrentCard: (card *) c {
    currentCard = c;
}

-(card *)currentCard{
	return currentCard;
}

@end
will.ray is offline   Reply With Quote
Old 03-13-2009, 01:12 AM   #2 (permalink)
Pro. Game Developer
iPhone Dev SDK Supporter
 
Join Date: Feb 2009
Location: żLa Islas Hermosas?
Posts: 2,178
Default

Without knowing what's in the .h files, it's nearly impossible to perform an accurate review. At first glance, you're probably leaking memory. I see lots of alloc/init code an not a single release in sight.
Kalimba is offline   Reply With Quote
Old 03-13-2009, 01:21 AM   #3 (permalink)
Registered Member
 
Join Date: Mar 2009
Posts: 174
Default

Quote:
Originally Posted by Kalimba View Post
Without knowing what's in the .h files, it's nearly impossible to perform an accurate review. At first glance, you're probably leaking memory. I see lots of alloc/init code an not a single release in sight.
thanks kalimba, would it be appropriate if i create a destructor and do all my releases there? thx
will.ray is offline   Reply With Quote
Old 03-13-2009, 01:22 AM   #4 (permalink)
Registered Member
 
Join Date: Mar 2009
Posts: 174
Default

o, and here are my .h files... thx again

Code:
//
//  card.h
// 
//
//  Created by Admin on 2/10/09.
//  Copyright 2009 __MyCompanyName__. All rights reserved.
//

#import <Foundation/Foundation.h>


@interface card : NSObject {
	int rank;
	NSString *suit;
}
-(void)setRank:(int) d;
-(int) rank;
-(void)setSuit:(NSString *) s;
-(NSString *) suit;

@end

next..

Code:
//
//  deck.h
//  
//
//  Created by Admin on 2/10/09.
//  Copyright 2009 __MyCompanyName__. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "card.h"

@interface deck : NSObject {
	@private
	card *currentCard;
	NSMutableArray *maincardlist;
	NSMutableArray *cardlist;
	NSMutableArray *shuffledlist;
}
-(void)setupDeck;
-(void)shuffleDeck;
-(void)gotoNextCard;
-(void)setCurrentCard:(card *) c;
-(card *) currentCard;
@end
will.ray is offline   Reply With Quote
Old 03-13-2009, 10:34 AM   #5 (permalink)
Registered Member
 
Join Date: Mar 2009
Posts: 174
Default

bump. ... anyone? beuler?
will.ray is offline   Reply With Quote
Old 03-13-2009, 11:01 AM   #6 (permalink)
Tylenol is my friend
 
Join Date: Oct 2008
Location: Midwest
Posts: 150
Default

I have built a few deck and card objects in my past.
I have started and restarted a card game program many times.

Some suggestions:

First: I am assuming this is for a standard deck of cards: Poker playing deck, 4 suites (heart, diamond, club, spade), 13 cards (2 - A), with possible of jokers?

I would recommend the following:

Have a TypeDef Enum file, for standard things:
-) Suit Type
-) Rank Value

That way it makes it a LOT easier to compare Ranks (since you can use simple == compares),

It makes it a LOT easier to sort Ranks (since you can use integer sort option, and making a case for the Ace being top or bottom)

It makes it a LOT easier to compare Suit and match them up.
For Suit, no need to retain since it is an integer value.


- Coding

You need to have a [card release] after you add it to one of your arrays.
The array will handle the final release, and you then need to release those array's in your dealloc.

If you are going to build the deck in a loop like that, and if you do the enums above, next the loop. So you have a loop inside the loop, instead of four sections repeated. Just makes the code easier to read (IMHO)

When you are building your "maincard" array.
Look at using the ArrayWithArray function, instead of looping again.

With the MutableArrays, you know the size you need. Pre-allocate them the space (initWithCapacity)

Highly recommend you do some research on the theories on the most accurate random shuffles (7 Split/Rift shuffle is considered the statistically the most random). Your current method may result in a fair enough shuffle, but it can repeat and be figured out.

Some other things you may consider, since I don't know what your end game is for this class.

How about an image method for the card and the deck.
The card image would be related to the suit and rank, the deck would be the backface of the card.

Depending on what your game is your building, do you have to worry about wild cards such as Jokers or "one-eye jacks"

When you ultimately build your "hand" object, make sure you are doing your retain and releases properly, so that you don't completely release your card from memory, as the deck should always be the owner of the card object)

On the surface, building a simple deck of cards seems to be a trivial process. But once you start digging into it, to build it right, it takes times a lot of planning.

If you haven't done so, get a deck of cards and really think about everything that can be done with a deck during a game. How people interact with the cards during the game.

Draw out an object flow diagram, and get all your linking and flow down on paper, it will save you a lot of time in the coding process.

Good luck.
__________________
-----
-) No matter what forum you join, you always start as a newbie. Even if you own the board.
-) There is no stupid question, if you think it is stupid don't answer it and it will fall of the screen
-) If you post some helpful solution, then that could lead to more helpful solutions. Soon, it is a chain reaction and then the forum becomes a monumental programming resource.
Fontano is offline   Reply With Quote
Old 03-13-2009, 01:32 PM   #7 (permalink)
Registered Member
 
Join Date: Mar 2009
Posts: 174
Default thanks fontano... you make some good points.

you made some good points.. i am doing some of what you say on the window code... I forgot to mention you need to seed the random generator with

srandom(time(NULL)); //seed random num generator

in your viewdidload ... i did it there, it doesn't matter as long as it gets called before you try to get a random number.
will.ray is offline   Reply With Quote
Old 04-13-2009, 11:35 AM   #8 (permalink)
New Member
 
Join Date: Apr 2009
Posts: 1
Default Additional Help

Quote:
Originally Posted by will.ray View Post
you made some good points.. i am doing some of what you say on the window code... I forgot to mention you need to seed the random generator with

srandom(time(NULL)); //seed random num generator

in your viewdidload ... i did it there, it doesn't matter as long as it gets called before you try to get a random number.

I am new to Objective C and XCode and this is a great learning example. I am having some trouble compiling with the Typedef enums in there. Any additional pointers would be great.
hlcole is offline   Reply With Quote
Old 04-13-2009, 11:56 AM   #9 (permalink)
Registered Member
 
Join Date: Jan 2009
Posts: 93
Default

Quote:
Originally Posted by hlcole View Post
I am new to Objective C and XCode and this is a great learning example. I am having some trouble compiling with the Typedef enums in there. Any additional pointers would be great.
PM me if you want me to test it on my phone
joshsroka is offline   Reply With Quote
Old 09-15-2009, 12:32 AM   #10 (permalink)
Registered Member
 
garysanchez_29's Avatar
 
Join Date: Sep 2009
Posts: 1
Default

Quote:
Originally Posted by will.ray View Post
here is my card and deck class... it is working in simulator... I am c# programmer... is there anything that might blow up a real iphone or get rejected by app store???

thanks... code follows..
Code:
//
//  card.m
// 
//
//  Created by Admin on 2/10/09.
//  Copyright 2009 __MyCompanyName__. All rights reserved.
//

#import "card.h"


@implementation card
-(void) setRank: (int) n {
    rank = n;
}
-(int)rank{
	return rank;
}
-(void) setSuit: (NSString *) s {
    suit = s;
}
-(NSString *)suit{
	return suit;
}

@end


--------

//
//  deck.m
// 
//
//  Created by Admin on 2/10/09.
//  Copyright 2009 __MyCompanyName__. All rights reserved.
//

#import "deck.h"


@implementation deck
-(id) init {
    self = [super init];
	maincardlist =  [[NSMutableArray alloc] init]; 
	cardlist = [[NSMutableArray alloc] init]; 
	shuffledlist = [[NSMutableArray alloc] init]; 
	return self;
}
-(void) setupDeck{
	int i;
	
	for(i=1; i < 14; i++)
	{
		card *c;
		c = [[card alloc]init];
		[c setRank:i];
		[c setSuit:(NSString *)"d"];
		[cardlist addObject:c];     
	}
	for(i=1; i < 14; i++)
	{
		card *c;
		c = [[card alloc]init];
		[c setRank:i];
		[c setSuit:(NSString *)"h"];
		[cardlist addObject:c];     
	}
	for(i=1; i < 14; i++)
	{
		card *c;
		c = [[card alloc]init];
		[c setRank:i];
		[c setSuit:(NSString *)"c"];
		[cardlist addObject:c];     
	}
	for(i=1; i < 14; i++)
	{
		card *c;
		c = [[card alloc]init];
		[c setRank:i];
		[c setSuit:(NSString *)"s"];
		[cardlist addObject:c];     
	} 
	int numcardsleft = 52;
	card *c;
	int x;
	for(x=0;x<52;x++)
	{
		c = [cardlist objectAtIndex:x];   
		[maincardlist addObject:c];
	}
	
	while(numcardsleft>1)
	{
		x =  random() % (numcardsleft); 
				
		c = [cardlist objectAtIndex:x];   
		[shuffledlist addObject:c];
		[cardlist removeObjectAtIndex:x];
		numcardsleft--;
	}
		c = [cardlist lastObject];
		[cardlist removeLastObject];
		[shuffledlist addObject:c];

	c = [shuffledlist lastObject];
	[shuffledlist removeLastObject];
	[self setCurrentCard:c];

}

-(void) shuffleDeck{
	[cardlist release];
	cardlist = [[NSMutableArray alloc] init]; 
	int numcardsleft = 52;
	card *c;
	int x;
	for(x=0;x<52;x++)
	{
		c = [maincardlist objectAtIndex:x];   
		[cardlist addObject:c];
	}
	[shuffledlist release];
	shuffledlist = [[NSMutableArray alloc] init]; 
	
	while(numcardsleft>1)
	{
		x =  random() % (numcardsleft); 
		
		c = [cardlist objectAtIndex:x];   
		[shuffledlist addObject:c];
		[cardlist removeObjectAtIndex:x];
		numcardsleft--;
	}
	c = [cardlist lastObject];
	[cardlist removeLastObject];
	[shuffledlist addObject:c];
	
	c = [shuffledlist lastObject];
	[shuffledlist removeLastObject];
	[self setCurrentCard:c];
	
}
-(void)gotoNextCard{
	[self setCurrentCard:[shuffledlist lastObject]];
	[shuffledlist removeLastObject];	
}

-(void) setCurrentCard: (card *) c {
    currentCard = c;
}

-(card *)currentCard{
	return currentCard;
}

@end
Hi will.ray, i am developing a card game, i try to follow your code but im stuck on the part where i need to display the image of the card on the players cards hand image.

What i did is create a class "cards" set the rank, suit, and card image.
My question is how can i display the image of the card on that class to a UIimage that represent the players cards hand.

Here is my code..

//
// PusoyDosViewController.m
// PusoyDos
//
// Created by Gary Sanchez on 9/11/09.
// Copyright __MyCompanyName__ 2009. All rights reserved.
//

#import "PusoyDosViewController.h"
#import "cards.h"


@implementation PusoyDosViewController

@synthesize pCard0, pCard1, pCard2, pCard3,
pCard4, pCard5, pCard6, pCard7,
pCard8, pCard9, pCard10, pCard11,
pCard12;

@synthesize legelPlays, lastPlayed;
//method to load players card


- (void)viewDidLoad {
[super viewDidLoad];

NSMutableArray *imageArray = [[NSMutableArray alloc] initWithObjects:
[UIImage imageNamed:@"d1.png"],
[UIImage imageNamed:@"d2.png"],
[UIImage imageNamed:@"d3.png"],
[UIImage imageNamed:@"d4.png"],
[UIImage imageNamed:@"d5.png"],
[UIImage imageNamed:@"d6.png"],
[UIImage imageNamed:@"d7.png"],
[UIImage imageNamed:@"d8.png"],
[UIImage imageNamed:@"d9.png"],
[UIImage imageNamed:@"d10.png"],
[UIImage imageNamed:@"d11.png"],
[UIImage imageNamed:@"d12.png"],
[UIImage imageNamed:@"d13.png"],
nil];

mainCardsList = [[NSMutableArray alloc] init];

int i;

for(i = 0; i < 13; i++) {

cards *c;
c = [[cards alloc] init];
[c setRank:i];
[c setSuit:@"d"];
[c setcardsPic:[imageArray objectAtIndex:i]];
[mainCardsList addObject:c];

}

pCard0.image = [mainCardsList objectAtIndex:1];

the image display a black color.

Thanks..
garysanchez_29 is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 252
24 members and 228 guests
ADY, AragornSG, bookesp, chillyh, dacapo, Dani77, Davey555, Dominus, dre, glenn_sayers, HemiMG, JasonR, karlam963, LEARN2MAKE, M.A.S., marshusensei, mer10, nobre84, Oral B, prchn4christ, Raggou, Rudy, spiderguy84, themathminister
Most users ever online was 1,187, 10-11-2011 at 08:09 AM.
» Stats
Members: 158,885
Threads: 89,230
Posts: 380,765
Top Poster: BrianSlick (7,129)
Welcome to our newest member, bookesp
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 02:28 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0