Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Die Roller problems

Status
Not open for further replies.

krappleby025

Programmer
Sep 6, 2001
347
NL
HI guys,

im having a bit of a problem with a function to roll a die..

here is the code i have created

********************

function Roll_Dice($die)
{
$die_roll = rand(1,$die);
return $die_roll;
}


#### testing function

$num_rolls = "0";
$die_rolls = "0";

while ($num_rolls <> "6")
{
while ($die_rolls <> "3")
{
Roll_Dice("6");
$roll . $die_rolls = $die_roll;
$die_rolls = $die_rolls + 1;
}
$num_rolls = $num_rolls + 1;
echo "Roll " . $num_rolls . " = " . $roll1 . " + " . $roll2 . " + " . $roll3 . " = " . $roll1+$roll2+$roll3 . "<br>";
}
*************

if any of you know about RPG you will understand what i am trying to do.. Create 6 rolls containing 3 dice each..

however. for some reason the function is not returning a valid number.. when running the script.. all i get is

exceeded 30 seconds..

if i remove most of the info to just leave the main programming i get a 0 only..

appreciate your help
 
Your problem with invalid numbers likely stems from the fact that your Roll_Dice() function returns a value, but you don't do anything with it.

I also do not know what this line:

$roll . $die_rolls = $die_roll;

is supposed to do, but I would be suprised if PHP will not give you a parse error on the line. If you're going to store multiple values, I recommend the use of an array.

Try:

Code:
<?php
function roll_dice ($die_size = 6)
{
	return rand(1,$die_size);
}

$number_of_rolls = 3;
$die_size = 6;
$rolls = array();

for ($counter = 0; $counter < $number_of_rolls; $counter++)
{
	$rolls[] = roll_dice($die_size);
}

print $number_of_rolls . 'd'. $die_size . ":";

foreach ($rolls as $roll)
	print ' ' .$roll;
	
print ' = ' . array_sum($rolls) . '<br>';
?>


Want the best answers? Ask the best questions!

TANSTAAFL!!
 
Code:
  Roll_Dice("6");
        $roll . $die_rolls = $die_roll;

Now as as Sleipnir said, $roll . $die_rolls=$die_roll who knows what this is doing,
However, assigning the value of a variable that comes from a function outside of the function is not possible. because your variable $die_roll only exists inside yor function. you'd have to assign it like this:

Code:
$rolling=Roll_Dice(6);

So you get the value that the function returns.

Try that and see waht it does.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top