Page 2 of 2

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

Posted: Sat May 08, 2010 4:44 am
by social_experiment
Could you paste your database structure and a data example or two?

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

Posted: Sat May 08, 2010 9:00 am
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;
  }
 } ?>

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

Posted: Sat May 08, 2010 6:55 pm
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.