Jump to content

Cleaner/Shorter code?


godsent

Recommended Posts

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. :)

Link to comment
Share on other sites

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

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.