乘法游戏

时间:2016-04-30 05:49:56

标签: php html

游戏规则: -

1-当用户第一次访问该站点时,会提供一个新表达式。

2-用户输入猜测并使用“检查”按钮提交表单。

3-程序检查答案,如果错误提供了正确的消息,并允许用户继续猜测相同的表达。

4-如果用户猜测结果,则提供新表达式。 我面临的游戏问题是: -

1-永远不会得到真实的结果。

2- $表达式不允许用户继续猜测相同的表达式。

注意: 我使用1到12之间的随机数函数rand(1,12)

     <?php
        $a= rand(1, 12);

        $b= rand(1, 12);

        $message;

        $answer = null;

        $expression = $a ." X ".$b ;

        if (!isset($_POST['guess'])) {

             $message = "Welcome to the Multiplication progarm<br/>";

        }

        elseif ($_POST['guess'] == $a*$b) { // matches!

             $message = $a ." X ".$b." = ".$a*$b." is Correct!<br/> Now try the new expression";

             $answer = $_POST['guess'];

        }

        elseif ($_POST['guess'] != $a*$b) { // some other condition

             $message = $_POST['guess']." is Wrong!";

             $answer = $_POST['guess'];

        }

        ?>


        <html>
         <body> 
        <h2><?php echo $message; ?></h2>
         <form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>">
         <input type="hidden" name="answer" value="<?php echo $answer;?>"> 
        <input type="hidden" name="expression" value="<?php echo $expression;?>">
     What is the value of the following multiplication expression: 
<br>
        <br>
     <?php echo $expression; ?><input type="text" name="guess"><br>
         <input type="submit" value="Check">
         </form> 
        </body> 
        </html> 

1 个答案:

答案 0 :(得分:0)

 <?php
    session_start();//you must use session in the first 
    $_SESSION['expression'] = $_POST['expression'];//define session to make the expression in the same value. 
    $_SESSION['answer'] = $_POST['answer'];//define session to make the answer in the same value.
    elseif ($_POST['guess'] == $a*$b) { // matches!
         $message = $a ." X ".$b." = ".$a*$b." is Correct!<br/> Now try the new expression";
         session_destroy();//you must use this to stop session to produces new expression.