# GML[SOLVED]ransomized placement of cards for a match 2 game

B

#### bluef

##### Guest
So im trying to randomize placement of cards. 5 cards along the x axis and 4 cards along the y axis.
wired stuff keeps happening. heres my code
Code:
``````loop = 1;
xri = 0;
yri = 0;
donuthin = 1;
xi = 32;
yi = 32;
randomize();
global.cur_card_no = random_range(1,6);
for (i = 0; i <= 20; i += 1)
{
if(instance_position(xi,yi,obj_cards))
{
donuthin = 1;
xi += 80;
if (xi > 352)
{
xi = 32;
yi += 80;
}
}
else
{
global.cur_card_no = random_range(1,6);
instance_create_depth(xi,yi,0,obj_cards);
xi += 80;
xri = choose(32,112,192,272,352);
yri = choose(32,112,192,272);
if(!instance_position(xri,yri,obj_cards))
{
while(instance_position(xri,yri,obj_cards)&& loop < 50)
{
loop += 1;
xri = choose(32,112,192,272,352);
yri = choose(32,112,192,272);
}

instance_create_depth(xri,yri,0,obj_cards);
}
}
if (xi >= 352)
{
xi = 32;
yi += 80;
}

}``````
now i am trying to get the gamemaker tto check weather a card is at a posistion x and y . if its not then place a card with random number and place another card of the same number on a random position. if there is a card there then move on to the next position. but it keeps going out of the specified positions and making cards and sometimes not making cards where there should be

#### Attachments

• 46.9 KB Views: 4

#### IndianaBones

##### Member
Personally I would alter the strategy by first creating a deck of cards with appropriate randomised value pairs, and then shuffle that deck. You can do this easily with a ds_list.

Then, it is simply a matter of taking the first card value in the ds_list, removing that value from the list and apply that value to the card.

The placement of the cards can then be handled by a simple double for loop.

I notice in your code you are not preventing multiple pairs of the same value, so I didn't put in any checks for that either.

Here's some example code I just wrote and tested:

Code:
``````// Setup some helper values for laying out the cards.
var layout_width = 5; // Make the card grid layout 5 cards wide
var layout_height = 4; // Make the card grid layout 4 cards high

// Starting x y position of the cards in the room
var start_x = 128;
var start_y = 128;

// Leave a little gap between the cards.
var card_spacing = 32;

// Calculate total number of cards.
var total_cards = layout_width * layout_height;

// Number of card pairs is half the number of total cards.
var number_of_pairs = total_cards / 2;
var card_min_value = 1;
var card_max_value = 6;

// Grab the actual width of the card sprite.
var card_width = sprite_get_width(spr_card);
var card_height = sprite_get_height(spr_card);

// ------------------------------------------

// Create the ds_list to hold the card pair values
card_deck = ds_list_create();

// Add value pairs to the list.
for ( var i = 1; i <= number_of_pairs; ++i)
{
var card_value = irandom_range(card_min_value, card_max_value);

// This is the magic of adding two cards of the same value to the list,
// so we get matching pairs.
}

// VERY IMPORTANT : Shuffle the values into a random order.
ds_list_shuffle(card_deck);

// Place the cards in the room
for ( var i = 0; i < layout_width; ++i )
{
for ( var j = 0; j < layout_height; ++j )
{
// Create the new card
var _new_card = instance_create_layer(x,y, "Instances", oCard);

// Position the card.
_new_card.x = start_x + (i * (card_width + card_spacing));
_new_card.y = start_y + (j * (card_height + card_spacing));

// Assign the card a value from the current first card in the deck.
_new_card.value = card_deck[| 0];

// Remove that card from the deck.
ds_list_delete(card_deck, 0);
}
}

// Destroy the ds_list so we don't get a memory leak.
ds_list_destroy(card_deck);``````

B

#### bluef

##### Guest
thanks bruh it works as intended. i was just learning ds grid for this too. someone told me its a better way. your code helps me a lot. i can learn from example much quicker

#### Attachments

• 29.3 KB Views: 4

#### IndianaBones

##### Member
thanks bruh it works as intended. i was just learning ds grid for this too. someone told me its a better way. your code helps me a lot. i can learn from example much quicker
Glad its working for you. A ds_grid could also be used if you wanted to. It all depends what suits your game best.

Also, I didn't put it in the code above, but if you want the cards to have different random positions each time you test the game in the IDE, be sure to include this code once at the start of your game:

Code:
``randomize()``
...like you did in the example code you posted. Otherwise you'll get the exact same layout every time when testing.

B

#### bluef

##### Guest
yes going to put randomize() in.