Jump to content

Recommended Posts

Okay well im creating a game and its now starting to get to the hard bits of coding.

ill explain exactly what im trying to do then maybe somone can point me into the right direction.

 

First,

Im trying to create a Crime mod which based on different values in the database, the player will succeed, fail or fail & prison.

 

So what im needing first is statement which will check what percentage of "maxnerve", "nerve" equals.

 

if ($player->nerve < $player->maxnerve * 0.25) {
}

else if ($player->nerve >= $player->maxnerve * 0.25 || $player->nerve < $player->maxnerve * 0.50) {
}

else if ($player->nerve >= $player->maxnerve * 0.50 || $player->nerve < $player->maxnerve * 0.75) {
}

else if ($player->nerve >= $player->maxnerve * 0.75 || $player->nerve < $player->maxnerve * 1) {
}

else if ($player->nerve >= $player->maxnerve * 1){
}

 

So maybe the code above works maybe it dont.

 

Second,

now i need each if statement to get 2 random numbers 1-100.

 

then ill need to do something like this...

 

If(randomnumber1 <= 10){crime sucess
}
else if(randomnumber1 >= 11 && randomnumber2 >= 10{ Crime Fail
}

else if(randomnumber2 >= 11 && randomnumber2 < 10 { Crime Fail and Prison
}

 

does anyone see what im getting at?

 

i know what needs to be done i just dont know how to do it,

 

all help would be great.

Link to comment
https://forums.phpfreaks.com/topic/134965-php-if-statements/
Share on other sites

Hello.

 

1. code look like is not working. You must use && and not ||

 

Then $player->nerve = 65

 

will be working

$player->nerve >= $player->maxnerve * 0.25 || $player->nerve < $player->maxnerve * 0.50

and not

else if ($player->nerve >= $player->maxnerve * 0.50 && $player->nerve < $player->maxnerve * 0.75) {

}

 

First,

Im trying to create a Crime mod which based on different values in the database, the player will succeed, fail or fail & prison.

 

So what im needing first is statement which will check what percentage of "maxnerve", "nerve" equals.

 

if ($player->nerve < $player->maxnerve * 0.25) {
}

else if ($player->nerve >= $player->maxnerve * 0.25 && $player->nerve < $player->maxnerve * 0.50) {
}

else if ($player->nerve >= $player->maxnerve * 0.50 && $player->nerve < $player->maxnerve * 0.75) {
}

else if ($player->nerve >= $player->maxnerve * 0.75 && $player->nerve < $player->maxnerve * 1) {
}

else if ($player->nerve >= $player->maxnerve * 1){
}

 

So maybe the code above works maybe it dont.

Second looks like is working fine. If you will be have problem, describe it.

Link to comment
https://forums.phpfreaks.com/topic/134965-php-if-statements/#findComment-702904
Share on other sites

for random numbers you may use

1. standart rand() function:

$random1 = rand(1,100);
$random2 = rand(1,100);

2. finde class for it at www.phpclasses.org/

3. write you own randnom function.

 

rand() function is not very good, like in any other language standart random function.

 

Link to comment
https://forums.phpfreaks.com/topic/134965-php-if-statements/#findComment-702915
Share on other sites

i wouldnt think... the random function would have to be very good.

 

as long as it does what it says it does give me a completely random number from 1-100.

 

 

maybe this isnt as hard as i first thought it would be....

 

im starting to understand how to do the seperate bits... but putting them all together is something completely different.

Link to comment
https://forums.phpfreaks.com/topic/134965-php-if-statements/#findComment-702917
Share on other sites

ive come up with this but when i run the page... it just goes all white...

 

so there must be errors.

 

Just to make it less confusing... after the first }} all the code pretty much repeats itself just with different values. i copy and pasted.

 

<?php	
if ($player->nerve >=1 && $player->awake < $player->maxawake * 0.25){
$random1 = rand(1,100);
$random2 = rand(1,100);

If($random1 <= 10){
$query = $db->execute("update `players` set `money`=?, `exp`=?, `nerve`=? where `id`=?", array($player->money + 10, $player->exp + 25, $player->nerve - 1, $player->id ));
echo "You Suceeded in your crime.";
echo "You gained $10 & 25 EXP.";
}
else if (randomnumber1 >= 11 && randomnumber2 >= 10){
$query = $db->execute("update `players` set `nerve`=? where `id`=?", array($player->nerve - 1, $player->id ));	
echo "You Failed your crime.";
}

else if(randomnumber2 >= 11 && randomnumber2 < 10) { 
$query = $db->execute("update `players` set `nerve`=? where `id`=?", array($player->nerve - 1, $player->id ));
echo "You failed your crime & you want sent to prison";
}
}

