I have a game and can't come up with an Algo that can solve this problem.
In the game, you draw cards from a deck. The deck has 100 cards. Inside this deck are color cards of which there are 5 different types (ORANGE, YELLOW, BLUE, GREEN, and RED) and there are several other types of cards we'll just call "other". The player gets an opening hand of 5 cards out of these 100 dealt off the top of a shuffled deck.
When the player starts the game, they need to decide if they want to proceed with the hand they have. To help them make this decision, they can state what their "needs" are in terms of what they want to draw off the top of the deck.
For example, the player may wonder what their chances are of drawing a YELLOW color card within the next 3 cards off the top of the deck. So they would state this as a need, and we would compute the probability for them by running maybe 10,000 sims where we draw 3 cards off the top of the deck and count up how many times they find a YELLOW color card. This is a simple case with a single need.
The PROBLEM is how to evaluate more complex needs. The user can state multiple needs while inserting brackets, AND, or OR clauses. For example, they may be interested in the following 3 needs:
need_1: YELLOW color within the first 3 cards
need_2: ANY color within the first 4 cards
need_3: BLUE color within first 5 cards
And they've built the following need string
( need_1 AND need_2 ) OR ( need_3 )
So to evaluate this, we would draw 5 cards in each simulation (because that's the most needed to satisfy all needs). After each 5 card set, we could evaluate each need individually, plug the result into the need string, then use eval to evaluate the entire string to True or False. If the entire string evals to true, the need string has been satisfied and its a success case.
For example, one sim is:
SIM 1:
card1: other card
card2: yellow color
card3: other card
card4: other card
card5: blue color
So:
need1 was satisfied (yellow drawn is <= 3 card limit)
need2 was NOT satisfied (yellow is any color, but that card is already used for need1, and no other color cards were drawn by card 4)
need3 was satisfied (blue drawn is <= 5 card limit)
And the overall need string would be
( TRUE and FALSE ) or ( TRUE )
Which would evaluate to TRUE, so in this particular simulation, the 3 needs were met.
This is actually much less straightforward than it sounds because I don't want the same card to meet multiple needs within an AND clause, but the same card can meet multiple needs between OR clauses.
So if you state as your needs string:
( need_1 AND need_2 AND need_3 )
The same card can't be used to meet all 3 needs, because user is stating they need 3 seperate cards, and these 3 seperate cards must meet all 3 criteria for success.
If on the other hand your needs string is
( need_1 OR need_2 OR need_3 )
We only care if 1 of these needs is met, so the same card can meet each need.
Sounds simple? What if we have
( need_1 AND ( need_2 OR need_3 ) AND need_4 )
Now what do I do? The same card can't be used for need_1, need_4, or the ( need_2 OR need_3 ) clause, but within that clause, the same card can be used for need_2 or need_3. But then whatever card that is, it can't be used for need_1 or need_4.
I hope this is making sense. I need a way to evaluate these strings while ensuring:
a) clauses seperated by AND use different cards to satisfy needs or fail.
b) clauses seperated by OR may use the same card to satisfy needs, in fact only one need has to be met.
c) with more complex strings including brackets, we ensure we maintain this integrity.
d) its an algo that can handle VERY complex cases with lots of brackets, and a mix of AND and OR clauses.
Any help would be appreciated. Thanks