So each "mana" unit satisfies one or two types? Are the "mana" units also cards in the player's hand? I was considering a simple search over the player's "mana" hand, but I am unsure how to resolve a situation where the cost is satisfiable, but not with the most obvious match. It seems that backtracking is needed to solve this in general, which leads to a regex solution: (untested)
sub can_play_card { my $cost = shift; # hashref of type => amount required my $mana = shift; # arrayref of mana types player holds my $pool = join '', map {printf "%2s", $_} sort @$mana; my $check = join '(?:..)*?', map {"(?:.$_|$_.)"} map {("$_") x $cost +->{$_}} sort keys %$cost; return $pool =~ m/$check/; }
The "magic" — pardon the pun — relies on the pattern constructed for $check and the string in $pool: each available mana card takes up exactly two characters and each required mana matches exactly two characters, with one of them being the required type. The sets are sorted to ensure that ordering will not prevent a possible match from being found. If managing the different types is part of the game's strategy, you probably want to allow players to choose which mana to use to pay a card's cost — this function can determine both whether a player can possibly play a card and if a player's choices for mana to use when playing a card are valid, depending on whether you pass in all the mana cards the player holds or only the cards the player has selected to play.
In reply to Re: How tell if you can play a card with multi type 'Mana'
by jcb
in thread How tell if you can play a card with multi type 'Mana'
by Dr.Altaica
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |