Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (4)
As of 2024-04-19 11:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found