Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

Re: How tell if you can play a card with multi type 'Mana'

by jcb (Priest)
on Dec 26, 2019 at 23:43 UTC ( #11110639=note: print w/replies, xml ) Need Help??

in reply to How tell if you can play a card with multi type 'Mana'

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.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://11110639]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (5)
As of 2020-06-04 05:37 GMT
Find Nodes?
    Voting Booth?
    Do you really want to know if there is extraterrestrial life?

    Results (30 votes). Check out past polls.