Got this example piece of code from a textbook I'm learning from and it's displaying some wierd bugs. It plays a game where soldiers are sent to battle and the casualties are displayed and only certain soldier types can take out other enemy soldier types but if you play a game and get to a point where, for example, both teams only have archers left it will state that other soldiers died in the battle despite there being none left. I won't put the whole code in but just what I think is the offending section but I can put the rest in if requested:
What is wrong with the code basically. I got it from a textbook so expected it to be correct and work but it doesn't, so I'm not learning much when the examples it gives are wrong. Just want to know how to fix it.
I think it's because they (the book people) did not put in all the error checking possible.
In this game swordsmen are killed by archers.
So for example, if you have no swordsmen left and the enemy has 8 archers left .
then swordsmen_dead = 3 * g_archers; will give a value of 24 swordmen on your side killed - even though as we say you
have no swordsmen left to kill.
I think that jaeger can give it a go to see if he can add more error checking and other
logic to the game.
The code is most definitely missing some error checking and I am coming up with strange results when people are dead, as guestgulkan says. The other issue is that on some occasions the game leads to a stalemate when both teams have only the same units left. If both teams have only archers left you can't win or lose because archers can't kill eachother. All in all, I think it's a poorly thought through example and I'm really dissapointed in this book as I seem to be left with more questions about why the code I'm writing is wrong than correct code and knowledge of how it works.
I'll give it a shot to make it better but I've only just started learning, so thanks for the help guys.
Also, this is a second edition of the book and I think it's the most recent as well so I don't think there is an errata of it after a quick search. Any bugs and glitches I'm just going to have to deal with until I can find a better book.
I'm thinking that the problem lies within the way the deaths are calculated rather than the logic and error checking. If the calculation was fixed it may solve the problem. I haven't done maths in a while and need to take a refresher but can anyone think of a calculation that would take into account the amount of soldiers the player has and then calculates based on that amount and the amount of enemy soldiers?
Promise I won't ask for maths again, just this once. Really just want to get this program fixed so I can move on from it and maybe learn some maths along the way.