Jump to content
MakeWebGames

Crime Feature with percentage outcomes


Recommended Posts

Posted (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 by tprice88
Posted

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. (:

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...