Jump to content

random selector help


katarra

Recommended Posts

I'm trying to get a random number (rand(0,99)) and then have that number decide the outcome/effect. However, I don't seem to have it correct.

 

Any help would be appreciated! Thanks!

 

else
{
	$mine = <75 && >=65 = $effect[0];
	$mine = <12 = $effect[1];
	$mine = <80 && >=75 = $effect[2];
	$mine = <30 && >=12 = $effect[3];
	$mine = <73 && >=70 = $effect[4];
	$mine = <100 && >=85 = $effect[5];
	$mine = <45 && >=30 = $effect[6];
	$mine = <85 && >=80 = $effect[7];
	$mine = <65 && >=45 = $effect[8];

        $effect[0] = "struck a <strong>small vein of gold.</strong>";
        $effect[1] = "struck a <strong>vein of silver.</strong>";
        $effect[2] = "struck a <strong>large vein of gold.</strong>";
        $effect[3] = "struck a <strong>vein of sulfur.</strong>";
        $effect[4] = "struck a <strong>vein of platinum.</strong>";
        $effect[5] = "struck a <strong>vein of bronze.</strong>";
        $effect[6] = "struck a <strong>vein of copper.</strong>";
        $effect[7] = "caused a <strong>LANDSLIDE!</strong>";
        $effect[8] = "struck a <strong>vein of tin.</strong>";

Link to comment
https://forums.phpfreaks.com/topic/196225-random-selector-help/
Share on other sites

Here's probably a better snapshot of the code. I'll leave off the "effects" attributes since it haven't even made it down there yet.

 

include("lib.php");
define("PAGENAME", "Gold mine");
$player = check_user($secret_key, $db);

//$metal = ($player->mine_level *3);
//$metal2 = ($player->mine_level * 5);
//$metal3 = ($player->mine_level * 10);
$expgain = 1;

echo "<center><a href=\"mining1.php?act=mine\"><img src=\"images/t_mine.jpg\"></a>";
$mining = $db->execute("SELECT * FROM `mining` WHERE `id`=?");

$mine = rand(0,99);

if ($_GET['act'] == "mine")
{
if ($player->energy < 1)
{

	echo "You do not have enough energy to mine.<p>";
	exit;
}
if ($mining->mine_count > 1000)
{

	echo "You can not mine anymore today.<p>";
	exit;
}

//$paxe = $db->execute("SELECT `paxe` from `mining` where `id`=?");

if ($mining->paxe < 1) 
       {
        echo "<p><strong>You do not have a pickaxe!</strong></p>";
        exit;
    
    }  

else
{
	$mine = <75 && >=65 = $effect[0];
	$mine = <12 = $effect[1];
	$mine = <80 && >=75 = $effect[2];
	$mine = <30 && >=12 = $effect[3];
	$mine = <73 && >=70 = $effect[4];
	$mine = <99 && >=85 = $effect[5];
	$mine = <45 && >=30 = $effect[6];
	$mine = <85 && >=80 = $effect[7];
	$mine = <65 && >=45 = $effect[8];

        $effect[0] = "struck a <strong>small vein of gold.</strong>";
        $effect[1] = "struck a <strong>vein of silver.</strong>";
        $effect[2] = "struck a <strong>large vein of gold.</strong>";
        $effect[3] = "struck a <strong>vein of sulfur.</strong>";
        $effect[4] = "struck a <strong>vein of platinum.</strong>";
        $effect[5] = "struck a <strong>vein of bronze.</strong>";
        $effect[6] = "struck a <strong>vein of copper.</strong>";
        $effect[7] = "caused a <strong>LANDSLIDE!</strong>";
        $effect[8] = "struck a <strong>vein of tin.</strong>";

I thought you might have stumbled onto a syntax that I didn't know, but using this does not work:

 

$var (conditional) = assignment

 

You need to expand the language to this:

 

(conditonal){assignment}

 

This would make your code into something like this (I like to use switch case syntax for this).  Also remember to define your array first, not after making the assignment, it doesn't work like a pointer.


        $effect[0] = "struck a <strong>small vein of gold.</strong>";
        $effect[1] = "struck a <strong>vein of silver.</strong>";
        $effect[2] = "struck a <strong>large vein of gold.</strong>";
        $effect[3] = "struck a <strong>vein of sulfur.</strong>";
        $effect[4] = "struck a <strong>vein of platinum.</strong>";
        $effect[5] = "struck a <strong>vein of bronze.</strong>";
        $effect[6] = "struck a <strong>vein of copper.</strong>";
        $effect[7] = "caused a <strong>LANDSLIDE!</strong>";
        $effect[8] = "struck a <strong>vein of tin.</strong>";

$x=5;                //default value (the value at the top of the range)
switch ($mine)
{

case ($mine < 85):
$x=7;
break;

case ($mine < 80):
$x=2;
break;

case ($mine <75):
$x=0;
break

case ($mine < 65):
$x=8;
break;

case ($mine < 45):
$x=6;
break;

case ($mine < 30):
$x=3;
break;

case ($mine <= 12):
$x=1;
break;
}
$mine=$effect[$x];

 

Here's the problem:

 

You have ranges that overlap!  The option 4 will not exist in this context, so you need to make a decision what the appropriate ranges should be for that variable.

 

You might baulk at the idea of assigning a variable multiple times, but doing double evaluations are not as efficient as this method.

 

So if I were you I'd modify the code to allow the $effect[4] to be included:

 


        $effect[0] = "struck a <strong>small vein of gold.</strong>";
        $effect[1] = "struck a <strong>vein of silver.</strong>";
        $effect[2] = "struck a <strong>large vein of gold.</strong>";
        $effect[3] = "struck a <strong>vein of sulfur.</strong>";
        $effect[4] = "struck a <strong>vein of platinum.</strong>";
        $effect[5] = "struck a <strong>vein of bronze.</strong>";
        $effect[6] = "struck a <strong>vein of copper.</strong>";
        $effect[7] = "caused a <strong>LANDSLIDE!</strong>";
        $effect[8] = "struck a <strong>vein of tin.</strong>";

$x=5;                //default value (the value at the top of the range)
switch ($mine)
{

case ($mine < 85):
$x=7;
break;

case ($mine < 80):
$x=2;
break;

case ($mine <75):
$x=4;
break;

case ($mine <73):
$x=0;
break

case ($mine < 65):
$x=8;
break;

case ($mine < 45):
$x=6;
break;

case ($mine < 30):
$x=3;
break;

case ($mine <= 12):
$x=1;
break;
}
$mine=$effect[$x];

 

But remember this second example changes the logic of the randomness.

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.