Dr.Altaica:

You can do it by using a hash to represent the "cost" of the card/spell and use a hash with an identical structure to show the current resources for each player. As an example, suppose you defined your cards something like this:

my @cards = ( { name => "Fireball", cost => { fire=>2, air=>1 }, # other card information... }, { name=>"Ice wall", cost=>{ water=>3, frost=>5 }, }, { name=>"Lightning bolt", cost=>{ air=>2, electricity=>4 }, }, );

and your players were defined like:

my @players = ( { name=>"Joe", mana =>{ fire=>5, air=>5, electricity=>5 }, # other information about player }, { name=>"Audrey", mana =>{ water=>5, frost=>10 }, }, );

With this structure, each card has a "cost" hash and each player has a "mana" hash. All we need to do to find out if a player can cast a card, then, is to iterate over the resources the card requires and verify that the player *has* the resource, and if so, has *enough* of the resource. If any of the checks fail, the player can't cast the card, like this:

sub can_cast { my ($rPlayer, $rCard) = @_; for my $manaType (keys %{$rCard->{cost}}) { return 0 if ! exists $rPlayer->{mana}{$manaType}; return 0 if $rPlayer->{mana}{$manaType} < $rCard->{cost}{$mana +Type}; } # Nothing rejected, so we can cast it! return 1; }

So, putting it all together you'd get something like:

$ cat mana.pl use strict; use warnings; my @cards = ( <<< snip >>> ); my @players = ( <<< snip >>> ); # Show which players can cast which spells for my $rPlayer (@players) { print "$rPlayer->{name} can cast: "; my $cnt=0; for my $rCard (@cards) { if (can_cast($rPlayer, $rCard)) { ++$cnt; print "$rCard->{name} "; } } if (!$cnt) { print "NOTHING!"; } print "\n"; } sub can_cast { <<<snip>>> } $ perl mana.pl Joe can cast: Fireball Lightning bolt Audrey can cast: Ice wall

The trick that makes it work so easily is we structured the data to make the problem simple: Each card may have many attributes you want to describe, but we made the cost of the spell a smaller hash inside the card definition. Similarly, the players are going to have various data you care about, but their mana resources are segregated into a hash. That way, we can verify the presence and value of each key in the 'cost' hash against the related 'mana' hash in the player object, and we never have to write any code where we care about the number of mana types or even what there names are to see if we can cast it or not.

I frequently use variations of this trick when programming: Choosing your data structures is every bit as important as writing your code. Do it well and things can be easy, do it poorly and your code can get ... difficult. ;^)

...roboticus

When your only tool is a hammer, all problems look like your thumb.


In reply to Re: How tell if you can play a card with multi type 'Mana' by roboticus
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.