Arrays Help

Discuss the development of new homebrew software, tools and libraries.

Moderators: cheriff, TyRaNiD

Post Reply
mplacona
Posts: 28
Joined: Tue Aug 07, 2007 7:07 am

Arrays Help

Post by mplacona »

Hi guys I've been working on a cards game, and am a little bit stucked on a tiny annoying problem

Basically I have an array os strings (representing all the cards from my deck) and I need and can't figure out how to prepperly shuffle this deck and distribute 5 cards.

I've got it working some way, but I'm sure this is not the best way, because sometimes the card repeats when I deal it, and basically none of my cards can repeat.

I thought about copying my main array to another array and remove the cards already dealt from thi array, that way, everythime I pick one card it would necessarily be a different one, but I'm sure this is still not the best approach.

My idea was to shuffle this array, and then to display the fisrt five cards, I could just loop from 0 to 4, 'cause as the array is shuffled, it wouldn't be a problem, but then when I need more cards I would have to have a counter to know from which position to take the next card, so that's why it wouyld be best to remove the cards from the array, but I can't figure out how to do that.

To sum up, what I need is:
  • Shuffle the contents of my array
  • Have a function to get the first item and remove it from the array, so that way I would just call a function like getCard() as many times as I needed it.
Can somebody help me with that?

Thanks in advance
SilverSpring
Posts: 110
Joined: Tue Feb 27, 2007 9:43 pm
Contact:

Post by SilverSpring »

It would be easier to keep your array static (so 0 - 'Ace of Spades' to 51 - 'King of Diamonds'). Then choose your favourite prng to generate your random index (0-51) and thats your random card. For your 5 unique cards you can just put a check to gen a new number if that number had already been drawn.
mplacona
Posts: 28
Joined: Tue Aug 07, 2007 7:07 am

Post by mplacona »

SilverSpring wrote:It would be easier to keep your array static (so 0 - 'Ace of Spades' to 51 - 'King of Diamonds'). Then choose your favourite prng to generate your random index (0-51) and thats your random card. For your 5 unique cards you can just put a check to gen a new number if that number had already been drawn.
Hi I ain't sure if I understood what you posted. You're telling me to generate a random number and pick it from my array?

That's kind of what I'm trying to do, but how can I know if this number hasn't been picked upo already?

Can you gimme some examples?

Cheers
User avatar
Jim
Posts: 476
Joined: Sat Jul 02, 2005 10:06 pm
Location: Sydney
Contact:

Post by Jim »

Initialise an array from 0-51 with the numbers 0-51

Code: Select all

int cards[52];
for &#40;x=0; x < 52; x++&#41;
 cards&#91;x&#93;=x;
then to get a random deck

Code: Select all

int deck&#91;52&#93;;
int d=0;
int r=52;
for &#40;x =0; x < 52; x++&#41;
&#123;
q=rand&#40;&#41;%r;
deck&#91;d&#93;=cards&#91;q&#93;;
cards&#91;q&#93;=cards&#91;r&#93;;
r--;
d++;
&#125;
I think that should work...:-)

Jim
mplacona
Posts: 28
Joined: Tue Aug 07, 2007 7:07 am

Post by mplacona »

Hey I'll try that one and come up with some feed back. That looks good to me..

Cheers
mplacona
Posts: 28
Joined: Tue Aug 07, 2007 7:07 am

Post by mplacona »

Hi, I've got it working on my PC, but am still finding some difficulty about how to check if the number has already been used. I want to have different numbers for the whole array.

Code: Select all

#include <stdlib.h>
#include <stdio.h>
#include <time.h>

int main&#40;int argc, char **argv&#41;
&#123;
  	int deck&#91;52&#93;;
	int d=0;
	int r=52;
	int q;
	int x = 0;
	
	int cards&#91;52&#93;;
	int count;
	int count2;

	/* Simple "srand&#40;&#41;" seed*/
  	unsigned int iseed = &#40;unsigned int&#41;time&#40;NULL&#41;;
  	srand &#40;iseed&#41;;

	for&#40;count=0; count < r; count++&#41;

	&#123;
		cards&#91;count&#93;=count; 
	&#125;


	for &#40;x=0; x < 52; x++&#41;
	&#123;
		q=rand&#40;&#41;%r;
		deck&#91;d&#93;=cards&#91;q&#93;;
		cards&#91;q&#93;=cards&#91;r&#93;;
		printf&#40;"%i\n",cards&#91;q&#93;&#41;;
		r--;
		d++;
	&#125;
	

	
  return &#40;0&#41;;
&#125;
Thanks
User avatar
Jim
Posts: 476
Joined: Sat Jul 02, 2005 10:06 pm
Location: Sydney
Contact:

Post by Jim »

The algorithm deals with that.
On the first round
It picks one of the 52 pre-calculated cards
then it replaces that card with the 51st card
On the 2nd round it picks of the remaining 51 cards,
then it replaces that card with the 50th card.
...
It can't re-pick the same number.

Jim
mplacona
Posts: 28
Joined: Tue Aug 07, 2007 7:07 am

Post by mplacona »

Hi, that was my fault, Raphael helped me to find the problem, and it was just silly. I was calling

Code: Select all

printf&#40;"%i\n",cards&#91;q&#93;&#41;;
instead of

Code: Select all

printf&#40;"%i\n",deck&#91;d&#93;&#41;;
Thank you ver much, it's working fine now.

Cheers
Post Reply