0 Favourites

Randomized an array for a deck of cards

  • Im playing a round with a card game project. I have the basic functions down, making the deck, shuffling the deck and dealing the cards out. If you could take a look at my capx and let me know if there is a better way to randomized the array for the deck. Or if you have any other tips or tricks for the rest of my code I would like to see it. As I am just learning Construct 2.

    http://dl.dropbox.com/u/1244375/CardGame2.capx

    Updated 1/25/2014 -----------------------------------------------------

    Hello all,

    I been away for a long time.

    I have updated this card game demo to fix the shuffle and I took out the plugin Moveto so it will run off stock Construct2.

    Here is the updated file:

    https://dl.dropboxusercontent.com/u/124 ... 2014-01-25 19:09:35

    I know it's been a long time but I have made some more updates to this demo. Updated 6/29/2016

    https://www.dropbox.com/s/6x3rl4dsnweterp/CardGame12.capx?dl=0

  • It seems you certainly know what you are doing... Achieving that all with 33 events is an achievement!

  • It's pretty nice. I don't thnik you can squeeze a lot more of improvements, but if you want, you can use the Randomized Array to do the shuffling automatically on start. (That's what I used in my Fifteen game sample).

  • you can use the Randomized Array to do the shuffling automatically on start

    Is there a tutorial that explains step by step how to use this array object?

    I have tries to self explore but without sucess.

    <img src="smileys/smiley5.gif" border="0" align="middle" />

  • Maxum, your way to shuffle is awesome. I never thought of something like that to permutate stuff around the first index from loop to loop is great.

    I would have done something like keeping a string of 52 tokens and just pick randomly each token, fill the Array and delete them, until there's no token to pick.

    But your way is far better.

    The rest of the code is a bit heavy though, so let me show you how to simplify :

    shuffleCard.capx

    in just 14 events

    vidi I don't know if there's a tuto, but in short you can set value to indexes like X or X,Y or X,Y,Z

    Array is just a data container. Test some stuff and come back with question if you don't understand what's going on.Yann2011-11-05 13:24:37

  • You know if C2 had a z-index, and a for each ordered, you wouldn't need the array at all. The cards themselves could be the array. To shuffle all you would have to do is:

    For each card set z-index to random(52)

    Then you could even un-shuffle them:

    For each card (ordered by instance variable book)

    -> For each card (ordered by instance variable number)

    --> card set z index to loopindex

    Easy peasy

  • you can use the Randomized Array to do the shuffling automatically on start

    Is there a tutorial that explains step by step how to use this array object?

    I have tries to self explore but without sucess.

    <img src="smileys/smiley5.gif" border="0" align="middle">

    Have you looked at the example?

    Example Card shuffle

    it' not very short and tricky but should explain how to use the plugin.

    Joe7

  • Thank you Joe7 It looks like I was looking for.<img src="smileys/smiley1.gif" border="0" align="middle" />

    Can you tell me wich plugin I need? I can not open this exemple.

  • Thanks Yann for checking out my project and showing me how to simplify it. I have to say your way is more elegant and efficient.

    I have made a few more changes.I am now using   rexrainbow Behavior plugin -- MoveTo it lets you move the sprite to the target position straight from current position.

    Here is the new capx Card Game Update

    And if you do not want to install the plugin you can see it here Card Game

    Thanks agian Yann and the others that took the time to check out my project. If any one else has any more pointers I like the hear them as I am trying to learn the best way to use Construct 2.

  • Right now I am trying to figure out the best way to enter in the card face value into my array system. I am thinking about using a three dimensional array. X would be the card deck number. Y would be the card face value. Z would be the ace check / alternate face value.

    I am trying to figure out the best way to code this. I would love to see your ideas on this.

    Thanks

  • 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
    • 3 = spades

    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

  • 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.

  • Thank you Joe7 It looks like I was looking for.<img src="smileys/smiley1.gif" border="0" align="middle">

    Can you tell me wich plugin I need? I can not open this exemple.

    Plugin Random Array

  • Construct 3

    Buy Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Buy Now Construct 3 users don't see these ads
  • 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

  • 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?

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)