Choosing card from deck

Learn how to make certain types of games and use gameEditor.

Choosing card from deck

Postby koala » Fri Apr 10, 2015 6:02 pm

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
Phascolarctos cinereus
YouTube: Marko Radivojevic
Google+: Marko Radivojevic
User avatar
koala
 
Posts: 301
Joined: Thu Mar 26, 2015 7:03 pm
Location: Serbia
Score: 30 Give a positive score

Re: Choosing card from deck

Postby lcl » Fri Apr 10, 2015 6:34 pm

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:
User avatar
lcl
 
Posts: 2339
Joined: Thu Mar 25, 2010 5:55 pm
Location: Finland
Score: 276 Give a positive score

Re: Choosing card from deck

Postby koala » Fri Apr 10, 2015 6:49 pm

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--;
Phascolarctos cinereus
YouTube: Marko Radivojevic
Google+: Marko Radivojevic
User avatar
koala
 
Posts: 301
Joined: Thu Mar 26, 2015 7:03 pm
Location: Serbia
Score: 30 Give a positive score

Re: Choosing card from deck

Postby lcl » Thu Apr 16, 2015 12:16 pm

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.
Attachments
selectFromUnselected.zip
(3.62 KiB) Downloaded 163 times
User avatar
lcl
 
Posts: 2339
Joined: Thu Mar 25, 2010 5:55 pm
Location: Finland
Score: 276 Give a positive score

Re: Choosing card from deck

Postby koala » Thu Apr 16, 2015 2:46 pm

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.
Phascolarctos cinereus
YouTube: Marko Radivojevic
Google+: Marko Radivojevic
User avatar
koala
 
Posts: 301
Joined: Thu Mar 26, 2015 7:03 pm
Location: Serbia
Score: 30 Give a positive score


Return to Tutorials

Who is online

Users browsing this forum: No registered users and 1 guest