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
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!";
   }

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

try

<?php
$u = 'Z';//users choice
$c = 'Y';//compute choice
if ($u == $c) $res ='PUSH!';
else {
$a = array('X', 'Y', 'Z');
$k = array_search($u,$a);
$a[]='X';
if ($c == $a[$k+1]) $res = 'LOSES TO'; else $res ='BEATS';
}
echo $res;
?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.