douan7529 2019-04-11 15:47
浏览 84
已采纳

对象PHP猜数

I make "Guess my number in PHP " and I don't know how to make "tries", because right now I got 10 tries but it count down every time. I guess a number and after 10 tries, I can still guess the number.

I got 3 files. index.php Guess.php The last file is only a view with a form

Index.php file

<?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 file

<?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> -->
  • 写回答

1条回答 默认 最新

  • douhongxie5436 2019-04-11 18:29
    关注

    At some point you just need an if statement in your main logic.

    if($games->tries() < 1 ){
      echo 'To many guesses';
      // Reset the game.
     }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度