# How do I get a working random/shuffled card system?

Get help using Construct 2

### » Sun Oct 02, 2016 10:49 pm

I am try to create a system that will give the player a random set of cards when they press the 'deal' button. The properties of the cards has not been established yet, I'm figuring I will just assign variables to each card as far as their attributes, but when the player say played a card, that would decrease the number of cards in hand. Each turn the player will draw a random card, and if the player draws a card and has exceeded the set number of allowed cards, the player will be required to discard a card before the turn is played. Now I am simply at the point of having the 'deal' button that gives the player 3 random cards when pressed, my problem is that it always gives the same card, card number 1. My question is what am I doing wrong here? This is the layout of events so far. Now later when I have things working, aside from assigning properties to the cards, I will want the cards to fly on over from a deck into the hand, but that's an animation/physics question for later.

(I know I don't have actual card graphics here yet, the primary purpose for this at this point is the have the functionality)
B
8
S
3
G
1
Posts: 33
Reputation: 1,016

### » Sun Oct 02, 2016 11:59 pm

I don't see how CardA_1, CardA_2 and CardA_3 can ever be 1, can you explain what's happening on event 4?
B
52
S
21
G
94
Posts: 2,421
Reputation: 55,530

### » Mon Oct 03, 2016 12:04 am

They are all global variables, so I set them to be a random number up to 1.
B
8
S
3
G
1
Posts: 33
Reputation: 1,016

### » Mon Oct 03, 2016 12:07 am

Are you trying to make it choose between 0 and 1?
B
52
S
21
G
94
Posts: 2,421
Reputation: 55,530

### » Mon Oct 03, 2016 12:19 am

Yeah, but at the same time I'm going to want to I prevent overlapping however I can.
B
8
S
3
G
1
Posts: 33
Reputation: 1,016

### » Mon Oct 03, 2016 12:30 am

That's what it looked like. random(1) will pick a decimal value between 0 and 1. So your event is saying something like on mouse click add 0.545345 to CardA_1, add 0.234245 to CardB_1 etc. You probably want choose(0,1) which will pick 0 or 1 at random.
B
52
S
21
G
94
Posts: 2,421
Reputation: 55,530

### » Mon Oct 03, 2016 12:38 am

Excellent thanks! I think that worked. Now my only problem is with overlapping, but I think that one way I can go about that would be rather to have a variable between 1 and 3 for cards and depending on what each 'card-in-hand spot' had would determine which card will be there.
B
8
S
3
G
1
Posts: 33
Reputation: 1,016

### » Mon Oct 03, 2016 12:48 am

I'm not sure exactly on the layout of that game and the 3 random cards but in its most basic functionality you could do On click, set a global variable to choose between a number of cards so choose(0,1,2,3,4,5,6...50,51), which can also be displayed as floor(random(52)), if each number relates to a card in the deck. Then you run through it again for as many times as you need to pick a new card. This works well for randomly picking a card, if you want to get into shuffling cards in a deck and drawing them then I would look at arrays.
B
52
S
21
G
94
Posts: 2,421
Reputation: 55,530

### » Mon Oct 03, 2016 12:57 am

Arrays are something that has has always confused me, I understand the base of them, but I just can't grasp how to use them and with what. would they help with the order of the cards, how many of one card is in a deck (or more so how many the player can have drawn before the game will no longer allow the player to draw said card) I see a lot of people mention arrays when it comes to inventory systems, I just don't understand exactly why and what they do.
B
8
S
3
G
1
Posts: 33
Reputation: 1,016

### » Mon Oct 03, 2016 1:04 am

Arrays just store data in a list or table and you can access the data at any point whenever you want, sort the data or add/delete data. They are stored as co-ordinates, so you could have 0,0 is Ac 1,0 is 2c 2,0 is 3c and so on for 52 cards in ascending order. You can then shuffle them with some logic. You can search the array for a specific card and delete it from the list if for example it has been dealt. If you were to deal the first 3 cards you would 'pop' the first 3 entries from the array as dealt, leaving the other 49 cards. This is fully visible in debug mode as well so you can see what's going on with the array.

This tutorial is good to just read to get familiar with arrays: https://www.scirra.com/tutorials/307/ar ... -beginners
B
52
S
21
G
94
Posts: 2,421
Reputation: 55,530

Next