DeanWhitehouse Posted July 21, 2008 Share Posted July 21, 2008 I am trying to make a script that will , depending on your experience , you will either succeed or fail. so for example if you have 15.5% experience you have a better chance of winning than if you had less. Any ideas, i cant work out any logic for this. Quote Link to comment https://forums.phpfreaks.com/topic/115920-stuck-on-chance-script/ Share on other sites More sharing options...
.josh Posted July 21, 2008 Share Posted July 21, 2008 There's really no way anybody can help you on this... first and foremost the formula is gonna have to be balanced with the rest of your game, which we know nothing about. For instance, is the base (no xp) supposed to be a 50/50 coin flip? And how much more per xp do you want it to lean in favor? A basic formula could be like so: start out with a 50/50 coin flip. That means there's 50 "points" to lean in your favor. Assuming xp goes from 0-100% you could do say, 0.5 points per xp % so if you had 10% xp the coin toss would be 55/45. But does that throw off the balance in your game? Only way you're gonna be able to figure that out is trial and error and tweaking it based on observation of a live game (be it a beta or final or w/e). Quote Link to comment https://forums.phpfreaks.com/topic/115920-stuck-on-chance-script/#findComment-595987 Share on other sites More sharing options...
DeanWhitehouse Posted July 21, 2008 Author Share Posted July 21, 2008 ok, this is the function i have made <?php function crime_chance() { $userid = $_SESSION['userid'] ; $row = mysql_fetch_assoc(mysql_query("SELECT * FROM users WHERE ID = '{$userid}'")); $chance = $row['crime_chance']; $random_chance = rand(0,100); echo $random_chance."<br>"; echo $chance; if ($chance >= $random_chance ) { $success = 1; } elseif($random_chance <= $chance) { $success = 0; } if($chance == 100) { $success = 1; } if($success == 1) { echo "<tr><td>Succeeded</td></tr>"; } else { echo "<tr><td>Failed</td></tr>"; } $add = rand(1,2); echo "<tr><td>Percent to add to exp ".$add. "</td></tr>"; } ?> Explanation of the game , what it will be is each time you commit a crime you gain either 1 or 2 percent more experience, and the chance of succeeding is based on how much experience you have. But the script we have i dont think it is very good, am i wrong ? Quote Link to comment https://forums.phpfreaks.com/topic/115920-stuck-on-chance-script/#findComment-595993 Share on other sites More sharing options...
ToonMariner Posted July 22, 2008 Share Posted July 22, 2008 Grab your percentages for your 2 crims... then calculate the relative chance by what portion of their overall chance they possess. Pass the 2 users chances to the 'best_crim' function and then see how won by generating a random number between 0 and 1 and comparing to the LARGEST relative power - if its great then the less likely crim wins!!! <?php function best_crim($crim1per, $crim2per) { $crimpower['crim1'] = $crim1per/($crim1per + $crim2per); / relative power of crim 1. $crimpower['crim2'] = 1 - $crimpower['crim1']; // relative power of crim 2. $perpetrator = do_battle($crimpower); return $perpetrator; } function do_battle($crimpower) { $prob = rand(0,1); // gen random criminality factor. if ($prob > max($crimpower)) { // probability is higher than most powerful crims relative share - they lose. $winner = array_keys($crimpower, min($crimpower)); } else { // probability is less than or equal than most powerful crims relative share they win. $winner = array_keys($crimpower, max($crimpower)); } return $winner[0]; } // some query to grab crim power of crims .... $crim1 = 0.23; //$crim1 = 23% $crim1 = 0.17; //$crim1 = 17% $the_crim = best_crim($crim1, $crim2); echo 'Perpetrator is ' . $the_crim; // add or remove % accordingly. ?> You could take the call to do_battle out of best_crim if you need to calculate that kind of thing else where but this should fit your current requirement. Quote Link to comment https://forums.phpfreaks.com/topic/115920-stuck-on-chance-script/#findComment-596040 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.