0

虽然我知道 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;
        }

    }

    ?>
4

3 回答 3

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();
于 2012-10-03T21:12:14.997 回答
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";
}
于 2012-10-03T21:20:01.840 回答
0

一个粗略的策略是将两个玩家的猜测建模为一个类。将输入验证放入内部以确保不会进行无效猜测。

接下来是一个比较函数,看看谁赢了。您可以像这样将一个猜测与另一个猜测进行比较:

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

我会让你在这一点上试试这个...... :)

于 2012-10-03T21:14:21.283 回答