Click to See Complete Forum and Search --> : Arrays and the Rand function


kylesite
10-24-2003, 10:44 PM
What do you think about this?<?php

$yourrace = array ("Half-Orc" , "Halfling" , "Gnome" , "Dwarf" , "Half-Elf" , "Elf" , "Human");
$yourrace = rand($yourrace);

$yourclass = array ("Assassin" , "Barbarian" , "Bard" , "Cleric" , "Druid" , "Fighter" , "Illusionist" , "Monk" , "Paladin" , "Ranger" , "Rogue" , "Sorcerer" , "Theif",
"Wizard");
$yourclass = rand($yourclass);

$yourweapon = array ("knife" , "club" , "dagger" , "bow" , "sword" , "gun" , "catapult");
$yourweapon = rand($yourweapon);

$yourstrength = rand(5,12);
if ($yourstrength == "5" || $yourstrength == "6" || $yourstrength == "7"){
$strengthword = "Weak";
} elseif ($yourstrength == "8") || $yourstrength == "9" || $yourstrength == "10"){
$strengthword = "Tough";
} elseif ($yourstrength == "11"){
$strengthword = "Strong!";
} elseif ($yourstrength == "12"){
$strengthword = "Super Strong!!!";
}

$yourdefense = rand(5,12);
if ($yourdefense == "5" || $yourdefense == "6" || $yourdefense == "7"){
$defenseword = "Weak";
} elseif ($yourdefense == "8") || $yourdefense == "9" || $yourdefense == "10"){
$defenseword = "Tough";
} elseif ($yourdefense == "11"){
$defenseword = "Strong!";
} elseif ($yourdefense == "12"){
$defenseword = "Super Strong!!!";
}

$bodymess = " <center>\n
<form action=\"".$_SERVER['PHP_SELF']."\" method=\"post\">\n
Your name: <input name=\"p1\">\n
<br>\n
<input type=\"submit\" value=\"Play!\">\n
</form>\n
</center>
";
echo $bodymess;

if (isset($_POST['p1'])){
$p1 = $_POST['p1'];
$bodymess = "
<hr width=\"100%\"><table width=\"30%\">
<b>".$p1."</b><br>Race: ".$yourrace."<br>Class: ".$yourclass."<br>Weapon: ".yourweapon."<br>Strength: <b>".$strengthword."</b>(<b>".$yourstrength."</b>/12)<br>Defense: <b>".$defenseword."</b>(<b>".$yourdefense."</b>/12)
</table></hr>
";
} else {
echo "<br><center><font color=\"red\">You forgot to enter your name</font></center>";
}

?>It would show something like this:
Kylesite
Race: Human
Class: Assassin
Weapon: gun
Strength: Strong!(11/12)
Defense: Weak(6/12)

LordShryku
10-24-2003, 11:24 PM
Parse error: parse error, unexpected T_BOOLEAN_OR in C:\www\test\a\tst.php on line 16

Moonglobe
10-24-2003, 11:53 PM
also change your rand()s in the beginning to array_rand()s.

drawmack
10-25-2003, 01:05 AM
1) You're breaking the d20l. You cannot use a random number generator in a d20l product as that is considered task resolution. Ha - bet you didn't expect someone to know about the d20l here. WARNING: Hasbro is cracking down and they will get you if you do this.

Now as far as the code itself. Instead of all those || in your if statements just us a < it'll be a lot faster.

Merve
10-25-2003, 03:13 PM
You're if statements can be replaced with a switch statement. Cool code though.

dta
10-27-2003, 10:10 PM
might want to throw the $yourstrength/defense thing into a function since its almost exactly repeated.. along with the < > or switch..

Weedpacket
10-31-2003, 05:21 AM
Originally posted by Merve
You're if statements can be replaced with a switch statement. Cool code though. Or an array lookup:
$yourstrengths = array(5=>'Weak', 6=>'Weak', 7=>'Weak',
8=>'Tough', 9=>'Tough', 10=>'Tough',
11=>'Super Strong!!!', 12=>'Super Strong!!!');
$yourstrength=$yourstrengths[rand(5,12)];
Which could of course be simplified by subtracting 5; leading towards Moonglobe's observation.

HalfaBee
10-31-2003, 10:30 PM
Weedpacket it should bee

$yourstrengths = array(5=>'Weak', 6=>'Weak', 7=>'Weak',
8=>'Tough', 9=>'Tough', 10=>'Tough',
11=>'Super Strong!!!', 12=>'Super Strong!!!');
$strengthword=$yourstrengths[$yourstrength=rand(5,12)];

// or

$stregnthword = $yourstrengths[ $yourstrength=array_rand( $yourstrengths)];



HalfaBee