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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.