首页 > 解决方案 > 对象 PHP 猜数

问题描述

我做了“猜猜我的 PHP 号码”,但我不知道如何“尝试”,因为现在我尝试了 10 次,但每次都倒计时。我猜一个数字,经过 10 次尝试,我仍然可以猜到这个数字。

我有3个文件。index.php Guess.php 最后一个文件只是一个带有表单的视图

索引.php 文件

<?php

// Guess my number

require __DIR__ . "/config.php";
require __DIR__ . "/autoload.php";

// Start a named session
session_name("maub");
session_start();

//Deal with incoming variables
$guess = $_POST["guess"] ?? null;
$doInit = $_POST["doInit"] ?? null;
$doGuess = $_POST["doGuess"] ?? null;
$doCheat = $_POST["doCheat"] ?? null;


// Settings for session.
$number = $_SESSION["number"] ?? null;
$tries = $_SESSION["tries"] ?? null;
$games = null;

// Init the game
if ($doInit || $number === null ) {
    session_destroy();
    session_start();
    $games = new Guess();
    $_SESSION["number"] = $games->number();
    $_SESSION["tries"] = $games->tries();
} elseif ($doGuess) {
    $games = new Guess($number, $tries);
    $result = $games -> makeGuess($guess);
    $_SESSION["tries"] = $games -> tries();
}

// Render the page
require __DIR__ . "/view/guess_my_number.php";

echo "<pre>";
var_dump($_POST);
echo "<br>";
var_dump($_SESSION);

Guess.php 文件

<?php
/**
 * Guess my number, a class supporting the game through GET, POST and SESSION.
 */
class Guess
{
    /**
    * @var int $number   The current secret number.
    * @var int $tries    Number of tries a guess has been made.
    */
    private $number = null;
    private $tries = 10;


    /**
    * Constructor to initiate the object with current game settings,
    * if available. Randomize the current number if no value is sent in.
    *
    * @param int $number The current secret number, default -1 to initiate
    *                    the number from start.
    * @param int $tries  Number of tries a guess has been made,
    *                    default 10.
    */

    public function __construct(int $number = -1, int $tries = 10)
    {
        $this->tries = $tries;
        if ($number === -1) {
            $number = rand(1,100);
        }
        $this->number = $number;
    }




    /**
    * Randomize the secret number between 1 and 100 to initiate a new game.
    *
    * @return void
    */
    public function random() : void
    {
        $this->number = rand(1, 100);
    }



    /**
    * Get number of tries left.
    *
    * @return int as number of tries made.
    */

    public function tries() : int
    {
        return $this->tries;
    }




    /**
    * Get the secret number.
    *
    * @return int as the secret number.
    */

    public function number() : int
    {
        return $this->number;
    }




    /**
    * Make a guess, decrease remaining guesses and return a string stating
    * if the guess was correct, too low or to high or if no guesses remains.
    *
    * @throws GuessException when guessed number is out of bounds.
    *
    * @return string to show the status of the guess made.
    */

    public function makeGuess(int $guess) : string
    {
        if ($guess < 1 || $guess > 100 ) {
            throw new GuessException("You can type only number between 1-100");
        }
        $this->tries--;

        if ($guess === $this->number) {
            $result = "Correct! Good Job";
            session_destroy();
        } elseif ($guess > $this->number) {
            $result = "Too high try again!";
        } else {
            $result = "Too low try again!";
        };
        return $result;
    }
}

<h1>Hey guess my number</h1>
<p>Guess a number between 1 and 100, you have <b><?= $tries ?></b> left.</p>

<form method="post">
    <input type="text" name="guess">
    <input type="hidden" name="number" value="<?= $number ?>">
    <input type="hidden" name="tries" value="<?= $tries ?>">
    <input type="submit" name="doGuess" value="Make guess">
    <input type="submit" name="doInit" value="Start from beginning">
    <input type="submit" name="doCheat" value="Cheat">
</form>


<?php if ($doGuess) : ?>
    <p>Your guess is : <?= $guess ?> is <?php echo $result ?>. </p>
<?php endif; ?>

<?php if ($doCheat) :  ?>
    <p>Cheat: Current number is: <?= $number ?>. </p>
<?php endif; ?>

<!-- <pre>
    <?= var_dump($_POST) ?>
</pre> -->

标签: phpclassobject

解决方案


在某些时候,您只需要在主逻辑中添加一个 if 语句。

if($games->tries() < 1 ){
  echo 'To many guesses';
  // Reset the game.
 }

推荐阅读