Page 1 of 1

Choosing card from deck

PostPosted: Fri Apr 10, 2015 6:02 pm
by koala
I am working on a card game. I've been using rand() function to choose cards from deck, but this function has its weaknesses and game often dealt same cards. Algorithm was:
Code: Select all
#define DECK 52 // number of cards in deck

int cards[DECK]; // deck of cards
int choice; // index of chosen card

do {
   choice = rand(52);
} while (cards[choice] == 0);

// set card attributes according to choice

choice = 0;


I've added stTime variable and changed do - while loop:
Code: Select all
stTime  t = getTime();

//...

choice = fmod((t.sec_utc + (int) rand(52)), 52);


This helped. :)

What do you think? :D

Re: Choosing card from deck

PostPosted: Fri Apr 10, 2015 6:34 pm
by lcl
Interesting idea to use stTime for making the results appear more random. :)

I have one question, though. You seem to use the code to get a random card from the deck, is the cards[] array for storing data of which cards have already been picked, so that same card can not be picked twice in a single game? Or is it a game where the same card can be picked many times?

I'm asking because if it is so that you should never get the same card twice in a game, there's better ways for achieving that than getting a new rand() until a card is found that has not been picked. Getting "random" numbers is kinda slow and if you have 52 cards to choose from but the game has progressed to where 51 have already been drawn and there's only 1 left, looping a do while until the rand() hits that card may well freeze you game for a while.

But I guess that is not what your game is doing, just wanted to make sure. :)

I hope this made sense, I'm having a hard time converting my ideas to English today :lol:

Re: Choosing card from deck

PostPosted: Fri Apr 10, 2015 6:49 pm
by koala
You are absolutely right. int cards[DECK] is array of 52 cards and game is choosing cards from that deck. At the place of chosen card is written 0. I know what you want to say, but I don't think it will freeze, or even slow down the game. However, I don't want to be arrogant. You have more experience and knowledge in GE and C, so I'll listen to your advice. :D Thanks! :D I might then instead of writing 0, "delete" card from array, something like this:
Code: Select all
// int n is number of cards and already exists. It is set to 52,
// and every time card is picked n is reduced by 1 (n--), so I can use that.

choice = fmod((t.sec_utc + (int) rand(n)), n);

for (i = choice + 1; i < n; i++)
   cards[i - 1] = cards[i];

n--;

Re: Choosing card from deck

PostPosted: Thu Apr 16, 2015 12:16 pm
by lcl
Oh, it seems I forgot to answer.

Could you post your whole code for selecting the cards? I could have a look at it and then see if the problem of delay / temporal freezing is possible.
In the meantime, here's an example I once made for a user having a similar problem. This uses an array to store different numbers, picks one from there one by one, and then replaces that number in the array with 0, and finally moves all 0's to the end of the array, so that the rand() can always only randomize on the part of the array that actually contains data. Of course your situation is a little different, but anyway. :D The codes are in global code - "global" and in debug - key down: return - script editor.

Re: Choosing card from deck

PostPosted: Thu Apr 16, 2015 2:46 pm
by koala
I would need to post the entire project, because everything is connected. I chose cards inside of function that has arguments. One argument is structure, other are integers. And project is currently a mess. More, or less, the algorithm works this way:

#define DECK 52

int cards[DECK];

- fill elements of cards[] so it consist information of suit, numeral and index
- choose an element from cards[] using fmod((t.sec_utc + (int) rand(n)), n)
- fill structure and card (actor) fields depending on cards[choose]
- "erase" chosen element and decrease the length of cards[]

So basically, there's an array of integers. First I've set 0 in place of chosen cards. Later, I've changed that algorithm with algorithm above.

I'll post the entire project when I finish a game, make everything and comment the code.