I want to use the “Gold, Silver Bronze” money system in my game. My addCoins function works as I want it to… I’m having trouble thou with my removeCoins function… Is there anybody who can help me out with it?
I’m not sure what I’m going to tell you is what you want but in fact if I want to use a system of Gold,silver,bronze coin in my game I think i would use just an integer.
so in fact having 1 gold is same as having 1 00 00 bronze coins
When you display how many you have on your screen (or in debug log) maybe you just have to enter your number in a table (for exemple) and use the two last field of your table as bronze coin, the two before as your silver coin and firsts fields as gold coins.
yes I think what you wrote should work.
Oh last thing, I think you should not insert code line after a “return”, because when a compiler read a “return” it doesn’t read what you have after
// _coins = 55320 for example
c = _coins % 100; // c = 553,2 and it's a FLOAT not a INT
_coins = (_coins - c) / 100; //_coins = (55320 - 553.2) / 100 = 547.668 ?????
s = _coins % 100; // s = 547.668 % 100 = 5.47668 ..... etc....
g = (_coins - s) / 100;
I don’t think this part of the code will works…
I recommend you to use this kind of code :
gold = coin /10000; g = (int) gold;
silver = (coin - (g * 10000))/100; s = (int) silver;
copper = coin - (g * 10000) - (s * 100) ; c = (int) copper;
Why are there floats there at all? You’re dealing in discrete quantities of coins. Keeping things to int makes it simpler:
private int[] CalculateCoins ( int total )
{
int[] results = new int[3];
results[0] = total % 100; // Copper
total /= 100; // Divide the total, so it now represents silver
results[1] = total % 100; // Silver
results[2] = total / 100; // Gold
return results;
}
Or to make it more general:
private int[] CalculateCoins ( int total, int numTypes, int step = 100 )
{
int[] results = new int[numTypes];
for(int i = 0; i < numTypes - 1; i++)
{
results[i] = total % step;
total /= step;
}
results[numTypes-1] = total;
return results;
}
I actually would prefer Properties, as Gold and Silver are easily derived values and don’t need to be stored on their own. Also, I’ve never liked magic arrays, where you mentally associate an index with a variable (gold =0, silver =1, copper =2). Not to difficult in the case of currency, but a practice I discourage in general.
private int _coins;
public int Gold {get {return Silver/100;} } //or _coins/100000
public int Silver {get {return _coins/1000;}}
public int Copper {get { return _coins; }}
If you don’t do the property route that’s fine (especially since it could force you up to a float if you have a LOT of gold), but I recommend your own currency data structure instead of a magical int[3]. I REALLY hate magical arrays if you can avoid them.
Yes, definitely do not store separate values of gold/bronze/silver whatever when in fact they are just handy denominations, powers of 10 or 100 in your case.
As suggested multiple times above, (including by the OP), keep a single value of your riches, and then display it however you like, by a modulo-and-devide process.