Ideas on how to do this without so many ifs

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

cwheel3915
Forum Commoner
Posts: 28
Joined: Wed Apr 28, 2010 8:02 pm

Ideas on how to do this without so many ifs

Post by cwheel3915 »

Code: Select all



$shooterquerystring = "SELECT * FROM `character` WHERE `charname` = '$charname' ";
$victemquerystring2 = "SELECT * FROM `character` WHERE `charname` = '$victem' ";
$victemquery2 = mysql_query($victemquerystring2, $mysql);
$shooterquery = mysql_query($shooterquerystring, $mysql);
$shooterget = mysql_fetch_assoc($shooterquery); 
$victemget = mysql_fetch_assoc($victemquery2);

#set stats for shooter, and victem
$cattack = $shooterget['attack'];
$cdefense = $shooterget['defense'];
$cstealth = $shooterget['stealth'];
$chealth = $shooterget['health'];
$conhand = $shooterget['onhand'];
$calive = $shooterget['alive'];

$vattack = $victemget['attack'];
$vdefense = $victemget['defense'];
$vstealth = $victemget['stealth'];
$vhealth = $victemget['health'];
$vonhand = $victemget['onhand'];
$valive = $victemget['alive'];

# victem defense minus character attack ='s hitchance.

$hitchance = $vdefense - $cattack;

