Card Game - How to compare arrays?

1 favourites
From the Asset Store
Card Game
$49 USD
Card Game Sound Effects Kit delivers the right variety of sounds to outfit your game.
  • Hello everyone. I am currently working on a card game. The game has 42 different (unique) cards. The cards can have up to 4 different values.

    I have now created a separate array for each card with 1-4 elements.

    The idea is, similar to a Uno card game, that the player can put certain cards only on other cards.


    Card 1 = 1,2,4,6

    Card 2 = 3,6,9

    Card 3 = 5

    Card 1 and 2 are compatible, card 3 can neither be placed on card 1 nor on card 2.

    I would now like to compare the arrays with each other.

    What is the best way to do this? Do you have any ideas?

    Translated with (free version)

  • You should check out the documentation for the Array type as a reference.

    How you want to compare them is dependent on how you intend the game logic to work (which you didn't specify), but the Compare At X condition will let you select one array at a given position. You can then use the Array.At(X) expression to select the other array you want to compare.

    If you want to know the number of elements in a given array, you can use the Array.Width expression and use some system comparison condition to compare it with some value.

    Hope this helps.

  • Thank you for the quick reply.

    I want to check if at least 1 value matches on two cards. So I have to compare all values of array_1 with all values of array_2. If at least one value is identical in both arrays, the cards may be placed on top of each other.

    Array.At(X) doesn't seem to be the right command to read all values of an array, does it?

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • You can use the condition Contains Value to see if a value is in an array

    You can also use a For Each Element condition and use a sub-event if you wanna do this for all values (where the expression CurValue is the current element in the looping array)

    No easier way to do this that I know of

  • I am to dumb. It´s sad to say but I can´t get things running.

    Maybe I should give up and try something less complicated :-/

    Sorry. Maybe my mood will be better tomorow, but today I am very frustrated, because its very logic and easy to explain, but I can´t get it. Maybe I should switch and work with variables.

    Don´t know.

  • Here is what I got so far...

    Maybe you get an idea of the game...

    Its a game about learning the basics of multiplication.

    1x1, 2x1, 3x1, 4x1 ... (1,2,3,4...)

    1x2, 2x2, 3x2, 4x2 ... (2,4,6,8...)

    1x3, 2x3, 3x3, 4x3 ... (3,6,9,12...)

    As you can see.. Some Numbers are in several rows...

    What do you think? Is working with array easier than working with variables?

  • I don't quite get the general idea, but arrays are a great resource for large data sets or just data sets in general really.

    Since I didn't understand perfectly what you were aiming for I'll try to give just a general advice that works wonders for me. Subdivide your problems into smaller problems. If you solve each part of the problem like, "a: get things from somewhere in the array" then "b: set a value somewhere in the array", etc, whatever your problems are, you'll be able to find where things go awry faster.

    I'm sorry I couldn't be of much help, but I feel that when you subdivide your problems, your questions will also be sorta "subdivided" and therefore easier to answer possibly.

    Also I think this is a nice initial project, don't give up on it just yet, I'm definitely positive you won't regret it when you see it working.

  • Thank you dupuqub.

    You are right!

    Breaking the problem down into its component parts should help in any case. I always find myself working around the actual problem. I will keep at it. I just hate it when I don't get anywhere fast enough.

  • Here is the core of my problem:

    "Find equal values in two arrays"

    array_01: 1,2,5,10

    array_02: 3,5,6

    How can I do that?

    Can you please help?

  • I took your sample file and simplified things - used one Sprite object "Card" and added animations for all the individual cards, and added instance variables for the 4 values.

    this eliminates a lot of duplicate code, and requires only one array "Current_Card".

    when a card is played, it loads the instance variables into the Current_Card array, then when another card is attempted to be played it uses IndexOf to see if any of the new card's values are in the array...

    CicadaGames solution should work fine (comparing every value in one array to each value in the second array) but I still think you should try to eliminate all the duplicate code and excess arrays.

    EDIT: fixed a bug - the reset function should reset the Current_Card array, and I decided to use array contains value instead of IndexOf to make the code a little easier to read.

  • OMG. You guys rock! I know that my code was very ugly.

    I am not a programmer (as you can see) but actually work as an illustrator. I already thought that this is so very cumbersome, but better ugly code than no code :D

    I'll have a deeper look at later.

    But for now: Thank you, thank you, thank you!

  • AllanR

    Now I see what you did. This is so much better. Thank you!

    Btw, where did you get the "missing" cards? ;-)

    Can you tell me how you choose what card will be selected at the start?

  • I just copied and pasted digits from other cards in the C3 image editor - didn't put in any effort to line things up so they are not perfect... and then gave up at 15 because that was enough to get the point.

    I made 4 instances of "Card" on the layout and made them the same as the 4 you choose for your example - so testing would be easy.

    if you wanted to randomize it I would make a "CreateCard" function that would create the instance, pick a random card value (from 1 to the highest card number), and then load the instance variables from a master array (2 dimensional, where x is the card number, and then 4 y values).

    it gets a little trickier if you want to make sure there are no duplicate cards, or if you want to make sure there is a possible way to play all four cards...

    to make sure there are no duplicates, I would make a master deck of cards, and then randomly pick from that deck (deleting the card from that deck so it can't get picked again).

  • Because of the values* of the cards I thought about putting them all in an array and sort them randomly. Let's see if it works.

    *(1,2,3,4,5,6,7,8,9,10,12,14,15,16,18,20,21,24,25,27,30 ... 80,81,90,100)

    As you can see some values are missing.

    I have seen that you use: pick CARD instance X

    Is it possible to assign these instance numbers?

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