godsent Posted January 8, 2010 Share Posted January 8, 2010 Maybe some of you know this game, that two persons pick one of the (paper, scissors, well) and winner determined in this way: paper > well paper < scissors scissors > paper scissors < well well > scissors well < paper And i generate winner like this: $person1 = "Tom"; $person2 = "Mike"; //Same answer if ($person1_choose == $person2_choose) { $winner = "none."; } //paper vs scissors if ($person1_choose == "paper" && $person2_choose == "scissorsr") { $winner = $person2; } if ($person1_choose == "scissorsr" && $person2_choose == "paper") { $winner = $person1; } //paper vc well if ($person1_choose == "paper" && $person2_choose == "well") { $winner = $person1; } if ($person1_choose == "well" && $person2_choose == "paper") { $winner = $person2; } //scissorsr vs well if ($person1_choose == "scissorsr" && $person2_choose == "well") { $winner = $person2; } if ($person1_choose == "well" && $person2_choose == "scissorsr") { $winner = $person1; } echo $winner; If you have any ideas please post. Quote Link to comment https://forums.phpfreaks.com/topic/187696-cleanershorter-code/ Share on other sites More sharing options...
cags Posted January 8, 2010 Share Posted January 8, 2010 How about something like... $beats = array('paper'=>'rock','scissors'=>'paper', 'rock'=>'scissors'); $player1 = $_POST['player1']; $player2 = $_POST['player2']; if($player1 == $player2) { echo "Tie"; } elseif($beats[$player1] == $player2) { echo "Player 1 Wins"; } else { echo "Player 2 Wins"; } Quote Link to comment https://forums.phpfreaks.com/topic/187696-cleanershorter-code/#findComment-990903 Share on other sites More sharing options...
ignace Posted January 8, 2010 Share Posted January 8, 2010 Hehe cags I have to remember that Quote Link to comment https://forums.phpfreaks.com/topic/187696-cleanershorter-code/#findComment-990907 Share on other sites More sharing options...
cags Posted January 8, 2010 Share Posted January 8, 2010 It could probably even be expanded to support rock, paper, scissors, lizard, spock by making the array values another array and using in_array Quote Link to comment https://forums.phpfreaks.com/topic/187696-cleanershorter-code/#findComment-990915 Share on other sites More sharing options...
Lamez Posted January 8, 2010 Share Posted January 8, 2010 Oh I love writing these games. I wrote one in Java, PHP, and C. I even made a little hand held game out of it (Arduino). Quote Link to comment https://forums.phpfreaks.com/topic/187696-cleanershorter-code/#findComment-990993 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.