if( $hitchance < 26 ){

#depending on modulus decide how much damage was done
 if($hitchance < 27 && $hitchance > 22){
 $victemdamage = rand(1,15);
 }
  if($hitchance < 23 && $hitchance > 18){
 $victemdamage = rand(5,25);
 }
  if($hitchance < 19 && $hitchance > 15){
 $victemdamage = rand(15,35);
 }
  if($hitchance < 15 && $hitchance > 10){
 $victemdamage = rand(30,50);
 }
 
 if($hitchance < 10 &&  $hitchance > 6 ){
 $victemdamage = rand(45,60);
 }
  if($hitchance < 6 && $hitchance >  1){
 $victemdamage = rand(50,75);
 }
  if($hitchance == 1 or $hitchance < 1){
 $victemdamage = rand(75,120);
 }

# subtract damage from victem health depending on amount of damage taken from victems health pick wound type
$victemwound = $vhealth - $victemdamage;

if( $victemwound > 55 && $victemwound < $vhealth ){

$wound = "Superficial";
}

if( $victemwound > 45 && $victemwound < 55 ){

$wound = "Significant";
}

if( $victemwound > 35 && $victemwound < 45 ){

$wound = "Major";
}

if( $victemwound > 25 && $victemwound < 35 ){

$wound = "Severe";
}

if( $victemwound > 1 && $victemwound < 25 ){

$wound = "Near Fatal";
}

if( $victemwound < 1 ){

$wound = "Fatal";
} 

$message = "Your shots find their target " . $victem . " Has suffered a " . $wound . " wound.";




####################################################################################
########################  Find out if there will be whackback. #######################################
####################################################################################

$hitchance2 = $cattack - $vdefense;

if( $hitchance2 < 26 ){

#depending on modulus decide how much damage was done
 if($hitchance2 < 27 && $hitchance2 > 22){
 $shooterdamage = rand(1,15);
 }
  if($hitchance2 < 23 && $hitchance2 > 18){
 $shooterdamage = rand(5,25);
 }
  if($hitchance2 < 19 && $hitchance2 > 15){
 $shooterdamage = rand(15,35);
 }
  if($hitchance2 < 15 && $hitchance2 > 10){
 $shooterdamage = rand(30,50);
 }
 
 if($hitchance2 < 10 &&  $hitchance2 > 6 ){
 $shooterdamage = rand(45,60);
 }
  if($hitchance2 < 6 && $hitchance2 >  1){
 $shooterdamage = rand(50,75);
 }
  if($hitchance2 == 1 or $hitchance2 < 1){
 $shooterdamage = rand(75,120);
 }

# subtract damage 
$shooterwound = $chealth - $shooterdamage;

if( $shooterwound > 55 && $shooterwound < $chealth ){

$wound2 = "Superficial";
}

if( $shooterwound > 45 && $shooterwound < 55 ){

$wound2 = "Significant";
}

if( $shooterwound > 35 && $shooterwound < 45 ){

$wound2 = "Major";
}

if( $shooterwound > 25 && $shooterwound < 35 ){

$wound2 = "Severe";
}

if( $shooterwound > 1 && $shooterwound < 25 ){

$wound2 = "Near Fatal";
}

if( $shooterwound < 1 ){

$wound2 = "Fatal";
} 

$message = "Your shots find their target " . $victem . " Has suffered a " . $wound . " wound, but " . $victem . " " . "does not 
miss either, you have suffered a " . $wound2 . " " . "wound.";



####################################################################################
############################  IF CHECK POCKETS ISSET THEN ADD VICTEMS ONHAND TO SHOOTERS 
#############################################ON HAND#################################
####################################################################################

if(isset($_POST['loot'] ) ){
$conhand = $conhand + $vonhand;

$message = $message . "<br/>" . "Your victems pockets yielded " . "$" . number_format($vonhand);
$vonhand = 0;
}






##############################################################################################
#############################Whack back###########################################################
##############################################################################################
# find out is no hit from attacker, and only whackback
# victem defense minus character attack ='s hitchance.

$hitchance = $cattack - $vdefense;

if( $hitchance < 26 ){

#depending on modulus decide how much damage was done
 if($hitchance < 27 && $hitchance > 22){
 $victemdamage = rand(1,15);
 }
  if($hitchance < 23 && $hitchance > 18){
 $victemdamage = rand(5,25);
 }
  if($hitchance < 19 && $hitchance > 15){
 $victemdamage = rand(15,35);
 }
  if($hitchance < 15 && $hitchance > 10){
 $victemdamage = rand(30,50);
 }
 
 if($hitchance < 10 &&  $hitchance > 6 ){
 $victemdamage = rand(45,60);
 }
  if($hitchance < 6 && $hitchance >  1){
 $victemdamage = rand(50,75);
 }
  if($hitchance == 1 or $hitchance < 1){
 $victemdamage = rand(75,120);
 }

# subtract damage from victem health depending on amount of damage taken from victems health pick wound type
$victemwound = $vhealth - $victemdamage;

if( $victemwound > 55 && $victemwound < $vhealth ){

$wound = "Superficial";
}

if( $victemwound > 45 && $victemwound < 55 ){

$wound = "Significant";
}

if( $victemwound > 35 && $victemwound < 45 ){

$wound = "Major";
}

if( $victemwound > 25 && $victemwound < 35 ){

$wound = "Severe";
}

if( $victemwound > 1 && $victemwound < 25 ){

$wound = "Near Fatal";
}

if( $victemwound < 1 ){

$wound = "Fatal";
} 

$message = "Your shots fail to find their target, but " . "$victem" . " " . "does not miss, you have suffered a " . $wound . " " . "wound.";


if($wound == "Fatal"){
$valive = 0;
}

if($wound2 == "Fatal"){
$calive = 0;
}

$chealth = $chealth - $victemwound;



} 

So basicly this script compares the attack variable of one player against the others defense. This is only part of the script but the rest is a repeat of this for different scenarios. As you can see im basing the outcome on series of random numbers resulting in a certain kind of "Wound" if the outcome is a hit.

I just want to see if anyone else has any ideas on how to go about this without so many if statements, The completed code is virtually unmanagable due to so many nested ifs.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Ideas on how to do this without so many ifs

Post by requinix »

Heh, "whackback".

Anyways, wrap that stuff into a function. Or more than one, if you don't know how to use only one.
cwheel3915
Forum Commoner
Posts: 28
Joined: Wed Apr 28, 2010 8:02 pm

Re: Ideas on how to do this without so many ifs

Post by cwheel3915 »

I have the enitire code in a function.
I was just wandering if based on the way im getting the random numbers now if anyone had an idea for a way tp do it, and get the same variables "Superficial" - "Fatal" in a tidier way.

Or do you think just using the if statements as above is fine?
cwheel3915
Forum Commoner
Posts: 28
Joined: Wed Apr 28, 2010 8:02 pm

Re: Ideas on how to do this without so many ifs

Post by cwheel3915 »

OKay I updated the script, I put it all into different functions, but now the only problem is every "wound" returned from the getvictemwound() function returns fatal. And no matter what the condition of the variables $hitchance, and $whaclback chance it always returns the message " You shots found the target, but so, and so didnt miss either.

Anyway its not working like it is supposed to, and I cant figure out where I have went wrong. Any help is greatly appreciated.


Code: Select all



##############################################################################
###############################################################################
#########################                                        ##################################
#########################       Whack functions         ##################################
#########################                                        ##################################
###############################################################################
  
  
  
  
#################################
#######  HIT CHANCE FUNCTION #########

function gethitchance($hitchance){

#depending on modulus decide how much damage was done
 if($hitchance < 27 && $hitchance > 22){
 $victemdamage = rand(1,15);
 }
  if($hitchance < 23 && $hitchance > 18){
 $victemdamage = rand(5,25);
 }
  if($hitchance < 19 && $hitchance > 15){
 $victemdamage = rand(15,35);
 }
  if($hitchance < 15 && $hitchance > 10){
 $victemdamage = rand(30,50);
 }
 
 if($hitchance < 10 &&  $hitchance > 6 ){
 $victemdamage = rand(45,60);
 }
  if($hitchance < 6 && $hitchance >  0){
 $victemdamage = rand(50,75);
 }
  if($hitchance < 1 or $hitchance < 0){
 $victemdamage = rand(75,120);
 }

$victemwound = $victemdamage;

return $victemwound;

}

################################3
#####33 GET VICTIM WOUND FUNCTION #####
################################

function getvictemwound($victemwound){

if( $victemwound > 65 ){

$wound = "Superficial";
}

if( $victemwound > 45 && $victemwound < 55 ){

$wound = "Significant";
}

if( $victemwound > 35 && $victemwound < 45 ){

$wound = "Major";
}

if( $victemwound > 25 && $victemwound < 35 ){

$wound = "Severe";
}

if( $victemwound > 1 && $victemwound < 25 ){

$wound = "Near Fatal";
}

if( $victemwound > 0 or $victemwound < 0 ){

$wound = "Fatal";
} 

return $wound;

}

####################################################################################
###############                                     MAIN WHACK FUNCTION        ###################################
###################################################################################
###################################################################################


function whack(){

#get post variables
$victem = isset($_POST['victem']) ? $_POST['victem'] : '';
$loot = isset($_POST['charname']) ? $_POST['charname'] : '';
$charname = $_SESSION['charname'];

#make db connection
$mysql = mysql_connect('localhost', 'root');
mysql_selectdb('mobgame');

#build query to check if victem exist,
$victemquerystring = "SELECT * FROM `character` WHERE `charname` = '$victem' ";
$victemquery = mysql_query($victemquerystring, $mysql);

if(mysql_num_rows($victemquery) < 1){
$message = "Wait to go! Your imaginary friend is now dead.";
} else {

#if victem exist do query to users stats, and victims stats.
$shooterquerystring = "SELECT * FROM `character` WHERE `charname` = '$charname' ";
$victemquerystring2 = "SELECT * FROM `character` WHERE `charname` = '$victem' ";
$victemquery2 = mysql_query($victemquerystring2, $mysql);
$shooterquery = mysql_query($shooterquerystring, $mysql);
$shooterget = mysql_fetch_assoc($shooterquery); 
$victemget = mysql_fetch_assoc($victemquery2);

#set stats for shooter, and victem
$cattack = $shooterget['attack'];
$cdefense = $shooterget['defense'];
$cstealth = $shooterget['stealth'];
$chealth = $shooterget['health'];
$conhand = $shooterget['onhand'];
$calive = $shooterget['alive'];

$vattack = $victemget['attack'];
$vdefense = $victemget['defense'];
$vstealth = $victemget['stealth'];
$vhealth = $victemget['health'];
$vonhand = $victemget['onhand'];
$valive = $victemget['alive'];


################################
######  Select scenario ################
######    ######      ###############
###############################

  $hitchance = $vdefense - $cattack;
 
  $whackbackchance = $cattack - $vdefense;
  

########################################3
########################################
########## Player hits with no whackback ############
#######################################
if($hitchance < 26 && $whackbackchance > 25){

#Get hitchance
$victemwound = gethitchance($hitchance);
$victemwound = $vhealth - $victemwound;


# get wound name
$wound = getvictemwound($victemwound);
 
 #message if not checkpockets
 
$message = "Your shots find the target " . "$victem" . " " . "has suffered a " . $wound . " wound";

  if($wound = "Fatal"){

  $valive = 0; 
  }
  
  if($wound <> "Fatal"){
  # subtract wound number from victem
$vhealth = $vhealth - $victemwound;
}

  
if(isset($_POST['loot'])) {
$message = $message . "<br/>" . "Your victems pockets yielded " . "$" . number_format($vonhand) . ".";
$conhand = $conhand + $vonhand;
}

  $upshooterquerystring = "UPDATE `character` SET `alive` = '$calive',`health` = '$chealth' , `onhand` = '$conhand' WHERE `charname` = '$charname' ";
$upvictemquerystring = "UPDATE `character` SET `alive` = '$valive',`health` = '$vhealth' ,`onhand` = '$vonhand' WHERE `charname` = '$victem' ";

mysql_query($upshooterquerystring, $mysql);
mysql_query($upvictemquerystring, $mysql);

} 


################################################################
################################################################
################### Player hit but also gets whackbacked. ########################
################################################################
if($hitchance < 26 && $whackbackchance < 26){

#get hit chance, and whackbackchance.
$shooterwound = gethitchance($whackbackchance);
$victemwound = gethitchance($hitchance);
$shooterwound = $chealth - $shooterwound;
$victemwound = $vhealth - $victemwound;

#get wound names
$shooterwoundname = getvictemwound($shooterwound);
$victemwoundname = getvictemwound($victemwound);

# if fatal change to variable to dead
if($shooterwoundname == "Fatal"){
$calive = 0;

} 

if($victemwoundname == "Fatal"){
$valive = 0;

}

if($victemwoundname <> "Fatal"){
#subtract wounds from shooter, and victem
$chealth = $chealth - $shooterwound;
$vhealth = $vhealth - $victemwound;
}

$message = "Your shots find the target " . $victem . " " . "has suffered a " . $victemwoundname . " wound, but " . $victem . 
" does not miss either. You have suffered a " . $shooterwoundname . " wound.";

if(isset($_POST['loot']) && $shooterwoundname <> "Fatal"){
$message = $message . "<br/>" . "Your victems pockets yielded " . "$" . number_format($vonhand) . ".";
$conhand = $conhand + $vonhand;

}

  $upshooterquerystring = "UPDATE `character` SET `alive` = '$calive',`health` = '$chealth' , `onhand` = '$conhand' WHERE `charname` = '$charname' ";
$upvictemquerystring = "UPDATE `character` SET `alive` = '$valive',`health` = '$vhealth' ,`onhand` = '$vonhand' WHERE `charname` = '$victem' ";

mysql_query($upshooterquerystring, $mysql);
mysql_query($upvictemquerystring, $mysql);

}

####################################################
############3  PLAYER MISSES, BUT IS WHACKBACKED ##############
####################################################
if($hitchance > 25 && $whackbackchance < 26) {

#Get hitchance
$shooterwound = gethitchance($hitchance);
$shooterwound = $chealth - $shooterwound;

# get wound name
$wound = getvictemwound($shooterwound);
 
 #message if not checkpockets
 $message = "Your shots fail to find the target, but " . $victem . " does not miss. You have suffered a " . $wound . " wound.";
 
  if($wound == "Fatal"){

  $calive = 0; 
  }  else {
  # subtract wound number from victem
$chealth = $chealth - $shooterwound;
}
$upshooterquerystring = "UPDATE `character` SET `alive` = '$calive',`health` = '$chealth' , `onhand` = '$conhand' WHERE `charname` = '$charname' ";
$upvictemquerystring = "UPDATE `character` SET `alive` = '$valive',`health` = '$vhealth' ,`onhand` = '$vonhand' WHERE `charname` = '$victem' ";

mysql_query($upshooterquerystring, $mysql);
mysql_query($upvictemquerystring, $mysql);

  }
  
  ###############################################################
  ###############################################################
  #################   PLAYER MISSES, BUT DOES NOT GET WHACKBACKED ############
  ###############################################################
  if($hitchance > 25 && $whackbackchance > 25 ) {
  $message = "Your shots fail to find the target";
  
 } 
 }
 echo $message;
  }
  
?>



User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Ideas on how to do this without so many ifs

Post by requinix »

cwheel3915 wrote:but now the only problem is every "wound" returned from the getvictemwound() function returns fatal

Code: Select all

if( $victemwound  > 0 or $victemwound < 0 ){

For the whacking stuff I don't really have the time or patience to look through all that code. Already I can see you've got a bunch of logic errors. For example, in gethitchance(), what if $hitchance=15? getvictemwound() doesn't know what to do if $victemwound=45.
(And by the way, it's "victim".)
cwheel3915
Forum Commoner
Posts: 28
Joined: Wed Apr 28, 2010 8:02 pm

Re: Ideas on how to do this without so many ifs

Post by cwheel3915 »

I rewrote everything into even more functions, and fixed a few logic errors, still no dice.
Im just going to rethink the whole system I suppose. I was more or less just looking for maybe an idea to do this kind of thing a different way.

FYI its "victem" for a reason....
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Ideas on how to do this without so many ifs

Post by social_experiment »

cwheel3915 : Have you thought about using a switch statement (or multiple switch statements)?

Code: Select all

<?php switch($hitchance) {
  case($hitchance < 27 && $hitchance > 22):
  $victemdamage = rand(1,15);
  return $victemdamage;
  break;  
  case($hitchance < 23 && $hitchance > 18):
  $victemdamage = rand(5,25);
  return $victemdamage;
  break;
  case($hitchance < 19 && $hitchance > 15):
  $victemdamage = rand(15,35);
  return $victemdamage;
  break;
  case($hitchance < 15 && $hitchance > 10):
  $victemdamage = rand(30,50);
  return $victemdamage;
  break;
  case($hitchance < 10 && $hitchance > 6):
  $victemdamage = rand(45,60);
  return $victemdamage;
  break;
  case($hitchance < 6 && $hitchance > 0):
  $victemdamage = rand(50,75);
  return $victemdamage;
  break;
  case($hitchance < 1 && $hitchance < 0):
  $victemdamage = rand(75,120);
  return $victemdamage;
  break;
 }

 $victemwound = $victemdamage;   
 return $victemwound; ?>
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Ideas on how to do this without so many ifs

Post by requinix »

social_experiment wrote:cwheel3915 : Have you thought about using a switch statement (or multiple switch statements)? [...]
Almost got it: it works until $hitchance=0.

Code: Select all

switch(true)
cwheel3915
Forum Commoner
Posts: 28
Joined: Wed Apr 28, 2010 8:02 pm

Re: Ideas on how to do this without so many ifs

Post by cwheel3915 »

Im going to check out the switch statements. It does looks alot cleaner that way. I dont have a development envirement set up on this pc, so Im going to have to stick it on my flash drive, and check it out later. I will let you know how it went.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Ideas on how to do this without so many ifs

Post by John Cartwright »

Although I do not understand your logic for determing damage, your gethitchance() function as a lookup table translates roughly into

Code: Select all

function gethitchance($hitchance)
{	
	$damages = array_merge(array(), 		
		array_fill(0, 1, rand(75, 120)),	//	0	75-120
		array_fill(1, 5, rand(50, 75)),		//	1-5	50-75
		array_fill(6, 5, rand(45, 60)),		//	6-10	45-60
		array_fill(11, 5, rand(30, 50)),	//	11-15	30-50
		array_fill(16, 5, rand(15, 35)),	//	16-20	15-35
		array_fill(21, 5, rand(5, 25)),		// 	21-25	5-25
		array_fill(26, 5, rand(1, 15))		//	26-30	1-15
	);
	
	return $damages[$hitchance];
}
cwheel3915
Forum Commoner
Posts: 28
Joined: Wed Apr 28, 2010 8:02 pm

Re: Ideas on how to do this without so many ifs

Post by cwheel3915 »

The way im trying to have it work, is that if your victems def minus your attack has a remainder of 25 or less, than you have the chance to actually hit them.

Then the amount of damage done is calculated based on a random number, and the amount of damage you can get up to depends on where the remainder of attack is between 25 and less.

Then you take the amount of damage done away from the victems health, and depending on how much health they have remaining after taking away the damage, you come up with what kind of wound they get . Superficial - Fatal.

I hope that makes sense :/
cwheel3915
Forum Commoner
Posts: 28
Joined: Wed Apr 28, 2010 8:02 pm

Re: Ideas on how to do this without so many ifs

Post by cwheel3915 »

Okay, i did another rewrite, I think negative numbers occurring was giving me problems with the way I was doing it before. Now if the attackers attack is greater than the victims defense, then hit = true, and if victems attack is greater than the attackers defense then
whackback = true.

However it seems that no matter what the condition the return is always Your shots find the target ___ has suffered a wound(it only says wound where it should give the wound name.) but ___ does not miss either you have suffered a wound(same not giving the wound name).


It always returns the result for if hit=true and whackback = true, even if thats not what the result should be.

All I know is im stumped.

Code: Select all


function whack()
{

   
    #get post variables
$victem = isset($_POST['victem']) ? $_POST['victem'] : '';
$charname = $_SESSION['charname'];

#make db connection
$mysql = mysql_connect('localhost', 'root');
mysql_selectdb('mobgame');

#build query to check if victem exist,
$victemquerystring = "SELECT * FROM `character` WHERE `charname` = '$victem' ";
$victemquery = mysql_query($victemquerystring, $mysql);

#if victem exist do query to users stats, and victims stats.
$shooterquerystring = "SELECT * FROM `character` WHERE `charname` = '$charname' ";
$victemquerystring2 = "SELECT * FROM `character` WHERE `charname` = '$victem' ";
$victemquery2 = mysql_query($victemquerystring2, $mysql);
$shooterquery = mysql_query($shooterquerystring, $mysql);
$shooterget = mysql_fetch_assoc($shooterquery); 
$victemget = mysql_fetch_assoc($victemquery2);

#set stats for shooter, and victem
$cattack = $shooterget['attack'];
$cdefense = $shooterget['defense'];
$cstealth = $shooterget['stealth'];
$chealth = $shooterget['health'];
$conhand = $shooterget['onhand'];
$calive = $shooterget['alive'];

$vattack = $victemget['attack'];
$vdefense = $victemget['defense'];
$vstealth = $victemget['stealth'];
$vhealth = $victemget['health'];
$vonhand = $victemget['onhand'];
$valive = $victemget['alive'];

#hit type determining formulations.

#if shooter attack is greater than victem defense
if($cattack > $vdefense)
{
    $hit = "true";
} else {
$hit = "false";
}

#if victem attack is greater than shooter defense whackback = true
if($vattack > $cdefense)
{
    $whackback = "true";
} else {
$whackback = "false";
}

#if hit = true get shot power, and victem wound.
if($hit == "true") {

$shootpower = $cattack - $vdefense;
$vdamage = getshotpower($shootpower);

#get type of wound.
if($vdamage > $vhealth){
$wound = "Fatal";
$valive = 0;
} else {
$vhealth = $vhealth - $vdamage;
$wound = getwoundtype($vhealth);

}
}

#if whackback = true get shot power, and victem wound.
if($whackback == "true") {

$shootpower2 = $vattack - $cdefense;
$cdamage = getshotpower($shootpower);

#get type of wound.
if($cdamage > $chealth){
$wound2 = "Fatal";
$calive = 0;
} else {
$chealth = $chealth - $cdamage;
$wound2 = getwoundtype($chealth);

}
}

#########################################
######### SET UP MESSAGES BASED ON SCENARIO ######
#########################################
if($hit == "true" && $whackback == "false"){   # hit but not whackbacked.

$message = "Your shots find the target" . " " . $victem . " " . "has suffered a" . " " . $wound . " " . "wound";

if(isset($_POST['loot']) && $wound == "Fatal"){

     $conhand = $conhand + $vonhand;
     $message = $message . "<br/>" . "Your victims pockets yielded". " " . "$" . $vonhand . ".";
     
     }
     
}

if($hit == "true" && $whackback == "true"){   # hit but not whackbacked.

$message = "Your shots find the target" . " " . $victem . " " . "has suffered a" . " " . $wound . " " . "wound, but" .
" " . $victem . " " . "does not miss either. You have suffered a" . " " . $wound2 . " " . "wound.";

if(isset($_POST['loot']) && $wound == "Fatal" && $wound2 <> "Fatal"){

     $conhand = $conhand + $vonhand;
     $message = $message . "<br/>" . "Your victims pockets yielded". " " . "$" . $vonhand . ".";
     
     }

}

if($hit == "false" && $whackback == "true"){   # hit but not whackbacked.

$message = "Your shots fail to find the target, but" . " " . $victem . " " . "does not miss. You have suffered a". " " . $wound2 . " " . "wound.";

}

if($hit == "false" && $whackback == "false"){   # hit but not whackbacked.

$message = "Your shots fail to find the target.";

}

echo $message;



}
#############################
####### GET THE POWER OF A SHOT ####
#############################


function getshotpower($shotpower)
{       
  switch($shotpower) {
  
  case($shotpower > 0 && $shotpower < 6):
  $victemdamage = rand(1,30);
  return $victemdamage;
  break;
  
   case($shotpower > 5 && $shotpower < 10):
  $victemdamage = rand(15,50);
  return $victemdamage;
  break;
  
    case($shotpower > 9 && $shotpower < 15):
  $victemdamage = rand(25,60);
  return $victemdamage;
  break;
  
  case($shotpower > 14 && $shotpower < 20):
  $victemdamage = rand(35,80);
  return $victemdamage;
  break;
  
  case($shotpower > 19 && $shotpower < 26):
  $victemdamage = rand(55,100);
  return $victemdamage;
  break;
  
    
  case($shotpower > 25):
  $victemdamage = rand(75,120);
  return $victemdamage;
  break;   
 }
 
 }
  
  
##################################################
######## GET THE NAME OF WOUND INFLICTED #################
##################################################

function getwoundtype($damage)
{
      
if($victemwound < 101 && $victemwound > 90  ){

$wound = "Superficial";
}

if( $victemwound < 91 && $victemwound > 80 ){

$wound = "Significant";
}

if( $victemwound < 81 && $victemwound > 65 ){

$wound = "Major";
}

if( $victemwound < 66 && $victemwound > 40 ){

$wound = "Severe";
}

if( $victemwound < 41 && $victemwound > 20 ){

$wound = "Near Fatal";
}

return $wound;

}

User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Ideas on how to do this without so many ifs

Post by social_experiment »

You could use a switch statement for the 'getwoundtype()' function as well
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: Ideas on how to do this without so many ifs

Post by Benjamin »

Have you tried Johns solution?
cwheel3915
Forum Commoner
Posts: 28
Joined: Wed Apr 28, 2010 8:02 pm

Re: Ideas on how to do this without so many ifs

Post by cwheel3915 »

I did try a version with the array. But still the same result. Im gonna look at it again tommorrow. I have redone this code so many times today, I just cant stand to look at it anymore tonight. It is getting a "cleaner" look everytime though, even if it isnt working as intended.
Post Reply