Jump to content

working with percentages help!!


seany123

Recommended Posts

Im making a game and i need help with one of the parts of the game.

 

im doing a crime mod which basically gives people exp and takes away 'nerve'.. that works fine, but obviously because i havnt coded it yet, it suceeds everytime.

 

this is the code:

<?php
include("lib.php");
define("PAGENAME", "Badge Car");
$player = check_user($secret_key, $db);

$training[] = "you Badged a Car!";


if ($_GET['act'] == "go")
{
if ($player->nerve <= 0)
{
	include("templates/private_header.php");
	echo "You don't have enough nerve left!";
	include("templates/private_footer.php");
	exit;
}

$expgain = 10; //Random EXP gains
$trainkey = array_rand($training); //Random training message
$output = $training[$trainkey];

if ($expgain + $player->exp >= $player->maxexp) //Player gained a level!
{
	//Update player, gained a level
	$output .= "<br /><br />You gained <b>" . $expgain . "</b> EXP and level up!";
	$newexp = $expgain + $player->exp - $player->maxexp;
	$query = $db->execute("update `players` set `stat_points`=?, `level`=?, `maxexp`=?, `maxhp`=?, `exp`=?, `gold`=?, `hp`=?, `nerve`=? where `id`=?", array($player->stat_points + 3, $player->level + 1, ($player->level+1) * 70 - 20, $player->maxhp + 30, $newexp, $player->nerve - 1, $player->id));
}
else
{
	//Update player
	$output .= "<br /><br />You gained <b>" . $expgain . "</b> EXP!";
	$query = $db->execute("update `players` set `exp`=?, `nerve`=? where `id`=?", array($player->exp + $expgain, $player->nerve - 1, $player->id));
}

$player = check_user($secret_key, $db);

include("templates/private_header.php");
echo $output;
include("templates/private_footer.php");
exit;
}

include("templates/private_header.php");

echo "So you've some for some training, huh?<br /><br />\n";
echo "<a href=\"crime1.php?act=go\">Train!</a>\n";

include("templates/private_footer.php");
?>

 

 

instead of whats above I want there to be a Chance of Failure... wait for it (based on another value 'awake'.

 

I want there to be a 80% chance of success if Awake is 100% - 75% full.

 

60% chance if awake 74% - 50%

 

40% chance if awake 49% - 25%

 

20% chance if awake 24% - 0%

 

I know this is very confusing but can it be done?

 

~ Sean

Link to comment
https://forums.phpfreaks.com/topic/125363-working-with-percentages-help/
Share on other sites

yes and is actually preety easy

here is an example

$awake = 1000; // lets say

if ( $awake < (($awake / 100) * 25)) {
$chance = 0;
} elseif ( $awake < (($awake / 100) * 50)) {
$chance = 25;
} elseif ( $awake < (($awake / 100) * 75)) {
$chance = 50;
} elseif ( $awake <= (($awake / 100) * 100)) {
$chance = 80;
}


 

Edit:  Bleh went afk half way through typing this.....  Posting it anyway.

 

 

 

No offense, but if you're making an OOP game, this shouldn't be very confusing.  Think about what you want to do.

 

 

 

You want to find the probability of something based on a seeded value.  So, first you need to decide which seed to use.  If the Awake value is 100-75, you want a 80% chance, else if... so on....

 

 

So:

 

if($awake >= .75) $chance = 80;
elseif($awake >= .5) $chance = 60;
elseif($awake >= .25) $chance = 40;
else $chance = 20;

if(rand(1, 100) <= $chance) { //I could've seen this part being confusing....  Kind of ghetto.
   echo 'Success!';
} 
else {
   echo 'Failure';
}

Corbin, that's pretty much how I did it.

 

seany123, I've written something similar - if you want the source PM me an email address and I'll send the lot to you, it's about 3.5MB zipped or you can have the ENTIRE source including graphics (Photoshop files etc.) and it's 19MB...

 

There's more than enough source there for you to learn by.

 

EDIT: I can upload the zip to my web space and PM you the link maybe?

if($awake >= .75) $chance = 80;
elseif($awake >= .5) $chance = 60;
elseif($awake >= .25) $chance = 40;
else $chance = 20;

if(rand(1, 100) <= $chance) { //I could've seen this part being confusing....  Kind of ghetto.
   echo 'Success!';
} 
else {
   echo 'Failure';
}

<?php

if( $awake >= 75 )
$max = 80;
elseif( $awake >= 50 )
$max = 60;
elseif( $awake >= 25 )
$max = 40;
else
$max = 20;

$rand = mt_rand(1,100);

if ( $rand <= $max )
echo 'Accomplished';
else
echo 'Failed';

?>

 

Deja vu ;)

OK I have to go to bed. The game isn't online any more, no idea what happened because I left the team a long time ago.

 

This was my first site using PHP and MySQL and I wrote 90% of what is in the archive.

 

http://www.pictureinthesky.net/files/gangstersonline.zip

 

The size is 19Mb.

 

I did find this little review: http://www.reviewcentre.com/reviews97610.html

 

There is a fair bit of "bad practice" going on there but hopefully someone can make use of it instead of sitting on my PC wasting away.

 

I've also started writing another - will gladly surrender the source if wanted but will have to upload tomorrow when I get home from work.

 

I'm only giving this away now that the website itself is offline.

Spamming yourself up to the top isn't going to get you any help.

 

On top of that, I don't think you're asking for help... you're kinda asking someone to do it for you.

 

We gave you working examples... it's up to you to figure out where they have to go. If you're really not sure, you've gotta start understanding your code better. If it's not your code, you're in the wrong section.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.