else if ($player->nerve >=1 && $player->awake >= $player->maxawake * 0.25 && $player->awake < $player->maxawake * 0.50) {

$random1 = rand(1,100);
$random2 = rand(1,100);

If($random1 <= 30){
$query = $db->execute("update `players` set `money`=?, `exp`=?, `nerve`=? where `id`=?", array($player->money + 10, $player->exp + 25, $player->nerve - 1, $player->id ));
echo "You Suceeded in your crime.";
echo "You gained $10 & 25 EXP.";
}
else if(randomnumber1 >= 31 && randomnumber2 >= 10){
$query = $db->execute("update `players` set `nerve`=? where `id`=?", array($player->nerve - 1, $player->id ));	
echo "You Failed your crime.";
}

else if(randomnumber2 >= 31 && randomnumber2 < 10) { 
$query = $db->execute("update `players` set `nerve`=? where `id`=?", array($player->nerve - 1, $player->id ));
echo "You failed your crime & you want sent to prison";
}
}

else if ($player->nerve >=1 && $player->awake >= $player->maxawake * 0.50 && $player->awake < $player->maxawake * 0.75) {

$random1 = rand(1,100);
$random2 = rand(1,100);

If($random1 <= 60){
$query = $db->execute("update `players` set `money`=?, `exp`=?, `nerve`=? where `id`=?", array($player->money + 10, $player->exp + 25, $player->nerve - 1, $player->id ));
echo "You Suceeded in your crime.";
echo "You gained $10 & 25 EXP.";
}
else if(randomnumber1 >= 61 && randomnumber2 >= 10){
$query = $db->execute("update `players` set `nerve`=? where `id`=?", array($player->nerve - 1, $player->id ));	
echo "You Failed your crime.";
}

else if(randomnumber2 >= 61 && randomnumber2 < 10){ 
$query = $db->execute("update `players` set `nerve`=? where `id`=?", array($player->nerve - 1, $player->id ));
echo "You failed your crime & you want sent to prison";
}
}

else if ($player->nerve >=1 && $player->awake >= $player->maxawake * 0.75 && $player->awake < $player->maxawake * 1) {

$random1 = rand(1,100);
$random2 = rand(1,100);

If($random1 <= 80){
$query = $db->execute("update `players` set `money`=?, `exp`=?, `nerve`=? where `id`=?", array($player->money + 10, $player->exp + 25, $player->nerve - 1, $player->id ));
echo "You Suceeded in your crime.";
echo "You gained $10 & 25 EXP.";
}
else if(randomnumber1 >= 81 && randomnumber2 >= 10){
$query = $db->execute("update `players` set `nerve`=? where `id`=?", array($player->nerve - 1, $player->id ));	
echo "You Failed your crime.";
}

else if(randomnumber2 >= 81 && randomnumber2 < 10) { 
$query = $db->execute("update `players` set `nerve`=? where `id`=?", array($player->nerve - 1, $player->id ));
echo "You failed your crime & you want sent to prison";
}
}

else if ($player->nerve >=1 && $player->awake >= $player->maxawake * 1){

$random1 = rand(1,100);
$random2 = rand(1,100);

If($random1 <= 90){
$query = $db->execute("update `players` set `money`=?, `exp`=?, `nerve`=? where `id`=?", array($player->money + 10, $player->exp + 25, $player->nerve - 1, $player->id ));
echo "You Suceeded in your crime.";
echo "You gained $10 & 25 EXP.";
}
else if(randomnumber1 >= 91 && randomnumber2 >= 10){
$query = $db->execute("update `players` set `nerve`=? where `id`=?", array($player->nerve - 1, $player->id ));	
echo "You Failed your crime.";
}

else if(randomnumber2 >= 91 && randomnumber2 < 10) { 
$query = $db->execute("update `players` set `nerve`=? where `id`=?", array($player->nerve - 1, $player->id ));
echo "You failed your crime & you want sent to prison";
}
}
?>

Link to comment
https://forums.phpfreaks.com/topic/134965-php-if-statements/#findComment-703354
Share on other sites

First, turn on error reporting.

