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

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 »

Could you paste your database structure and a data example or two?
“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
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 »

The code below echo's the wound stuff you wanted. Im not sure what the problem was but i think the way you assigned (and checked) the values of '$hit' and '$whackback' was the problem. I left out the loot stuff your last code had in, you can just add that where it needs to be, but test your code with the sample below and see if it works. hth.

Code: Select all

<?php
  $victemquerystring = "SELECT * FROM `character` WHERE charname = '$victem'";
  $victemquery = mysql_query($victemquerystring) or die(mysql_error());
  
  $shooterquerystring = "SELECT * FROM `character` WHERE charname = '$charname'";
  $shooterquery = mysql_query($shooterquerystring) or die(mysql_error());
  $shooterget = mysql_fetch_assoc($shooterquery);
  
  $victemquerystring2 = "SELECT * FROM `character` WHERE charname = '$victem'";
  $victemquery2 = mysql_query($victemquerystring2) or die(mysql_error());
  $victemget = mysql_fetch_assoc($victemquery2);
  
  //attacker statistics
  $cattack = $shooterget['attack'];
  $cdefense = $shooterget['defense'];
  $cstealth = $shooterget['stealth'];
  $chealth = $shooterget['health'];
  $conhand = $shooterget['onhand'];
  $calive = $shooterget['alive'];
  
  //victim statistics
  $vattack = $victemget['attack'];
  $vdefense = $victemget['defense'];
  $vstealth = $victemget['stealth'];
  $vhealth = $victemget['health'];
  $vonhand = $victemget['onhand'];
  $valive = $victemget['alive'];

if ($cattack > $vdefense) {
   $hit = 'true';      
  } 
  else {
   $hit = 'false';  
  }
  //   
  
  if ($vattack > $cdefense) {
   $whackback = 'true';
  }
  else {
   $whackbask = 'false';
  }
  //
    
  if ($hit == true) {
   $shotpower = $cattack - $vdefense;
   $vdamage = getshotpower($shotpower);   
   //get type of wound
   if ($vdamage > $vhealth) {
    $wound = 'Fatal';
	$valive = 0;
   }
   else {
    $vhealth = $vhealth - $vdamage;
	$wound = getwoundtype($vhealth);
   }
  }
  //
  
  if ($whackback == true) {
   $shootpower2 = $vattack - $cdefense;
   $cdamage = getshotpower($shootpower);
  
   if ($cdamage > $chealth) {
    $wound = 'Fatal';
	$calive = 0;
   }
   else {
    $chealth = $chealth - $cdamage;
	$wound2 = getwoundtype($chealth);
   }
  }
  //
  // set up messages based on scenario
  if ($hit == true && $whackback == false) {
   $message = 'Your shots find the target.'. ' '.$victem.' '.'has
   suffered a'. ' '.$wound.' wound';
   echo $message;
  }  
 
  if ($hit == true && $whackback == true) {
   $message = 'Your shot finds the target.'. ' '.$victem.' '.'has
   suffered a'. ' '.$wound.' wound, but '.$victem.' does not miss
   either. You have suffred a '.$wound2.' wound';
   echo $message;   
  }
  
  if ($hit == false && $whackback == true) {
   $message = 'Your shots failt to find the target but '.$victem.' 
   does not miss. You have suffered a '.$wound2.' wound';
   echo $message;
  }
  
  if ($hit == false && $whackback == false) {
   $message = 'Your shots fail to find the target';
   echo $message;
  }
 } ?>
“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
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 something similiar to what you presented this morning social_experiment, and now its workings like a charm. There was really no need to get a true or false value for hit, and whackback. Im not quite sure why I did it in the first place. I wrapped the Whack() function into a couple of switch statements, and for whatever reason it works exactly how I wanted now.

Thanks for all the help guys.
Post Reply