简体   繁体   中英

Multiplication Game

The Game Rules :-

1- When the user first comes to the site a new expression is provided.

2- The user enters the guess and submits the form using the “Check” button.

3- The program checks the answer and if wrong provides a proper message and allows the user to keep guessing the same expression.

4- If the user guesses the result, then a new expression is provided. The Game problem I face it is :-

1- Never get true result.

2- The $expression not allows the user to keep guessing the same expression.

Note: I use random number between 1 and 12 inclusive function 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> 
 <?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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM