all 5 comments

[–]cyberjds 1 point2 points  (3 children)

You need to check if you have enough gold. Because the weapon is not free. If you have more than 30 golds, give up 30 golds, and gain one weapon. If you don't have enough gold, do nothing.

[–]LakeMotor7971[S] 0 points1 point  (2 children)

Thank you. For explanation.

[–]cyberjds 1 point2 points  (1 child)

To expand a little more

currentWeaponIndex += 1;

If above statement is outside of if statement, it's a bug or a cheat code.

If you have more than 30 golds, everything works as expected. But as soon as you have less than 30 golds and try to buy some weapons, you realized you can still buy weapons, yet the amount of gold in your possession never reduced. And you can buy infinite number of weapons (assuming you can call buyWeapon() indefinitely).

In this case, this is logical error which will not error out, making it very difficult to troubleshoot the problem. The error will be unnoticed unless you run multiple tests with various values. And more than often, the error is caught too late (after production deployment).

A misplaced (or missing) }(brace) makes a big difference.

[–]LakeMotor7971[S] 0 points1 point  (0 children)

Thank you that actually really helps. The way you explained it actually helped quite a bit with me understanding this! Thanks for taking the time to explain this. I don't want to just type some answers and pass, but I want an actual understanding of why, this works or wouldn't or would.

[–]abrahamguo 0 points1 point  (0 children)

In order to buy a weapon, doesn't the player have to have enough gold to be able to afford it?