tprice88 Posted August 8, 2012 Posted August 8, 2012 (edited) This has ben really hard for me I have been working on this for 3 days and am about to give up lol... :( I'm trying to code a feature in my game that the outcome is successful based on a percentage, and this percentage needs to go up the more you use the feature. You start low at 1% and would goto jail most of the time, or just fail the feature. But as u do it more at hit 100% you would be successful every time here is the code I got so far. If anyone knows the correct way to code this and point me in the right direction it would be very helpful I'm pretty sure I have to recode most of this. if crime chance hits 100 this way itll always be 1 so u will always be succeful but there must be a better way to do this with percents. crimechance1 is set to default 1 so it starts as 1 and goes up each time u do the function at 100 u will always complete the function.. i need this to work at percents though example at 60 u have 60% chance of passing at 80 u have 80% chance of passing. <?php if ($_POST['Commit']){ $radiobutton=$_POST['radiobutton']; if ($radiobutton == "1"){ $sql = "SELECT * FROM users WHERE id='".mysql_real_escape_string($_SESSION['user_id'])."'"; $query = mysql_query($sql) or die(mysql_error()); $row = mysql_fetch_object($query); $crimechance1 = htmlspecialchars($row->crime1); $crime_chance = 100 / $crimechance1; $crimechance1++; if ($crime_chance == 1){ $result = mysql_query("UPDATE users SET exp='100' WHERE name='$name'") or die(mysql_error()); echo "You passed the Function"; } else { echo "You failed the Function"; } } } ?> Edited August 8, 2012 by tprice88 Quote
Djkanna Posted August 8, 2012 Posted August 8, 2012 A way, would be to store the amount of times that a user has used said feature, I see you're selecting something to do with their chance, but aren't updating the table after they've 'used' the function. Getting a percentage is the easy part (actually you don't need to do any calculations for it, providing you know the percentage, 35% of 100 is, well, 35.) As for using percentage in determining the results, I'm a little shady on. Perhaps this would work, essentially generate a number between 1 and 100 in your case. , then check to see if the random number is between 1 and the percent chance (inclusive). This way you have a 40 in 100 chance of passing, and a, well, 60 in 100 of failing. I think, I could be wrong or someone could provide a more elegant solution, but doesn't hurt to give this a try. $perc = 40; $rand = rand(1, 100); if ($rand >= 1 && $rand <= $perc) { //Probably could just do if ($rand <= $perc) { //pass } else { //Fail } Note: Incredibly tired, so I take no responsibility in breaking something/or being wrong and wasting your time trying it out. (: Quote
tprice88 Posted August 8, 2012 Author Posted August 8, 2012 Thanks, that outcome is working much better then mine :cool: 1 Quote
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.