Login | Register

Shuffle a deck of card

July 18, 2006, 13:30 by Calle
[loading]
-->
//Return some variables to standard values.
global.pointer = 0;
//Shuffle the deck using lists.
global.deck = ds_list_create()
while(ds_list_size(global.deck)<52) {
unique_card = false;
while(unique_card == false) {
card = ceil(random(52));
if(ds_list_find_index(global.deck,card)==-1) {
ds_list_add(global.deck,card);
unique_card = true;
}
}
}
//The deck is shuffled. Everything is stored in the
//global.deck array/list. Now store it into an array.
for (i = 0; i <52; i+=1) {
global.content[i] = ds_list_find_value(global.deck,i);
}


I didn't create this script myself, my brother did. As you can see it uses data structure functions to shuffle, and this is the ideal choice for a deck of cards. Don't ask me why my brother then wanted to pass it on onto an array, but probably because it was easier to integrate with the rest of the game, e.g. card dealing.

The function of the first while loop? Obviously it assigns the deck to the data structure, but how it works; the condition is; while the data structure contains less than 52 values - do this. So the loop runs 52 times. Each time it picks a random number, and picks even another number until that number is unique for that deck. All numbers that are picked are below 52, as you can see by looking at the first and only argument in function random(). The result from function random is manipulated by the rounding function ceil(), which will always round up to the closest integer. Also this is the ideal choice, even though other variants are available (such as floor(random(53))).

If there if anything else you�d like to be explained, please ask!

Comments

Loading comments... [loading]
.
Users logged in:

game maker articles, game maker examples, game maker tutorials, gmtutorials, game maker questions and answers, game maker crash course, how to create games