<?php
// add this at the top of your script
error_reporting(E_ALL);
ini_set('display_errors', TRUE);
?>

 

Second, pretty code is happy code:

<?php   
if ($player->nerve >=1 && $player->awake < $player->maxawake * 0.25)
{
$random1 = rand(1,100);
$random2 = rand(1,100);

if($random1 <= 10)
{
	$query = $db->execute("update `players` set `money`=?, `exp`=?, `nerve`=? where `id`=?", array($player->money + 10, $player->exp + 25, $player->nerve - 1, $player->id ));
	echo "You Suceeded in your crime.";
	echo "You gained $10 & 25 EXP.";
}
else if (randomnumber1 >= 11 && randomnumber2 >= 10)
{
	$query = $db->execute("update `players` set `nerve`=? where `id`=?", array($player->nerve - 1, $player->id ));   
	echo "You Failed your crime.";
}
else if(randomnumber2 >= 11 && randomnumber2 < 10) 
{ 
	$query = $db->execute("update `players` set `nerve`=? where `id`=?", array($player->nerve - 1, $player->id ));
	echo "You failed your crime & you want sent to prison";
}
}
else if ($player->nerve >=1 && $player->awake >= $player->maxawake * 0.25 && $player->awake < $player->maxawake * 0.50) 
{
   
$random1 = rand(1,100);
$random2 = rand(1,100);

if($random1 <= 30)
{
	$query = $db->execute("update `players` set `money`=?, `exp`=?, `nerve`=? where `id`=?", array($player->money + 10, $player->exp + 25, $player->nerve - 1, $player->id ));
	echo "You Suceeded in your crime.";
	echo "You gained $10 & 25 EXP.";
}
else if(randomnumber1 >= 31 && randomnumber2 >= 10)
{
	$query = $db->execute("update `players` set `nerve`=? where `id`=?", array($player->nerve - 1, $player->id ));   
	echo "You Failed your crime.";
}
else if(randomnumber2 >= 31 && randomnumber2 < 10) 
{ 
	$query = $db->execute("update `players` set `nerve`=? where `id`=?", array($player->nerve - 1, $player->id ));
	echo "You failed your crime & you want sent to prison";
}
}
else if ($player->nerve >=1 && $player->awake >= $player->maxawake * 0.50 && $player->awake < $player->maxawake * 0.75) 
{
   
$random1 = rand(1,100);
$random2 = rand(1,100);

if($random1 <= 60)
{
	$query = $db->execute("update `players` set `money`=?, `exp`=?, `nerve`=? where `id`=?", array($player->money + 10, $player->exp + 25, $player->nerve - 1, $player->id ));
	echo "You Suceeded in your crime.";
	echo "You gained $10 & 25 EXP.";
}
else if(randomnumber1 >= 61 && randomnumber2 >= 10)
{
	$query = $db->execute("update `players` set `nerve`=? where `id`=?", array($player->nerve - 1, $player->id ));   
	echo "You Failed your crime.";
}
else if(randomnumber2 >= 61 && randomnumber2 < 10)
{ 
	$query = $db->execute("update `players` set `nerve`=? where `id`=?", array($player->nerve - 1, $player->id ));
	echo "You failed your crime & you want sent to prison";
}
}
else if ($player->nerve >=1 && $player->awake >= $player->maxawake * 0.75 && $player->awake < $player->maxawake * 1) 
{
   
$random1 = rand(1,100);
$random2 = rand(1,100);

if($random1 <= 80){
	$query = $db->execute("update `players` set `money`=?, `exp`=?, `nerve`=? where `id`=?", array($player->money + 10, $player->exp + 25, $player->nerve - 1, $player->id ));
	echo "You Suceeded in your crime.";
	echo "You gained $10 & 25 EXP.";
}
else if(randomnumber1 >= 81 && randomnumber2 >= 10)
{
	$query = $db->execute("update `players` set `nerve`=? where `id`=?", array($player->nerve - 1, $player->id ));   
	echo "You Failed your crime.";
}
else if(randomnumber2 >= 81 && randomnumber2 < 10) 
{ 
	$query = $db->execute("update `players` set `nerve`=? where `id`=?", array($player->nerve - 1, $player->id ));
	echo "You failed your crime & you want sent to prison";
}
}
else if ($player->nerve >=1 && $player->awake >= $player->maxawake * 1)
{
   
$random1 = rand(1,100);
$random2 = rand(1,100);

if($random1 <= 90)
{
	$query = $db->execute("update `players` set `money`=?, `exp`=?, `nerve`=? where `id`=?", array($player->money + 10, $player->exp + 25, $player->nerve - 1, $player->id ));
	echo "You Suceeded in your crime.";
	echo "You gained $10 & 25 EXP.";
}
else if(randomnumber1 >= 91 && randomnumber2 >= 10)
{
	$query = $db->execute("update `players` set `nerve`=? where `id`=?", array($player->nerve - 1, $player->id ));   
	echo "You Failed your crime.";
}
else if(randomnumber2 >= 91 && randomnumber2 < 10) 
{ 
	$query = $db->execute("update `players` set `nerve`=? where `id`=?", array($player->nerve - 1, $player->id ));
	echo "You failed your crime & you want sent to prison";
}
}
?>

