1
\$\begingroup\$

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.

\$\endgroup\$
3
  • 3
    \$\begingroup\$ There is nothing that changes in the while loop that affects its condition, so you would get stuck there. \$\endgroup\$ – Guffa Dec 20 '13 at 1:36
  • \$\begingroup\$ 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. \$\endgroup\$ – Kieveli Dec 20 '13 at 13:13
  • 1
    \$\begingroup\$ 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. \$\endgroup\$ – Christian Dec 20 '13 at 14:05
13
\$\begingroup\$

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.)

\$\endgroup\$
8
  • \$\begingroup\$ The code works perfectly, however I don't understand the r that's multiplying dbattle at the 3rd line? Where did it come from? \$\endgroup\$ – user112289 Dec 20 '13 at 12:45
  • \$\begingroup\$ 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. \$\endgroup\$ – elixenide Dec 20 '13 at 13:11
  • \$\begingroup\$ SmallBasic, not SmallTalk ;) \$\endgroup\$ – konijn Dec 20 '13 at 15:14
  • \$\begingroup\$ @tomdemuyt, good point. I'm not a SmallBasic guy, either! :) \$\endgroup\$ – elixenide Dec 20 '13 at 15:17
  • 1
    \$\begingroup\$ Glad to help out! And they say algebra is useless in the real world... :) \$\endgroup\$ – elixenide Dec 20 '13 at 18:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.