Dr.Altaica has asked for the wisdom of the Perl Monks concerning the following question:
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: How tell if you can play a card with multi type 'Mana'
by jcb (Parson) on Dec 26, 2019 at 23:43 UTC | |
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)
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. | [reply] [d/l] [select] |
|
Re: How tell if you can play a card with multi type 'Mana'
by roboticus (Chancellor) on Dec 27, 2019 at 04:30 UTC | |
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:
and your players were defined like:
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:
So, putting it all together you'd get something like:
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. | [reply] [d/l] [select] |
by Dr.Altaica (Scribe) on Dec 30, 2019 at 01:34 UTC | |
| [reply] |
|
Re: How tell if you can play a card with multi type 'Mana'
by bliako (Abbot) on Dec 27, 2019 at 10:31 UTC | |
I would understand more if you described this situation in more general terms. From what you described, a Tree with many branches at any node (n-ary tree) may be a suitable data structure to evaluate all scenaria at any depth you wish, given available resources. There are algorithms to find the route with the least cost. A hash (of hashes) can also be used but you will have to implement the search algorithms yourself. In a multi-player game such trees are used to enumerate all available moves/choices/scenaria by all the players at any depth (given resources!ouch!) Then winning the game is a matter of following the branches which minimise your costs (and maximise your opponents'), re: the minmax algorithm, assuming your opponents also make reasonable moves. | [reply] |
by Dr.Altaica (Scribe) on Dec 30, 2019 at 01:45 UTC | |
| [reply] |
|
Re: How tell if you can play a card with multi type 'Mana'
by Anonymous Monk on Dec 26, 2019 at 21:30 UTC | |
| [reply] |
by Dr.Altaica (Scribe) on Dec 27, 2019 at 04:43 UTC | |
| [reply] [d/l] |