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.

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

报告相同问题?

悬赏问题

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