I’m working on a Monopoly based game with properties, money, cards, etc. I recently ran into a problem when working on the chance card aspect of the game…
I have an array of Strings that say things, ex,”Tax refund; collect $25″ or “You lose money due to stocks; -$100”. Each card does something different, not all of them just deal with money. My question: how can i store these cards to each hold a string with its description but also any action that is involved. For example, card1 has a String and an int value (-25), but on the other hand another card, card2 has a String, an int value(+10) and a property. Sorry if the question is really vague but I don’t know how else to describe it.
Just to clarify:
A card could contain simply the description and a money value. While another card might contain a description, money value and move certain spaces.
Thanks to everyone who gave out awesome ideas so quickly!
If your range of actions is very limited (say, 2 or 3 actions involving money, move squares, etc) then I might use the following class:
This way (as already pointed out by some other answers), you can use an array of
Cards instead of array ofStrings. You can then have text in one field, the action in a separate field, and the value associated with that action in a third field. For example, you could have the following cards:When you’re ‘processing’ the cards, you can do so in the following manner:
EDIT:
If the cards can hold more than one action, you can use a HashMap to store actions for that card:
A
HashMapis a collection that can store key-value pairs. So for a card that has 2 actions, you can use the above code as:Now, when you’re actually processing the cards, you need to check the
HashMapfor all actions stored in this card. One way to iterate through theHashMapis to do the following: