# Randomized an array for a deck of cards

Get help using Construct 2

### » Sat Nov 05, 2011 6:15 pm

If what you want to do is knowing what is the card you have, don't forget that just the number 0 to 51 can represent it, 'cause a deck of classical card is just a grid. you have color (spades (?), hearts (?), diamonds (?) and clubs (?)), on the rows and values (1,2,...,Q,K,A) on the columns
So basically if you choose to order things like in your animation in the Card sprite :
for colors
- 0 = clubs
- 1 = diamond
- 2 = hearts
for values
- 0 = 2 (ok it's a bit counter intuitive but heh...)
- 1 = 3
- ...
- 12 = K
- 12 = A

so you have a grid with 4 rows and 13 columns
the number 0 = the 2 of clubs
the number 51 = the Ace of spades

So basically you don't need any 3 dimensionnal array, all the informations are in the number representing the card
You just have to extract it as I did for placing cards

CardDeck.At(DeckCardNumber) is the number
to get the value it's like the X calculation
value = CardDeck.At(DeckCardNumber)%13

color is like the Y calculation
color = floor(CardDeck.At(DeckCardNumber)/13)

then you have your card (:

let's try the number 16
value = 16%13 = 3 it's a 4
color = floor(16/13) = 1 it's a diamond

16 = 4 of diamonds

easy peasy :D
B
69
S
22
G
14
Posts: 1,488
Reputation: 16,594

### » Sat Nov 05, 2011 6:31 pm

You shouldn't need to store the card value in an array since it is easily calculated from the card number. If anything you can store the value in an instance variable for each dealt card.

Change the order of your animation frames so that the Ace comes before the two. Then you can calculate the face value 1-13 as follows:
(card % 13)+1

Edit:
I didn't see yann's post until now.R0J0hound2011-11-06 19:47:39
B
98
S
36
G
132
Posts: 5,521
Reputation: 84,005

### » Sun Nov 06, 2011 9:08 am

[QUOTE=vidi] Thank you Joe7 It looks like I was looking for.
Can you tell me wich plugin I need? I can not open this exemple.
[/QUOTE]
Plugin Random Array
B
14
S
7
G
5
Posts: 173
Reputation: 4,802

### » Mon Nov 07, 2011 2:18 am

Thanks again for your help Yann. As always you are a big help.

I updated my demo so now it gets the card values and can total up card hands.

Thanks R0J0hound for your input. I did change the order of the animation frames so that the Ace comes before the two. So now it more straight forward to calculate the face value. (card % 13)+1

Updated capx Card Game

or you can check it out at here Card Game

B
15
S
10
G
8
Posts: 107
Reputation: 4,944

### » Thu Nov 24, 2011 4:38 pm

Hi, I'm making a solitaire game, I followed all the advices here and I understand what are you all saying, but there's a problem.

The formula that Yann posted gets you the current card, and I understand the principle BUT what if I want to get the card value and suit LATER?

I lay down my rows, then I want to click on a card and retrieve the value and suit, how can one do that?

Thanks

EDIT: I could keep track of the position of each card, but it looks "hackish" is there a way to set a variable to each of the spawned card object?0plus12011-11-24 16:39:44
B
30
S
9
G
6
Posts: 525
Reputation: 8,329

### » Thu Nov 24, 2011 10:40 pm

You have two things involved that are close to each other but quite differente in application.
On one side you have the Array with all the card shuffled or not
On the other side you have the number (from 0 to 51) from which you can guess the value and color (see my last post for that)

Basically, you can really do anything that you want with this system.
The cards will always keep the same order in the deck, so you will always find out witch card has been drawn and where (in the deck).
If you have some game that involve putting back card in the deck you will just have to move values inside the Array
If you have some game that involve other deck, just use another array

However, if you want to know if you have pairs, flush, straight, three of a kind... that kind of stuff, it will me more tricky
I guess you could start by loading each hands on a two dimensionnal array (set value at(hand,index) to cardValue) and compare each member to count similarities (how many cards have the same value or color) or sequences (what is the biggest sequence and what is it?)

Well... dunno it's getting complicated and I don't even know if I'm in the scope of your question...
Yann2011-11-24 22:41:20
B
69
S
22
G
14
Posts: 1,488
Reputation: 16,594

### » Thu Nov 24, 2011 11:28 pm

Thanks Yann, you answer confirm that this is not the way to go for me.

What I want is to be able to click on a card, retrieve it's value/suit, then destroy it or drag it across the screen.

The array method gives me the value with a mathematical "trick" but doesn't get me the spawned object id.

Now I just started in construct, so I'm not really sure if it's even possible to have an event like:

onclick spawned_object_10 do something..

As I see it he only way would be to load each card as a different object and then using a randomized array spawning them on the screen, keeping each of them indipendent.
B
30
S
9
G
6
Posts: 525
Reputation: 8,329

### » Fri Nov 25, 2011 12:33 am

For me to have a usefull opinion on your question, I need to know more precisely how your game would be played.
'cause maybe there's some easier ways to do, not what you say here, but what you want to achieve.
Without telling me the end result I can't really guess if you take an easy or a hard path... a wrong or a good one.

Here it's as if I look at your game too closely.
B
69
S
22
G
14
Posts: 1,488
Reputation: 16,594

### » Fri Nov 25, 2011 12:37 am

Hi 0plus1,
if you check out the card object it has CardSuite and FaceValue for each card as it dealed out. So it has every thing you need.
B
15
S
10
G
8
Posts: 107
Reputation: 4,944

### » Fri Nov 25, 2011 10:44 am

@Maxum Thanks, I understand now, I didn't knew construct kept track of the instanced object, it's incredible how easy it is, I'm a programmer (applications, not games) so I'm accustomed to another way of thinking, incredible.. as soon as I finish my game I'll be sure to show it you.

@Yann I'm re-doing the classic solitaire (the one included in windows) to learn construct
B
30
S
9
G
6
Posts: 525
Reputation: 8,329

PreviousNext

### Who is online

Users browsing this forum: No registered users and 26 guests