-2

How to use for loop in this game? The game has 10 rounds. When a player wins, he scores +1 until it reaches 10 rounds that is when the game will end. And also can I ask what function is used when a user inputs 2 char correctly but misspelled it? For example, ROcc instead of rock. Will it accept the user input?

<html>
<body>
<h1>ROCK PAPER SCISSORS</h1>
<?php

print ('<form action="" method="post">');
print ('<p>Player 1: <input type="text" name="p1" /></p>');
print ('<p>Player 2: <input type="text" name="p2" /></p>');
print ('<input type="submit" name="submit" value="PLAY" />');
print ('</form>');

if (isset($_POST['submit'])) {
    $player1score = 0;
    $player2score = 0;
    $draw = 0;
    $player1 = strtolower($_POST['p1']);
    $player2 = strtolower($_POST['p2']);

    if ($player1 == 'scissors' && $player2 == 'scissors') {
        $draw++;
        print "Player 1: $player1score<br>";
        print "Player 2: $player2score<br>";
        print "DRAW: $draw";
    }

    else if ($player1 == 'rock' && $player2 == 'rock') {
        $draw++;
        print "Player 1: $player1score<br>";
        print "Player 2: $player2score<br>";
        print "DRAW: $draw";
    }

    else if ($player1 == 'paper' && $player2 == 'paper') {
        $draw++;
        print "Player 1: $player1score<br>";
        print "Player 2: $player2score<br>";
        print "DRAW: $draw";
    }

    else if ($player1 =='rock' && $player2 =='scissors') {
        $player1score++;
        print "Player 1: $player1score<br>";
        print "Player 2: $player2score<br>";
        print "DRAW: $draw";
    }

    else if ($player1 == 'rock' && $player2 =='paper') {
        $player2score++;
        print "Player 1: $player1score<br>";
        print "Player 2: $player2score<br>";
        print "DRAW: $draw";

    }

    else if($player1 == 'scissors' && $player2 == 'rock') {

        $player2score++;
        print "Player 1: $player1score<br>";
        print "Player 2: $player2score<br>";
        print "DRAW: $draw";

    }

    else if ($player1 =='scissors' && $player2 =='paper') {
        $player1score++;
        print "Player 1: $player1score<br>";
        print "Player 2: $player2score<br>";
        print "DRAW: $draw";

    }
    else if ($player1 =='paper' && $player2 =='rock') {
        $player1score++;
        print "Player 1: $player1score<br>";
        print "Player 2: $player2score<br>";
        print "DRAW: $draw";

    }

    else if ($player1 =='paper' && $player2 == 'scissors') {
        $player2score++;
        print "Player 1: $player1score<br>";
        print "Player 2: $player2score<br>";
        print "DRAW: $draw";
    }
}
?>
</html>
</body>
Dan Lowe
  • 51,713
  • 20
  • 123
  • 112
  • 2
    You have a try, then show us if you cannot get it to work. We do not write it for you. – RiggsFolly Sep 15 '15 at 13:40
  • humongous code duplication... – Karoly Horvath Sep 15 '15 at 13:49
  • a couple of hints: instead relying on user input, use – Marek Bettman Sep 15 '15 at 14:11

1 Answers1

0

Next time, please consider posting some code to show us that you at least tried to solve the issue.

What you could do, in this situation, is create an array with the order: Rock > scissors > paper.

$orderArray = array("rock", "scissors", "paper");

Now, for each player we have to loop through the array:

$player1Answer = 0;
$player2Answer = 0;
for ($i = 0; $i < count($orderArray); $i++) {
    if ($player1 == $orderArray[i])
       $player1Answer = i;
    if ($player2 == $orderArray[i])
       $player2Answer = i;
}

As we only have 3 elements, this doesn't cause any problems for optimisation. Now we have to check for the winning condition. This is just one way to do it, and I'm sure that there are even better ways. But it will do:

if ($player1Answer == $player2Answer)
    // DRAW
else if ($player1Answer == $player2Answer + 1 || ($player1Answer == 2 && $player2Answer == 0)
    // 1 wins
else
    // 2 wins

Now, for your 2nd question, please, next time, post it in a separate question, or have a look on the site, it has been answered:

startsWith() and endsWith() functions in PHP

The code has not been tested on a live server, so might have some smaller errors (capitalizing and stuff).

Community
  • 1
  • 1
Jordumus
  • 2,763
  • 2
  • 21
  • 38