Implement purchase logic for a gem-based card game.
There are five gem colors: BLUE, WHITE, GREEN, RED, and YELLOW.
Each Card has:
-
a
color
representing the permanent discount it grants after purchase
-
a
cost
map from gem color to required quantity
Each Player has:
-
a
gems
map from gem color to current quantity owned
-
a
hand
containing purchased cards
-
a
discountMap
where
discountMap[c]
equals the number of previously purchased cards of color
c
Rules:
-
When buying a card, the player gets a discount on each gem color equal to the number of owned cards of that same color.
-
Example: if the player owns 2 white cards and 1 red card, then a card costing 4 white and 1 red has discounted cost 2 white and 0 red.
-
A discounted cost for any color cannot go below 0.
-
canPurchase(card)
should return
true
if the player can afford the card after applying discounts, otherwise
false
.
-
purchase(card)
should:
-
return
false
and change nothing if the card is not affordable
-
otherwise deduct the discounted gem cost from the player's gems
-
add the card to the player's hand
-
increase the discount count for the purchased card's
color
by 1
-
return
true
Write the logic for canPurchase and purchase correctly.