armedunity.com uses cookies to improve user experience.

By continuing to use this site, you agree to allow us to store cookies on your computer.

• 0

# Generating balanced card hand

## Question

Imagine having a card stack, it contains a lot of cards that each have a value cost from 4 to 36

How does one generate a hand of 5 cards from a deck that has a sum value of <=60?

Do i start drawing a first card with a random value and then draw the other ones accordingly?

## Recommended Posts

• 1

First shuffle the deck (array of cards). Then you can use while(hand < 5) to keep looping until you get a full hand. Now the tough part bc I don’t know your code (how the values are laid out), I’m assuming there’s only 1 card of each value from 4-36. but you want to grab the first card (obviously). Then check if the second card is <= 60 - firstCard - 22 “4 + 5 + 6 + 7” unless one of those numbers was the first card.  And so on and so on. There’s too much stuff to explain but the jist is, make sure you don’t get to 60 before the 5th card and make sure there’s atleast 1 card left in the deck that will keep you under that value by card 5. You looking at a lot of comparing, and if you mess up, look forward to crashing your computer several times. So make sure all your checks are correct so you can break out of the while loop every time.

edit: and before either 4, 5, 6, 7, or 8 is chosen, replace it a value < 60 - hand. So if 9 is lower and you choose 5, value now has to be lower than 60 - hand - (4+9+6+7+8) depending on how many cards you still need in your hand. If you still need 3 cards then it’s just (4+5+6) or (4+9+6)

## Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

×   Pasted as rich text.   Paste as plain text instead

Only 75 emoji are allowed.