The algorithme I was thinking about, was like this;
All cards are stored in an array; like this:
Code: Select all
.db Card1, Card2, Card3, Card4, Card5 ....
Code: Select all
.db 0,0,0,Card1,0
Code: Select all
.db Card3, Card5, Card2, Card1, Card4
Moderator: MaxCoderz Staff
Code: Select all
.db Card1, Card2, Card3, Card4, Card5 ....
Code: Select all
.db 0,0,0,Card1,0
Code: Select all
.db Card3, Card5, Card2, Card1, Card4
Yeah... good idea... I already worried that in case of alot cards (around 60), generating every time a new random number would take up a lot of time...CoBB wrote:I'd probably not generate a new random number if the slot chosen is already taken, but I'd rather start going in either direction (with wraparound at the borders) until I find the first free slot. The direction doesn't have to be always the same, you can decide it e. g. according to the LSB of the original number.
Code: Select all
int num_of_cards = 50;
cards[num_of_cards];
for (int i = num_of_cards-1; i > 0; i--) {
int rand = randint(...); // 0 <= rand < i
tempcard = cards[rand];
cards[rand] = cards[i];
cards[i] = tempcard;
}
Hey, that's nice... I already thought there would be another way... I think I will use this oneKalimero wrote:You can shuffle an array in place by picking a random spot and swap that with the first. Pick one of the remaining random spots and swap it with the second and so on. Actually, what you do is making a new deck with random cards from the old one. The following code does it backwards, which has some advantages.If you need to preserve the original order, just make a copy.Code: Select all
int num_of_cards = 50; cards[num_of_cards]; for (int i = num_of_cards-1; i > 0; i--) { int rand = randint(...); // 0 <= rand < i tempcard = cards[rand]; cards[rand] = cards[i]; cards[i] = tempcard; }