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

1 favourites
From the Asset Store
Simple but fun word game. Restore an original word from shuffled letters.
  • 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)

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

  • They are all global variables, so I set them to be a random number up to 1.

  • Are you trying to make it choose between 0 and 1?

  • Yeah, but at the same time I'm going to want to I prevent overlapping however I can.

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

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

  • Try Construct 3

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

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

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

  • 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

  • That makes a lot of sense when you put it like that, I will look more into this thankyou.

  • how do i keep the card from repeating???

    the random function sometimes picks up a prior value.

  • You can use this to get an array of unique numbers:

  • thank you for the response.

    I just started Construct 2.

    So I'm at scratch,,, is there any capx file for reference??

    I don't know how to input the array values to BHTSmartRandom

  • I don't have a card example, but you don't input the values, it will generate them for you. Give a range of 1 to 52, for example, and decode the cards from there. Or, use it four times for each suit from 1 to 13.

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