http://qs321.pair.com?node_id=11110647


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.