Jump to content

defining values of variables


mjohnson025

Recommended Posts

Hi all!

 

I was wondering if anyone could help me figure out how to define and compare values of one variable to another. For example, I am making a game where the player makes a choice (x,y,z) and cpu makes a choice from the same options. I am trying to define x > z but x < y, and so on.

 

As always, any help greatly appreciated!

 

Thanks,

Mike

Link to comment
https://forums.phpfreaks.com/topic/94592-defining-values-of-variables/
Share on other sites

Yes, this is what I ended up doing, which does work, just not very efficient if I need to add more options.

 

//test if playerChoice is X
if ($playerChoice == "X" && $cpu_Choice == "X") {
   $result = "PUSH!";
   }
   elseif ($playerChoice == "X" && $cpu_Choice == "Y"){
   $result = "LOSES TO";
   }
   elseif ($playerChoice == "X" && $cpu_Choice == "Z"){
   $result = "BEATS";
   }
   
//test if playerChoice is Y
if ($playerChoice == "Y" && $cpu_Choice == "X") {
   $result = "BEATS";
   }
   elseif ($playerChoice == "Y" && $cpu_Choice == "Y"){
   $result = "PUSH!";
   }
   elseif ($playerChoice == "Y" && $cpu_Choice == "Z"){
   $result = "LOSES TO";
   }
   
//test if playerChoice is Y
if ($playerChoice == "Z" && $cpu_Choice == "X") {
   $result = "LOSES TO";
   }
   elseif ($playerChoice == "Z" && $cpu_Choice == "Y"){
   $result = "BEATS";
   }
   elseif ($playerChoice == "Z" && $cpu_Choice == "Z"){
   $result = "PUSH!";
   }

 

 

Use numbers 1,2,3 ... instead of letters X,Y,Z ... for the choices. Then you just need:

 

if ($playerChoice < $cpu_Choice){

  $result = "LOSES TO";

  }

  elseif ($playerChoice > $cpu_Choice){

  $result = "BEATS";

  }

  else{

  $result = "PUSH";

  }

 

Or if you must have letters, then define a function that converts letters into numbers:

 

function convert($letter)

{

if($letter=="X"){$number=0;}

if($letter=="Y"){$number=1;}

if($letter=="Z"){$number=2;}

return $number;

}

 

and then you just need:

 

$A=convert($playerChoice);

$B=convert($cpu_Choice);

 

if ($A < $B){$result = "LOSES TO";}

elseif ($A > $B){$result = "BEATS";}

else{$result = "PUSH";}

 

To add more options, just add to the function.

Use numbers 1,2,3 ... instead of letters X,Y,Z ... for the choices. Then you just need:

 

if ($playerChoice < $cpu_Choice){

  $result = "LOSES TO";

  }

  elseif ($playerChoice > $cpu_Choice){

  $result = "BEATS";

  }

  else{

  $result = "PUSH";

  }

 

Or if you must have letters, then define a function that converts letters into numbers:

 

function convert($letter)

{

if($letter=="X"){$number=0;}

if($letter=="Y"){$number=1;}

if($letter=="Z"){$number=2;}

return $number;

}

 

and then you just need:

 

$A=convert($playerChoice);

$B=convert($cpu_Choice);

 

if ($A < $B){$result = "LOSES TO";}

elseif ($A > $B){$result = "BEATS";}

else{$result = "PUSH";}

 

To add more options, just add to the function.

 

You can compare strings in PHP, so

$foo = "z";

$bar = "y";

if($foo > $bar) print "Yes";

 

will output

"Yes"

 

Just compare the alphabetical strings and PHP does the rest :) exactly the same as comparing numbers.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.