hyster Posted January 11, 2013 Share Posted January 11, 2013 is it possable to code this in php? its for a game and i want to be ale to work out how many battles i have to play to increase my winrate by 1% ive been old the equation below will do this but i have no idea how to code it. p and c comes from a html form p = Number of Battles played c = Win % w = Battles Won N = number of games need for 1% increase (w + N) = (C+1)/100 (p + N) thanks Quote Link to comment Share on other sites More sharing options...
requinix Posted January 11, 2013 Share Posted January 11, 2013 What have you done so far? Even if it doesn't work it's better than nothing. Quote Link to comment Share on other sites More sharing options...
hyster Posted January 11, 2013 Author Share Posted January 11, 2013 this what i got so far. a friend who understands algebra more than me tryed to explain bit by bit and i tryed to code it as i went but i got lost quickly lol only thing missing is the form $bat = $_GET["battle"]; $win = $_GET["winr"]; echo "Battles :".$bat."</br>"; echo "Win Rate :".$win."%</br>"; $step1 = $bat * ($win/100); echo "Battle Won :".$step1."</br>"; $increase = 1; $step2 = $step1 * (1 + ($increase/10)); echo "</br>"; echo "</br>"; $step3 = $step2 - $win; echo $step3; Quote Link to comment Share on other sites More sharing options...
Christian F. Posted January 11, 2013 Share Posted January 11, 2013 (edited) To solve an equation in a programming language, you'll want to move all the operations to one side. Which makes the entire equation equals 0. In this case, the equation would look like this then: 0 = (C+1) / 100 * (p+N) - (w+N) Since you know the number of battles (p) and the win percentage (C), you also know the number of battles won (w). Which leaves only the number of battles needed (N) as the remaining unknown factor. Personally, I'd use number of battles and number of battles won, and then calculate the percentage from that. Seeing as the percentage is a calculated value anyway, which is derived from the two integers p and w Anyway, that's a digression. Back to solving the equation... We continue by expanding the parentheses, which is done by multiplying (or dividing, as the case might be) the value on the outside with the values on the inside. That leaves us with the following: 0 = (C*p + C*N + 1*p + 1*N) / 100 - (w+N) Then we resolve the division, using the same rule, and expands the final pair of parentheses by multiplying their values with -1: 0 = C*p/100 + C*N/100 + 1*p/100 + 1*N/100 - w - N Having the /100 bit in there makes things look a bit more complicated than necessary, so let's just multiply everything by 100 to get rid of it: 0 = C*p + C*N + p + N - 100*w - 100*N What we need to do now, is to move all of the N-values to one of the equation: 100*N - C*N - N = C*p + p - 100*w Collapsing the 100N - N gives us 99N, and by extracting the N-value (by adding a pair of parentheses) we make the equation look like this: N*(99 - C) = C*p + p - 100*w Once that is done, we can finally move the rest of the non-N-values to the right hand side again, by dividing with the parentheses. N = (C*p + p - 100*w) / (99 - C) Now that we got this, we can substitute the letters with the known PHP variables. Let's say that the following is true: // Known p = $Battles w = $Won C = $Percent = round ($Won / $Battles * 100, 0) // Unknown N = $Needed That gives us a PHP equation which looks like this: // Retrieve the number of battles from the URL. $Battles = (int) $_GET['battles']; $Won = (int) $_GET['won']; // Calculate the percentage of battles won. $Percent = round ($Won/$Battles*100, 0); // Calculate the number of battles needed to increase win percentage by 1%. $Needed = ($Percent*$Battles + $Battles - 100*$Won) / (99 - $Percent) Hopefully that was a bit more helpful and enlightening than what you found your friend to be? PS: I know I've done a couple of unnecessary steps in here, and I did them in an attempt to further break down the process and make it easier to understand. Edited January 11, 2013 by Christian F. Quote Link to comment Share on other sites More sharing options...
Solution Barand Posted January 11, 2013 Solution Share Posted January 11, 2013 I got similar to Christian but as you can't win part of a battle I added ceil() $n = ceil((100*$won - $battles * (1 + $winrate))/($winrate-99)); Quote Link to comment Share on other sites More sharing options...
hyster Posted January 11, 2013 Author Share Posted January 11, 2013 massive thanks for the help guys. ill try and wade through it and make sense. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.