Can't seem to find my issue...

Place for questions and answers for all newcomers and new coders. This is a free for all forum, no question is too stupid and to noob.
Post Reply
User avatar
Xaos
Posts: 940
Joined: Wed Jan 11, 2012 4:01 am

Can't seem to find my issue...

Post by Xaos »

Code: Select all

<?php

$diceroll=rand(1,170);
$yardage=20;

if ($diceroll >=1){
  $commentary= "The Quarterback drops back......Pressure from the backside........ SACK ! Big hit by the defensive end.....wait ! There's a fumble.....recovered by the linebacker ! Touchdown Defense!";
  $playgain=-100;
  $yardage=($yardage+$playgain);
}
if ($diceroll >=10){
$commentary= "Yes.";
$yardage=($yardage+$playgain);
$playgain=5;
}
if ($diceroll >=20){
 $commentary= "No.";
 $yardage=($yardage+$playgain);
     $playgain=10;
}
if ($diceroll >=30){
 $commentary= "Maybe";
$yardage=($yardage+$playgain);
     $playgain=15;
}
if($diceroll >=40){
$commentary= "You're an ig+no+ra+moose!!";
$yardage=($yardage+$playgain);
     $playgain=20;
}
if ($diceroll >=50){
 $commentary= "lol did you seriously just ask that?";
$yardage=($yardage+$playgain);
     $playgain=25;
}
if ($diceroll >=60){
$commentary= "umad?";
$yardage=($yardage+$playgain);
        $playgain=30;
}
if ($diceroll >=70){
 $commentary="Oh heck naw!";
  $yardage=($yardage+$playgain);
               $playgain=35;
}
if ($diceroll >=80){
  $commentary= "LOL@UR SWAG";
 $yardage=($yardage+$playgain);
        $playgain=40;
}
if($diceroll >=90) {

  $commentary= "Ask your mother.";
 $yardage=($yardage+$playgain);
      $playgain=45;
}
if ($diceroll >=100){

  $commentary= "You'll know when you're older.";
$yardage= ($yardage+$playgain);
      $playgain=50;
}
if ($diceroll >=110){
  $commentary= "Most Definetly!";
 $yardage=($yardage+$playgain);
             $playgain=55;
  }
  if ($diceroll >=120){
    $commentary= "Oh Yeaaaaaaah!";
    $yardage=($yardage+$playgain);
             $playgain=60;
    }
    if ($diceroll >=130){
      $commentary= "This is factual.";
      $yardage=($yardage+$playgain);
              $playgain=65;
      }
      if ($diceroll >=140){
        $commentary= "Google it.";
        $yardage=($yardage+$playgain);
          $playgain=70;
        }
        if ($diceroll >=150){
          $commentary= "After pondering your inquest most thoroughly I have come to the conclusion that one most certainly could paricpate int the question of your concern";
          $yardage=($yardage+$playgain);

            $playgain=75;
          }
          if ($diceroll >=160){
            $commentary= "Fo Shizzle My Nizzle.";
            $yardage=($yardage+$playgain);
               $playgain=80;
            }
            if ($diceroll >=170){
              $commentary= "You betcha.";
              $yardage=($yardage+$playgain);
                      $playgain=85;
              }

               echo "$commentary<br>";
                echo "$yardage<br>";
                echo "$diceroll<br>";
                echo "$playgain<br>";


              ?>
When I do it, everything works correctly, but I get huge numbers for Yardage that doesn't make any sense to me, such as -40 for a diceroll of 88, or 95 for a diceroll of 111. Also, ignore the "commentary".....that's not the real commentary of which will be in the game, this is all imported code using the same system as a Magic 8 Ball app i'm currently using on my site.
User avatar
Callan S.
Posts: 2042
Joined: Sun Jan 24, 2010 5:43 am

Re: Can't seem to find my issue...

Post by Callan S. »

What's at -40?
Last edited by Callan S. on Wed Aug 01, 2012 2:43 am, edited 1 time in total.
User avatar
Xaos
Posts: 940
Joined: Wed Jan 11, 2012 4:01 am

Re: Can't seem to find my issue...

Post by Xaos »

Callan S. wrote:What's -40?
The yardage when I roll, examples:

(first number is yardage, second is dice-roll)

You'll know when you're older.
45
102

Google it.
275
142

LOL@UR SWAG
-40
83

Fo Shizzle My Nizzle.
420
166

The only one that works is the first one.
User avatar
Callan S.
Posts: 2042
Joined: Sun Jan 24, 2010 5:43 am

Re: Can't seem to find my issue...

