如何在表单验证中使用for循环?

时间:2022-01-10 16:11:25

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?

如何在这个游戏中使用循环?这场比赛有10轮。当一名球员获胜时,他将获得+1,直至达到10轮,即比赛结束时。我也可以问一下,当用户正确输入2个字符但错误拼写时,使用了什么功能?例如,ROcc而不是摇滚。它会接受用户输入吗?

<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>

1 个解决方案

#1


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.

在这种情况下,您可以做的是创建一个包含以下顺序的数组: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:

由于我们只有3个元素,因此不会对优化产生任何问题。现在我们必须检查获胜条件。这只是一种方法,我相信还有更好的方法。但它会做:

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

PHP中的startsWith()和endsWith()函数

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

代码尚未在实时服务器上进行测试,因此可能会有一些较小的错误(大写和填充)。

#1


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.

在这种情况下,您可以做的是创建一个包含以下顺序的数组: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:

由于我们只有3个元素,因此不会对优化产生任何问题。现在我们必须检查获胜条件。这只是一种方法,我相信还有更好的方法。但它会做:

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

PHP中的startsWith()和endsWith()函数

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

代码尚未在实时服务器上进行测试,因此可能会有一些较小的错误(大写和填充)。