arrays - Java Monopoly-ish Game -
arrays - Java Monopoly-ish Game -
i'm working on monopoly based game properties, money, cards, etc. ran problem when working on chance card aspect of game...
i have array of strings things, ex,"tax refund; collect $25" or "you lose money due stocks; -$100". each card different, not of them deal money. question: how can store these cards each hold string description action involved. example, card1 has string , int value (-25), on other hand card, card2 has string, int value(+10) , property. sorry if question vague don't know how else describe it.
just clarify:
a card contain description , money value. while card might contain description, money value , move spaces.
thanks gave out awesome ideas quickly!
if range of actions limited (say, 2 or 3 actions involving money, move squares, etc) might utilize next class:
class card { // practice not create next fields // public , utilize getters/setters instead i've made them // way illustration purposes public string text; public string action; public int value; card(string text, string action, int value) { this.text = text; this.action = action; this.value = value; } }
this way (as pointed out other answers), can utilize array of card
s instead of array of string
s. can have text in 1 field, action in separate field, , value associated action in 3rd field. example, have next cards:
card lose25 = new card("you lose $25", "money", -25); card move5squares = new card("move 5 squares ahead!", "move", 5);
when you're 'processing' cards, can in next manner:
... if (card.action.equals("money") { // update user's money card.value } else if (card.action.equals("move") { // update user's position card.value } else if (card.action.equals("...") { // , on... } ...
edit: if cards can hold more 1 action, can utilize hashmap store actions card:
class card { public string text; public hashmap<string, integer> actions; card(string text) { this.text = text; actions = new hashmap<string, integer>(); } addaction(string action, int value) { actions.put(action, value); } }
a hashmap
collection can store key-value pairs. card has 2 actions, can utilize above code as:
card acard = new card("lose $25 , move 3 spaces!"); acard.addaction("money", -25); acard.addaction("move", -3);
now, when you're processing cards, need check hashmap
actions stored in card. 1 way iterate through hashmap
following:
card processcard = ...; (map.entry<string, integer> entry : processcard.actions.entryset()) { // loop each 'action' , 'value' added // hashset card , process it. string action = entry.getkey(); int value = entry.getvalue(); // add together before 'card processing' code here... if (action.equals("money") { // update user's money value } else if (action.equals("move") { // update user's position value } else if (action.equals("...") { // , on... } }
java arrays swing
Comments
Post a Comment