Advertise Books Events Forum News Social Networking Support Us

sdkIQ for iPhone
($4.99)

Shape Up
($0.99)

Your First iPhone App
($1.99)

iVidCam Free
(free)

Kid Art
($0.99)

iPUBQUIZ
(£1.19)

ArtStudio
($3.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 04-19-2009, 03:37 AM   #1 (permalink)
New Member
 
Join Date: Apr 2009
Posts: 92
Default Shuffling Cards.

I cannot figure out how to do it efficiently.

Please someone help.
iphonedev. is offline   Reply With Quote
Old 04-19-2009, 05:17 AM   #2 (permalink)
Registered Member
 
Stitch's Avatar
 
Join Date: Aug 2008
Posts: 398
Default

I find the best way is to place them all face down on a table and then do multiple circle movements over the top of them. (kind of a wax on, wax off motion).
Stitch is offline   Reply With Quote
Old 04-19-2009, 10:36 AM   #3 (permalink)
Pro. Game Developer
iPhone Dev SDK Supporter
 
Join Date: Feb 2009
Location: żLa Islas Hermosas?
Posts: 1,426
Talking

Quote:
Originally Posted by Stitch View Post
I find the best way is to place them all face down on a table and then do multiple circle movements over the top of them. (kind of a wax on, wax off motion).
Oh, snap.
Kalimba is offline   Reply With Quote
Old 04-19-2009, 11:45 AM   #4 (permalink)
New Member
 
Join Date: Apr 2009
Posts: 92
Default

Quote:
I find the best way is to place them all face down on a table and then do multiple circle movements over the top of them. (kind of a wax on, wax off motion).
Seriously dude I really need help with it.
iphonedev. is offline   Reply With Quote
Old 04-19-2009, 11:53 AM   #5 (permalink)
New Member
 
Join Date: Mar 2009
Posts: 48
Default

Then maybe you should try being more specific about your problem. The guy above me answered your question, if that's not what you meant, then clarify...

You could also try bridging the cards, thats always fun to do.
XenoSage is offline   Reply With Quote
Old 04-19-2009, 12:01 PM   #6 (permalink)
New Member
 
Join Date: Apr 2009
Posts: 92
Default

Quote:
Originally Posted by XenoSage View Post
Then maybe you should try being more specific about your problem. The guy above me answered your question, if that's not what you meant, then clarify...

You could also try bridging the cards, thats always fun to do.
Okay. I have 52 images of all the cards, I want to have an array of these images and then all of the indexes get reasigned randomly (there in order when you start.) But ive tried this before and I couldnt get it to work.
iphonedev. is offline   Reply With Quote
Old 04-19-2009, 12:05 PM   #7 (permalink)
Registered Member
 
Join Date: Nov 2008
Posts: 788
Default

Quote:
Originally Posted by iphonedev. View Post
Okay. I have 52 images of all the cards, I want to have an array of these images and then all of the indexes get reasigned randomly (there in order when you start.) But ive tried this before and I couldnt get it to work.
What couldn't you get to work? rand()?
johnqh is offline   Reply With Quote
Old 04-19-2009, 12:08 PM   #8 (permalink)
New Member
 
Join Date: Mar 2009
Posts: 48
Default

next thing you want to do is post your code as is right now so we don't suggest what you already have.
XenoSage is offline   Reply With Quote
Old 04-19-2009, 12:11 PM   #9 (permalink)
New Member
 
Join Date: Apr 2009
Posts: 92
Default

Quote:
Originally Posted by johnqh View Post
What couldn't you get to work? rand()?
Well it kept repeating numbers and I could not get them to change indexes within the array. Like card 1 is at index 0 and after shuffling card 1 is at index 25.
iphonedev. is offline   Reply With Quote
Old 04-19-2009, 12:17 PM   #10 (permalink)
Registered Member
 
Join Date: Nov 2008
Posts: 788
Default

Quote:
Originally Posted by iphonedev. View Post
Well it kept repeating numbers and I could not get them to change indexes within the array. Like card 1 is at index 0 and after shuffling card 1 is at index 25.
We still don't know how you are shuffling them.
johnqh is offline   Reply With Quote
Old 04-19-2009, 12:20 PM   #11 (permalink)
New Member
 
Join Date: Mar 2009
Posts: 48
Default

I would suggest doing something more along the lines of not actually changing the index positions of the cards. Instead, use a random number generator and an if check to make sure you havent accessed that position already to retrieve cards from the deck at random.

If this were in java i'd say just use Collections.shuffle(), but i don't know the NSMutableArray equivalent if there is one.
XenoSage is offline   Reply With Quote
Old 04-19-2009, 12:23 PM   #12 (permalink)
New Member
 
Join Date: Apr 2009
Posts: 92
Default

Code:
for (int j = 0; j < numberArray.count; j++) {
	int randomIndex = (arc4random() % 52);
             [cardsArray exchangeObjectAtIndex:j withObjectAtIndex:randomIndex];
	}
Thats what I am using to shuffle the cards but it says "cardsArray may not respond to exchangeObjectAtIndex"
iphonedev. is offline   Reply With Quote
Old 04-19-2009, 12:26 PM   #13 (permalink)
New Member
 
Join Date: Mar 2009
Posts: 48
Default

are you using NSMutableArray and not NSArray?
XenoSage is offline   Reply With Quote
Old 04-19-2009, 12:27 PM   #14 (permalink)
Magic Hands' Daddy
 
Join Date: Aug 2008
Location: Memphis, TN, USA
Age: 22
Posts: 1,372
Send a message via ICQ to smithdale87 Send a message via AIM to smithdale87 Send a message via Skype™ to smithdale87
Default

@OP:

You should really learn to use Google to your benefit. A simple search for "objc shuffle nsarray" provided exactly what you're looking for.

Even though you may not want to use the code directly, you can at least use it as a learning tool.
__________________
Games
AppRoach - (v1.0)
7 Deaths In Nagamachi (v1.1)
Segment (v1.2)
ThumStruck Free (v1.5)
Plummet - new (v1.0)
ThumStruck 2! - Coming Soon

Apps
ArtsMemphis - (v1.0)
Elvis Mobile (v1.1)
DataLoss DB - (v1.1)
Lovers & Haters - (v1.0)
smithdale87 is offline   Reply With Quote
Old 04-19-2009, 12:31 PM   #15 (permalink)
New Member
 
Join Date: Apr 2009
Posts: 92
Default

Quote:
Originally Posted by XenoSage View Post
are you using NSMutableArray and not NSArray?
Usinag a NSArray. Should I be using NSMutableArray.
iphonedev. is offline   Reply With Quote
Old 04-19-2009, 12:33 PM   #16 (permalink)
Magic Hands' Daddy
 
Join Date: Aug 2008
Location: Memphis, TN, USA
Age: 22
Posts: 1,372
Send a message via ICQ to smithdale87 Send a message via AIM to smithdale87 Send a message via Skype™ to smithdale87
Default

Yea
__________________
Games
AppRoach - (v1.0)
7 Deaths In Nagamachi (v1.1)
Segment (v1.2)
ThumStruck Free (v1.5)
Plummet - new (v1.0)
ThumStruck 2! - Coming Soon

Apps
ArtsMemphis - (v1.0)
Elvis Mobile (v1.1)
DataLoss DB - (v1.1)
Lovers & Haters - (v1.0)
smithdale87 is offline   Reply With Quote
Old 04-19-2009, 12:33 PM   #17 (permalink)
New Member
 
Join Date: Mar 2009
Posts: 48
Default

Yes, for that method to work, you need an NSMutableArray.

Mutable literally means (you can edit it!)
XenoSage is offline   Reply With Quote
Old 04-19-2009, 12:40 PM   #18 (permalink)
Magic Hands' Daddy
 
Join Date: Aug 2008
Location: Memphis, TN, USA
Age: 22
Posts: 1,372
Send a message via ICQ to smithdale87 Send a message via AIM to smithdale87 Send a message via Skype™ to smithdale87
Default

Give to a bunch of developers for making object type literally describe what they do :-)
__________________
Games
AppRoach - (v1.0)
7 Deaths In Nagamachi (v1.1)
Segment (v1.2)
ThumStruck Free (v1.5)
Plummet - new (v1.0)
ThumStruck 2! - Coming Soon

