mjohnson025 Posted March 5, 2008 Share Posted March 5, 2008 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 Quote Link to comment Share on other sites More sharing options...
mjohnson025 Posted March 5, 2008 Author Share Posted March 5, 2008 ok, so i used a bunch of if and elseif statements to figure it out and it worked, but if anyone knows of a simpler answer that would be very helpful! Mike Quote Link to comment Share on other sites More sharing options...
Barand Posted March 5, 2008 Share Posted March 5, 2008 Have you an example of what you are trying to do? Quote Link to comment Share on other sites More sharing options...
mjohnson025 Posted March 6, 2008 Author Share Posted March 6, 2008 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!"; } Quote Link to comment Share on other sites More sharing options...
johnny44 Posted March 6, 2008 Share Posted March 6, 2008 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. Quote Link to comment Share on other sites More sharing options...
mjohnson025 Posted March 6, 2008 Author Share Posted March 6, 2008 That function is the type of thing I was looking for. I was trying to figure out how to convert the value. Does $letter need to be initialized elsewhere? Quote Link to comment Share on other sites More sharing options...
johnny44 Posted March 6, 2008 Share Posted March 6, 2008 No need to initialize $letter. It just signals that a single entity is to be processed by the function. Quote Link to comment Share on other sites More sharing options...
sasa Posted March 6, 2008 Share Posted March 6, 2008 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; ?> Quote Link to comment Share on other sites More sharing options...
mjohnson025 Posted March 6, 2008 Author Share Posted March 6, 2008 Thanks for the help everyone! Mike Quote Link to comment Share on other sites More sharing options...
deadonarrival Posted March 7, 2008 Share Posted March 7, 2008 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. Quote Link to comment Share on other sites More sharing options...
johnny44 Posted March 7, 2008 Share Posted March 7, 2008 You can compare strings in PHP, so $foo = "z"; $bar = "y"; if($foo > $bar) print "Yes"; will output "Yes" Gee I didn't know that. But I do now. Quote Link to comment Share on other sites More sharing options...
deadonarrival Posted March 7, 2008 Share Posted March 7, 2008 Just don't try it in certain low-level programming languages, they don't usually like it. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.