Post by Callan S. »

See the $playgain=-100;?

Code: Select all

    if ($diceroll >=1){
      $commentary= "The Quarterback drops back......Pressure from the backside........ SACK ! Big hit by the defensive end.....wait ! There's a fumble.....recovered by the linebacker ! Touchdown Defense!";
      $playgain=-100;
      $yardage=($yardage+$playgain);
    }

Code: Select all

    if ($diceroll >=80){
      $commentary= "LOL@UR SWAG";
     $yardage=($yardage+$playgain);
            $playgain=40;
    }
Possibly '$yardage=($yardage+$playgain);' should be after the $playgain modification. Otherwise sometimes playgain is -100, it makes $yardage take that into account, then it makes $playgain=40;

Getting closer to the prob?
User avatar
Xaos
Posts: 940
Joined: Wed Jan 11, 2012 4:01 am

Re: Can't seem to find my issue...

Post by Xaos »

Callan S. wrote:Well, you do have $playgain set here to =-100. And any roll at all is going to make it have -100.

Code: Select all

    if ($diceroll >=1){
      $commentary= "The Quarterback drops back......Pressure from the backside........ SACK ! Big hit by the defensive end.....wait ! There's a fumble.....recovered by the linebacker ! Touchdown Defense!";
      $playgain=-100;
      $yardage=($yardage+$playgain);
    }
I have it echoing the playgain for each one, and everytime, it says the right number I have in the code, but the yardage doesn't reflect that. IE:


Yardage 475
Diceroll 148
Playgain 70

I can even set the first number to 0, but it still creates absurd numbers. Another example

Yardage 20
Diceroll 10
Playgain 5

As we see here, it isn't added AT ALL. So confused.


Alas, it doesn't change a thing.
User avatar
Xaos
Posts: 940
Joined: Wed Jan 11, 2012 4:01 am

Re: Can't seem to find my issue...

Post by Xaos »

Okay, I have found the source of the problem. It is adding up all of the "yardgains" then adding it to the yardage. Any idea how I can fix this? :(
User avatar
Callan S.
Posts: 2042
Joined: Sun Jan 24, 2010 5:43 am

Re: Can't seem to find my issue...

Post by Callan S. »

I'll just check the effect you want

Code: Select all

if ($diceroll >=20){
 $commentary= "No.";
 $yardage=($yardage+$playgain);
     $playgain=10;
}
if ($diceroll >=30){
 $commentary= "Maybe";
$yardage=($yardage+$playgain);
     $playgain=15;
You know that if $diceroll=33, for example, BOTH the first and second if statement will occur?

If you only want one if statement to occur, you'd need something like

Code: Select all

if ($diceroll >=20 && $diceroll<30){
 $commentary= "No.";
 $yardage=($yardage+$playgain);
     $playgain=10;
}
if ($diceroll >=30 && $diceroll<40){
 $commentary= "Maybe";
$yardage=($yardage+$playgain);
     $playgain=15;
As well as doing that to all your other if statements.
User avatar
Xaos
Posts: 940
Joined: Wed Jan 11, 2012 4:01 am

Re: Can't seem to find my issue...

Post by Xaos »

Gah ! Of Course ! Now I feel dumb, lol. I just imported the dice rolls and such from the magic 8 ball like I had said, and because that was dealing with just text, I didn't have those kind of issues. Thanks for your help !
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: Can't seem to find my issue...

Post by Jackolantern »

You could also change each IF to an IF/ELSE IF so that only 1 can be completed for each roll. You also need to put the most exceptional result at the top. In this case, that means the highest first, then the next highest, and on down. Like Callen said, when you have >=1 at the top, that will always get called for any roll when 1 is the lowest that can be rolled. You need to have the 170 check first, then 160, 150, etc. And just to be safe, make them all IF/ELSE IF structure :)
The indelible lord of tl;dr
User avatar
Xaos
Posts: 940
Joined: Wed Jan 11, 2012 4:01 am

Re: Can't seem to find my issue...

Post by Xaos »

Jackolantern wrote:You could also change each IF to an IF/ELSE IF so that only 1 can be completed for each roll. You also need to put the most exceptional result at the top. In this case, that means the highest first, then the next highest, and on down. Like Callen said, when you have >=1 at the top, that will always get called for any roll when 1 is the lowest that can be rolled. You need to have the 170 check first, then 160, 150, etc. And just to be safe, make them all IF/ELSE IF structure :)
Thanks alot mate, I did'nt even think about that !
Post Reply

Return to “Beginner Help and Support”