# Calculating kills and deaths in a game

I'm trying to make a program that calculates your k/d ratio (kill/death, which is used in FPS games to make people believe that it's skill), how many kills you need without dying once to reach a goalKD. It also has a part that calculates how many battles you need if you give the program your average battle kills and battle deaths.

Is there a efficient way to program it? Currently, the way I'm doing it is getting me into programmer-efficiency hell.

For i = 1 to 100000 {
While GoalKD>KDratio {
Kills = BattleKills + Kill
Deaths = BattleDeaths + Death
KDratio = Kills / Deaths
i++
}
}


I'm programming this in SmallBasic because I'm most familiar with it, and I think it's easily readable. Also, if possible, don't give the answer right away; give me some hints for me to practice my mind. Add the answer in a spoiler box.

• There is nothing that changes in the while loop that affects its condition, so you would get stuck there. – Guffa Dec 20 '13 at 1:36
• Always prefer ++i over i++. When you do i++, it sets a temp value to i, then adds one to i, then returns the temp for you to use. With ++i, it increments i, then returns i. Very very small efficiency gain, but still worth it. – Kieveli Dec 20 '13 at 13:13
• As far as general hints, there a reason why there are a lot of math classes for CS majors. Some problem just get much less complex if you use math on them instead of trying to bruteforce a solution with general computing principles. – Christian Dec 20 '13 at 14:05

## 1 Answer

For the kills needed part, you are trying to solve this equation:

k{current} + n
--------------  = r
d{current}


Where r is the target rate and n is the number you're looking for. Some basic algebra:

n = rd - k


For the battles part, you need to solve

k{current} + b * k{battle}            // current kills + additional kills
--------------------------  = r
d{current} + b * d{battle}            // current deaths + additional deaths

k{current} + b * k{battle} = r * (d{current} + b * d{battle})  // multiply both sides by d{current} + b * d{battle}

k{current} + b * k{battle} = r * d{current} + r * b * d{battle})  // just showing multiplication of r

b * (k{battle} - r * d{battle}) = r * d{current} - k{current}  // subtracting terms from each side

r * d{current} - k{current}
b =  ---------------------------  // dividing by k{battle} - r * d{battle}
k{battle} - r * d{battle}


In your code above, this becomes

need = Math.ceiling( GoalKD * Deaths - Kills )
battles = Math.ceiling( ( GoalKD * Deaths - Kills ) / (BattleKills - GoalKD * BattleDeaths) )


[I'm not a SmallBasic guy, so please forgive any syntax errors above.]

(Edited to fix math error re battles required and add comments to the math.)

• The code works perfectly, however I don't understand the r that's multiplying dbattle at the 3rd line? Where did it come from? – user112289 Dec 20 '13 at 12:45
• I added some comments above. When you multiply both sides by d{current} + b * d{battle}, you end up with a term r * b * d{battle} on the right. Subtracting that from both sides moves it to the left. Pulling b out of both terms on the left gives you the r * d{battle} term on the next to last line. – elixenide Dec 20 '13 at 13:11
• SmallBasic, not SmallTalk ;) – konijn Dec 20 '13 at 15:14
• @tomdemuyt, good point. I'm not a SmallBasic guy, either! :) – elixenide Dec 20 '13 at 15:17
• Glad to help out! And they say algebra is useless in the real world... :) – elixenide Dec 20 '13 at 18:52