PHP的石头剪刀布蜥蜴Spock

扎克

因此,我是PHP的新手,正在构建RPSLS实现,仅在命令行中执行。我有一个半工作的实现,但有两个问题。

1)当玩家2击败玩家1时,以下代码似乎未达到else条件,我无法确定原因?

2)这是一组非常重复的条件。什么是更有效的实施方式?我真的很想了解如何使它变得更好。

谢谢

<?php

// Assign moves to integers (1 = Rock, 2 = Paper, 3 = Scissors, 4 = Lizard, 5 = Spock)
echo 'Welcome to Rock, Paper Scissors, Lizard, Spock';
echo "\n";

// Randomize Moves
$player1 = rand(1, 5);
$player2 = rand(1, 5);

// Declare wins
$rock_wins = array(3, 4);
$paper_wins = array(1, 5);
$scissors_wins = array(2, 4);
$lizard_wins = array(5, 2);
$spock_wins = array(3, 1);

// Conditional logic for wins
if ($player1 == $player2) {
    echo "Tie.";
    echo "\n";
} elseif ($player1 == 1) {
        if (in_array($player2, $rock_wins)) {
            echo "Player 1 wins";
            echo "\n";
        }
} elseif ($player1 == 2) {
    if (in_array($player2, $paper_wins)) {
            echo "Player 1 wins";
            echo "\n";
        }
} elseif ($player1 == 3) {
    if (in_array($player2, $scissors_wins)) {
            echo "Player 1 wins";
            echo "\n";
        }
}
elseif ($player1 == 4) {
    if (in_array($player2, $lizard_wins)) {
            echo "Player 1 wins";
            echo "\n";
        }
}
elseif ($player1 == 5) {
    if (in_array($player2, $spock_wins)) {
            echo "Player 1 wins";
            echo "\n";
        }
} else {
    echo "Player 2 wins";
}

?>
纳迪尔·桑波利(Nadir Sampaoli)

您可以将逻辑编码为二维数组:

<?php
$play = function ($player1, $player2) {
    $rock = 1;
    $paper = 2;
    $scissors = 3;
    $lizard = 4;
    $spock = 5;
    $matches = array(
        $rock => array($scissor, $lizard),
        $paper => array($rock, $spock),
        $scissors => array($paper, $lizard),
        $lizard => array($spock, $paper),
        $spock => array($scissor, $rock),
    );

    return in_array($player2, $matches[$player1]);
};

// Assign moves to integers (1 = Rock, 2 = Paper, 3 = Scissors, 4 = Lizard, 5 = Spock)
echo 'Welcome to Rock, Paper Scissors, Lizard, Spock';
echo "\n";

// Randomize Moves
$player1 = rand(1, 5);
$player2 = rand(1, 5);

if ($player1 == $player2) {
    echo "Draw!\n";
} else if (Game::play($player1, $player2)) {
    echo "Player 1 wins\n";
} else {
    echo "Player 2 wins\n";
}

显然,改进是无止境的:

  • 您可以play在2d数组上将其替换为闭包(带有方法的类或工厂函数内的嵌套函数),这样就不必在每次调用时都实例化它
  • 您可以使用常量而不是变量来表示岩石,纸张等的值。
  • 应该测试输入内容是否在有效值的集合之内,依此类推。

    <?php
    class Game {
        const ROCK = 1;
        const PAPER = 2;
        const SCISSORS = 3;
        const LIZARD = 4;
        const SPOCK = 5;
        const MATCHES = array(
            self::ROCK => array(self::SCISSOR, self::LIZARD),
            self::PAPER => array(self::ROCK, self::SPOCK),
            self::SCISSORS => array(self::PAPER, self::LIZARD),
            self::LIZARD => array(self::SPOCK, self::PAPER),
            self::SPOCK => array(self::SCISSOR, self::ROCK),
        );
    
        public static function play($player1, $player2) {
            if (!self::isValid($player1) || !self::isValid($player2)) {
                throw new Exception('Invalid input!');
            }
            return in_array($player2, self::matches[$player1]);
        }
    
        public static function isValid($num) {
            return array_key_exists(self::MATCHES, $num);
        }
    }
    

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章