dongyunwei8596 2017-09-12 06:56 采纳率: 100%
浏览 37
已采纳

数字猜测游戏PHP不起作用

I am trying to write a php game that raises an random ($secretNumber) number between 1 and 100, prints it to the screen, prints a form with buttons for the user to select either 'higher' 'lower' or 'correct'. Upon a button press, the outside values are altered accordingly and the computer raises another random number between the lowest and highest values. This continues until the user select correct and the game resets. I am using a session to ensure that the values don't reset each time a button is clicked. My problem is that the $secretNumber resets a couple of times before the $_SESSION locks it in. Also the $randomGuess doesn't seem to lock in before updating.

Any suggestions as to why this isn't working?

<?php session_start() ?>

<?php
$low = 1;
$high = 100;
//create a function to hold a random number, between 1 and 100  and save it 
//to a session variable.
secretNumber();

function secretNumber(){ //********** secretNumber() **********
 global $randomGuess;
        if(!isset($_SESSION["secretNumber"])){
        $secretNumber = rand(1,100);
        $_SESSION['secretNumber'] = $secretNumber;
    } else {
        $secretNumber = $_SESSION["secretNumber"];
        echo($_SESSION['secretNumber']);
    }

    echo 'Secret number is '.$secretNumber;
    echo '  <br/>  ';
    }                   //********** end secretNumber() ***********

    //create a function to create a random guess as to the value of the 
    //first random number

randomGuess();

function randomGuess() { //********** randomGuess() **********
 global $randomGuess;
 global $low;
 global $high;

if (isset($_SESSION['low'])){
        $low = $_SESSION['low'];
} 
if (isset($_SESSION['high'])){
        $high = $_SESSION['high'];
}
    $randomGuess = round($high-(($high-$low)/2));
    echo 'Random guess is '.$randomGuess;
    echo "<br/>";
    $_SESSION['randomGuess'] = $randomGuess;
    }                   // ********** end randomGuess() **********

//create a function to provide a response as to whether the guess is low, 
//high or correct, and adjust the limits of the random guess accordingly.

checkGuess();

function checkGuess() { // ********** checkGuess() **********
 global $low;
 global $high;
 global $randomGuess;

// form to show a 'high' button, a 'low' button and a 'correct' button
// Correct button needs to reset $_SESSION["secretNumber"], and start 
// process over.

    print <<<HERE
        <div>
                <br/><br/><br/>
                <form method = "post" action = ""  style="width:35%; 
       padding-left:35%">
                    <fieldset>

                        <button type = "submit" value="high" name ="high">Too High</button> <br/><br/>
                        <button type = "submit" value="low" name = "low">Too Low</button> <br/><br/>
                        <button type = "submit" value="correct" name = "correct">Correct</button> <br/><br/>

                    </fieldset>
                </form>
            </div>  
HERE;

    if (isset($_POST['low'])) {
                echo "low <br/>";
                echo "randomGuess = ".$randomGuess. "<br/>";
                unset($_SESSION["low"]);
                $low = $randomGuess;
                $_SESSION['low'] = $low;
                unset($_SESSION["randomNumber"]);
    }
    if (isset($_POST['high'])){
                echo "high <br/>";
                echo "randomGuess = ".$randomGuess. "<br/>";
                unset($_SESSION["high"]);
                $high = $randomGuess;
                $_SESSION['high'] = $high;
                unset($_SESSION["randomNumber"]);
    }
 } //                   ********** end checkGuess() **********

if (isset($_POST['correct'])){
            print <<<HERE
            <h2>Yes I guessed the correct secret number. Your secret is out</h2>
    HERE;
        unset($_SESSION["secretNumber"]);// unset the random number as the guess was correct.
            unset($_SESSION["high"]);
            unset($_SESSION["low"]);
        }//end if

echo "low =".$low."<br/>";
echo "high =".$high." <br/>";

?>
  • 写回答

1条回答 默认 最新

  • 普通网友 2017-09-12 08:35
    关注

    Try to check if $secretNumber is same as session.

    var_dump($secretNumber);

    if($secretNumber != $_SESSION["secretNumber"]) { echo "Restart is here"; }

    Test it after every function and then you can see what is problem.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥170 如图所示配置eNSP
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上