Apps
ArtsMemphis - (v1.0)
Elvis Mobile (v1.1)
DataLoss DB - (v1.1)
Lovers & Haters - (v1.0)
smithdale87 is offline   Reply With Quote
Old 04-19-2009, 12:52 PM   #19 (permalink)
New Member
 
Join Date: Mar 2009
Posts: 48
Default

I know right?!?!
XenoSage is offline   Reply With Quote
Old 04-19-2009, 12:58 PM   #20 (permalink)
New Member
 
Join Date: Apr 2009
Posts: 92
Default

Quote:
Originally Posted by XenoSage View Post
Yes, for that method to work, you need an NSMutableArray.

Mutable literally means (you can edit it!)
Thanks lol

Also how do I put my UIImageViews into an array.
iphonedev. is offline   Reply With Quote
Old 04-19-2009, 01:34 PM   #21 (permalink)
New Member
 
Join Date: Mar 2009
Posts: 48
Default

what are you doing right now that isnt working?
XenoSage is offline   Reply With Quote
Old 04-20-2009, 01:24 AM   #22 (permalink)
New Member
 
Join Date: Apr 2009
Posts: 92
Default

Quote:
Originally Posted by XenoSage View Post
what are you doing right now that isnt working?
Roght now I got all the cards to shuffle using NSNumbers that start out 1-52 then there indexes change like the code i posted before. Based on those indexes I can set the images to the corresponding index for the user to see them. Now I am stuck as how to do one thing, I am making blackjack so I need to see if the cards are geater than 21 added up, so that means that (since the cards are label 1-52) it will not always add up to want. Say Number 1 is Jack of spades, and Number 10 is Ace of Spades. Those do not add up to 21, they add up to 11, so how do I reassign these values so that Jack of Spades is 10 and Ace of Spades is eleven, etc.

Sorry if that was confusing lol
iphonedev. is offline   Reply With Quote
Old 04-20-2009, 01:41 AM   #23 (permalink)
Registered Member
 
Join Date: Nov 2008
Posts: 192
Default

Quote:
Originally Posted by iphonedev. View Post
Roght now I got all the cards to shuffle using NSNumbers that start out 1-52 then there indexes change like the code i posted before. Based on those indexes I can set the images to the corresponding index for the user to see them. Now I am stuck as how to do one thing, I am making blackjack so I need to see if the cards are geater than 21 added up, so that means that (since the cards are label 1-52) it will not always add up to want. Say Number 1 is Jack of spades, and Number 10 is Ace of Spades. Those do not add up to 21, they add up to 11, so how do I reassign these values so that Jack of Spades is 10 and Ace of Spades is eleven, etc.

Sorry if that was confusing lol
Instead of shuffling the images... you should shuffle the "idea" of a card. I guess you could create a Card class.

On a side note, is there not already a blackjack app available? If there is, there's no rush... and I would learn the programming fundamentals properly. It will only help you in the long run.
jsonli is offline   Reply With Quote
Old 04-20-2009, 02:14 AM   #24 (permalink)
Registered Member
 
Stitch's Avatar
 
Join Date: Aug 2008
Posts: 398
Default

What have you stored in cardsArray?

I would probably place a NSDictionary something like:

Code:
[cardsArray addObject:[NSDictionary dictionaryWithObjectsAndKeys:
	[NSString stringWithString:@"Diamond"], @"suitKey",
	[NSString stringWithString:@"Jack"], @"cardKey",
	[NSNumber numberWithInt:10], @"valueKey",
             nil]];
This way you can easily find the Suit, Card Number and value at a specific index point in cardsArray.

Code:
int cardValue = [[[cardsArray objectAtIndex:25] objectForKey:@"valueKey"] intValue];
Stitch is offline   Reply With Quote
Old 04-20-2009, 09:16 AM   #25 (permalink)
Registered Member
 
Join Date: Nov 2008
Posts: 192
Default

Probably a good idea to extract that into a struct or class.
jsonli 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


Enter the iPhone App Challenge!  Win $500!
» Advertisements
» Stats
Members: 24,288
Threads: 39,077
Posts: 171,356
Top Poster: smasher (2,575)
Welcome to our newest member, germainesluaus
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 08:47 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0