To register for an Internet.com membership to receive newsletters and white papers, use the Register button ABOVE.
To participate in the message forums BELOW, click here
PHPBuilder.com  
 

 

Go Back   PHPBuilder.com > PHP Help > Code Critique

Code Critique Having someone critique your code is always a great way to hone the skills. Stop in and post your code to see what your peers may have done differently.

Reply
 
Thread Tools Rate Thread Display Modes
Old 10-24-2003, 10:44 PM   #1
kylesite
Junior Member
 
kylesite's Avatar
 
Join Date: Oct 2003
Location: here
Posts: 17
Arrays and the Rand function

What do you think about this?
PHP Code:
<?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)

Last edited by kylesite; 10-24-2003 at 10:51 PM.
kylesite is offline   Reply With Quote
Old 10-24-2003, 11:24 PM   #2
LordShryku
kung foo code monkey
 
LordShryku's Avatar
 
Join Date: Aug 2002
Location: Occupational Hypnotherapy
Posts: 7,473
Parse error: parse error, unexpected T_BOOLEAN_OR in C:\www\test\a\tst.php on line 16
LordShryku is offline   Reply With Quote
Old 10-24-2003, 11:53 PM   #3
Moonglobe
Better fan than rebelo!
 
Moonglobe's Avatar
 
Join Date: Apr 2003
Location: brain://localhost:left-side
Posts: 2,381
also change your rand()s in the beginning to array_rand()s.
__________________
there's no place i can be, since i found serenity.
Moonglobe is offline   Reply With Quote
Old 10-25-2003, 01:05 AM   #4
drawmack
Computers can do that?
 
drawmack's Avatar
 
Join Date: Apr 2003
Location: Pocono Mtns PA
Posts: 3,268
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.
drawmack is offline   Reply With Quote
Old 10-25-2003, 03:13 PM   #5
Merve
black sheep with red wool
 
Merve's Avatar
 
Join Date: Jul 2003
Location: North of the 49th parallel
Posts: 2,579
You're if statements can be replaced with a switch statement. Cool code though.
__________________
"A proof is a proof. What kind of a proof? It's a proof. A proof is a proof. And when you have a good proof, it's because it's proven." -- Jean Chrétien

The Three C's
Merve is offline   Reply With Quote
Old 10-27-2003, 10:10 PM   #6
dta
Member
 
dta's Avatar
 
Join Date: Oct 2003
Location: n.jersey
Posts: 84
might want to throw the $yourstrength/defense thing into a function since its almost exactly repeated.. along with the < > or switch..
__________________
$drink->heineken();
dta is offline   Reply With Quote
Old 10-31-2003, 05:21 AM   #7
Weedpacket
Custom User Title™
 
Weedpacket's Avatar
 
Join Date: Aug 2002
Location: Rapid Offensive Unit "Foreign Object Damage"
Posts: 19,122
Quote:
Originally posted by Merve
You're if statements can be replaced with a switch statement. Cool code though.
Or an array lookup:
PHP Code:
$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.
__________________
On two occasions I have been asked [by Members of Parliament], "Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?" I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question.
Weedpacket is offline   Reply With Quote
Old 10-31-2003, 10:30 PM   #8
HalfaBee
Not very lazy.
 
Join Date: Jun 2003
Location: Sydney, Australia
Posts: 1,851
Weedpacket it should bee

PHP Code:
$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
__________________
The lazy man always finds the best way!
Q: Who invented the auto-pilot?
A: The lazy pilot!
HalfaBee is offline   Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump


All times are GMT -4. The time now is 02:43 PM.






Acceptable Use Policy

internet.comMediabistrojusttechjobs.comGraphics.com

WebMediaBrands Corporate Info


Advertise | Newsletters | Feedback | Submit News

Legal Notices | Licensing | Permissions | Privacy Policy


Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.