forums.ps2dev.org Forum Index forums.ps2dev.org
Homebrew PS2, PSP & PS3 Development Discussions
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Arrays Help

 
Post new topic   Reply to topic    forums.ps2dev.org Forum Index -> PSP Development
View previous topic :: View next topic  
Author Message
mplacona



Joined: 07 Aug 2007
Posts: 28

PostPosted: Mon Oct 22, 2007 10:22 pm    Post subject: Arrays Help Reply with quote

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
Back to top
View user's profile Send private message
SilverSpring



Joined: 27 Feb 2007
Posts: 115

PostPosted: Mon Oct 22, 2007 10:56 pm    Post subject: Reply with quote

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.
_________________
PSP PRX LibDocs
Back to top
View user's profile Send private message Visit poster's website
mplacona



Joined: 07 Aug 2007
Posts: 28

PostPosted: Mon Oct 22, 2007 11:07 pm    Post subject: Reply with quote

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
Back to top
View user's profile Send private message
Jim



Joined: 02 Jul 2005
Posts: 487
Location: Sydney

PostPosted: Mon Oct 22, 2007 11:49 pm    Post subject: Reply with quote

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

int cards[52];
for (x=0; x < 52; x++)
 cards[x]=x;

then to get a random deck
Code:

int deck[52];
int d=0;
int r=52;
for (x =0; x < 52; x++)
{
q=rand()%r;
deck[d]=cards[q];
cards[q]=cards[r];
r--;
d++;
}

I think that should work...:-)

Jim
_________________
http://www.dbfinteractive.com
Back to top
View user's profile Send private message Visit poster's website
mplacona



Joined: 07 Aug 2007
Posts: 28

PostPosted: Tue Oct 23, 2007 12:04 am    Post subject: Reply with quote

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

Cheers
Back to top
View user's profile Send private message
mplacona



Joined: 07 Aug 2007
Posts: 28

PostPosted: Tue Oct 23, 2007 8:10 pm    Post subject: Reply with quote

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:
#include <stdlib.h>
#include <stdio.h>
#include <time.h>

int main(int argc, char **argv)
{
     int deck[52];
   int d=0;
   int r=52;
   int q;
   int x = 0;
   
   int cards[52];
   int count;
   int count2;

   /* Simple "srand()" seed*/
     unsigned int iseed = (unsigned int)time(NULL);
     srand (iseed);

   for(count=0; count < r; count++)

   {
      cards[count]=count;
   }


   for (x=0; x < 52; x++)
   {
      q=rand()%r;
      deck[d]=cards[q];
      cards[q]=cards[r];
      printf("%i\n",cards[q]);
      r--;
      d++;
   }
   

   
  return (0);
}


Thanks
Back to top
View user's profile Send private message
Jim



Joined: 02 Jul 2005
Posts: 487
Location: Sydney

PostPosted: Tue Oct 23, 2007 10:42 pm    Post subject: Reply with quote

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
_________________
http://www.dbfinteractive.com
Back to top
View user's profile Send private message Visit poster's website
mplacona



Joined: 07 Aug 2007
Posts: 28

PostPosted: Tue Oct 23, 2007 11:38 pm    Post subject: Reply with quote

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

Code:
printf("%i\n",cards[q]);


instead of

Code:
printf("%i\n",deck[d]);


Thank you ver much, it's working fine now.

Cheers
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    forums.ps2dev.org Forum Index -> PSP Development All times are GMT + 10 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group