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

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

by roboticus (Chancellor)
on Dec 27, 2019 at 04:30 UTC ( #11110647=note: print w/replies, xml ) Need Help??


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

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.

Replies are listed 'Best First'.
Re^2: How tell if you can play a card with multi type 'Mana'
by Dr.Altaica (Scribe) on Dec 30, 2019 at 01:34 UTC
    A hash! of course that would work so much better than 15 scalers... My brain was a bit fried from trying to think of how the algorithm should work.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (7)
As of 2020-06-03 13:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you really want to know if there is extraterrestrial life?



    Results (24 votes). Check out past polls.

    Notices?