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

时间:2015-09-15 13:31:03

标签: php loops for-loop

如何在这个游戏中使用循环?这场比赛有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 个答案:

答案 0 :(得分:0)

下次,请考虑发布一些代码,告诉我们您至少尝试解决此问题。

在这种情况下,您可以创建一个包含以下顺序的数组:Rock&gt;剪刀&gt;纸。

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

现在,对于每个玩家,我们必须遍历数组:

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

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

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

现在,对于您的第二个问题,请在下一次将其发布在单独的问题中,或者在网站上查看,它已被回答:

startsWith() and endsWith() functions in PHP

代码尚未在实时服务器上进行测试,因此可能会出现一些较小的错误(大写和其他内容)。