Link to comment
https://forums.phpfreaks.com/topic/134965-php-if-statements/#findComment-704400
Share on other sites

Okay i put that error reporting code in and got these errors:

 

Notice: Undefined variable: player in /home/www/xxxxxxxxxxxxxxxx/crimes/1.php on line 7

 

Notice: Undefined variable: player in /home/www/xxxxxxxxxxxxxxxx/crimes/1.php on line 27

 

Notice: Undefined variable: player in /home/www/xxxxxxxxxxxxxxxx/crimes/1.php on line 48

 

Notice: Undefined variable: player in /home/www/xxxxxxxxxxxxxxxx/crimes/1.php on line 69

 

Notice: Undefined variable: player in /home/www/xxxxxxxxxxxxxxxx/crimes/1.php on line 90

 

Line 7

if ($player->nerve >=1 && $player->awake < $player->maxawake * 0.25){

 

Line 27

else if ($player->nerve >=1 && $player->awake >= $player->maxawake * 0.25 && $player->awake < $player->maxawake * 0.50) {

 

Line 48

else if ($player->nerve >=1 && $player->awake >= $player->maxawake * 0.50 && $player->awake < $player->maxawake * 0.75) {

 

Line 69

else if ($player->nerve >=1 && $player->awake >= $player->maxawake * 0.75 && $player->awake < $player->maxawake * 1) {

 

Line 90

else if ($player->nerve >=1 && $player->awake >= $player->maxawake * 1){

 

Link to comment
https://forums.phpfreaks.com/topic/134965-php-if-statements/#findComment-704454
Share on other sites

right i have actually fixed the issue with the errors...

 

now im getting a different problem..

 

if the code determines a sucess then it works great.. but if its a fail or fail & jail... then the page just goes white.. i was thinking maybe it was the "{ and }"s i have in place a distrupting the other codes.

 

here is the part of code that are being used.

 


if ($player->nerve >=1 && $player->awake < $player->maxawake * 0.25){
$random1 = rand(1,100);
$random2 = rand(1,100);

if($random1 <= 10){
	header("location: ../crime.php");
$query = $db->execute("update `players` set `money`=?, `exp`=?, `nerve`=? where `id`=?", array($player->money + 10, $player->exp + 25, $player->nerve - 1, $player->id ));
}

else if (randomnumber1 >= 11 && randomnumber2 >= 10){
	header("location: ../crime.php");
$query = $db->execute("update `players` set `nerve`=? where `id`=?", array($player->nerve - 1, $player->id ));
}

else if(randomnumber2 >= 11 && randomnumber2 < 10) { 
	header("location: ../crime.php");
$query = $db->execute("update `players` set `nerve`=? where `id`=?", array($player->nerve - 1, $player->id ));
}
else {
	header("location: ../crime.php");
}	
}

 

EDIT: i just ran error test and got these errors:

 

Notice: Use of undefined constant randomnumber1 - assumed 'randomnumber1' on line 43

 

Notice: Use of undefined constant randomnumber2 - assumed 'randomnumber2' on line 49

 

 

Link to comment
https://forums.phpfreaks.com/topic/134965-php-if-statements/#findComment-705240
Share on other sites

      header("location: ../crime.php");
$query = $db->execute("update `players` set `nerve`=? where `id`=?", array($player->nerve - 1, $player->id ));

 

That query will never get executed. Put it above the header line if you want it to actually be executed.

Link to comment
https://forums.phpfreaks.com/topic/134965-php-if-statements/#findComment-705246
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.   Restore formatting

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

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.