有人能告诉我如何能够明智地处理这个代码对象。

时间:2012-10-03 21:01:07

标签: php

虽然我知道OOP的基础知识,但我不知道如何使这个正常的PHP代码像OOP一样。我写下的是一个摇滚纸剪刀有点游戏。有人请你花一些时间来帮助我,我真的想要变得更好。我会很感激。

<?php

    if(isset($_POST['submit']))
    {

        $array = array('water', 'wood' 'fire');
        $enemy_guess = array_rand(1,3);
        $player_guess = $_POST['picked_skill'];

            switch ($player_guess){

            case 'water':

                if($enemy_guess == 'fire'){
                        echo "you won";
                    }
                }else
                    {
                        echo "you lost";
                    }
            break;

            case 'wood':

                if($enemy_guess == 'water'){
                        echo "you won";
                }
                }else
                    {
                        echo "you lost";
                    }
            break;

            case 'fire':

                if($enemy_guess == 'wood'){
                        echo "you won";
                }
                }else
                    {
                        echo "you lost";
                    }
            break;
        }

    }

    ?>

3 个答案:

答案 0 :(得分:2)

这是一个例子,考虑到你不考虑绘制场景,所以我也没有。 如果您有疑问,请继续制作:)

class Game {

   private $player_guess;
   private $enemy_guess;
   private $rules = array( //basically, water beats fire, fire beats wood and wood beats water, anything else is a lose scenario.
           array("water" => "fire"),
           array("fire" => "wood"),
           array("wood" => "water"));

   private $options = array("water", "wood", "fire");


   public function compGuess() {    
        $this->enemy_guess = array_rand($this->options);
   }
   public function playerGuess($guess) {
        $this->player_guess = $guess;
   }

   public function result() {
        if($this->rules[$this->enemy_guess] == $this->player_guess) {
          echo "You loose";
        } else {
           echo "You WIN!";       
        }

   }
}

//Usage:
$game = new Game();
$game->compGuess();
$game->playerGuess($_POST['picked_skill']);
$game->result();

答案 1 :(得分:0)

一个粗略的策略是将两个球员的猜测模型化为一个阶级。将输入验证放在内部以确保不会进行无效猜测。

接下来将是一个比较函数,以查看谁获胜。你将一个猜测与另一个猜测比较如下:

$result = $guess->compare($otherGuess);

我现在让你试试这个......:)

答案 2 :(得分:0)

有很多方法可以做OOP,这取决于你。这就是我要做的事。

class Skill
{
    static $skills = array("Wood", "Water", "Fire");
    $skill;

    __construct($skill)
    {
        if is_int($skill)
        {
            $this->skill = $skill;
        }
        else
        {
            $this->skill = array_search($skill, self::$skills);
        }
    }

    function Beats($other)
    {
        $beat_other = $other->skill - 1;
        if($beat_other < 0)
        {
            $beat_other = count(self::$skills) - 1;
        }
        if($beat_other == $this->skill)
        {
            return true;
        }
        return false;
    }
}

$Enemy_Skill = new Skill(rand(0,2));
$My_Skill = new Skill($_POST['picked_skill']);

if($My_Skill->Beats($Enemy_Skill))
{
    echo "You Win";
}
elseif($Enemy_Skill->Beats($My_Skill))
{
    echo "You Lose";
}
else
{
    echo "Draw";
}