Web Analytics Made Easy -
StatCounter Guessing Game Problems - CodingForum

Announcement

Collapse
No announcement yet.

Guessing Game Problems

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Guessing Game Problems

    I'm new at php, started to make a guessing game, and I wanted the script to tell you how many guesses you made. What did I do wrong/how do I fix it?

    PHP Code:
    <html>
    <head>
    <title>Guessing Game</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    </head>
    <body>
    <form name="numberform" method="post" action="Guessinggame.php">
      <p align="center"><font face="Geneva, Arial, Helvetica, sans-serif">PHP Guessing 
        Game</font></p>
      <p align="center"><font face="Geneva, Arial, Helvetica, sans-serif">Guess the 
        number! The number is between 0 and 10,000!</font></p>
      <p> <font face="Geneva, Arial, Helvetica, sans-serif">
        <input name="numberguess" type="text" id="numberguess" value="0" maxlength="4">
        <input type="submit" name="numberSubmit" value="Submit">
        </font></p>
    </form>
    <font face="Geneva, Arial, Helvetica, sans-serif">
    <?php
      $i 
    0;
      
    $correctanswer 4572;
      if(
    $numberguess == $correctanswer){
      echo 
    "You got it!\n";
      echo 
    "You guessed ".$i." times\n";
      }
      if(
    $numberguess $correctanswer){
      echo 
    "Too high. Try again!\n";
      
    $i $i 1;
      }
      if(
    $numberguess $correctanswer){
      echo 
    "Too low. Try again!\n";
      
    $i $i 1;
      }
      
    ?>
    </font>
    </body>
    </html>
    Thanks!

  • #2
    Rather than just fix the script, I'll give you some bits of advice:

    1- Everytime someone clicks that submit button and it executes the code, it resets the variable $i to 0...every time

    2-
    PHP Code:
    $i $i 1
    can be written as
    PHP Code:
    $i++; 
    3- I think you'll find it easier if you seperate the two parts of your application (the display, and the logic). In otherwords, have the form as one document, and the code that processes it as another.

    4- in your script, you reference the variable
    PHP Code:
    $numberguess 
    , in the newer versions of PHP, that variable doesn't exist unless a thing called REGISTER_GLOBALS is turned on. in your case, it may or not be. A safer way of referring to that variable, would be to replace all instances of
    PHP Code:
    $numberguess 
    with
    PHP Code:
    $_POST["numberguess"
    5- You will need a way to save the number of guesses each time. this could be done by storing the value in a cookie, writing hte number of guesses to a file or database, writing a hidden form field with the number of guesses, or passing the number of guesses via the url like http://www.mysite.com/guessingame.php?num_guesses=3



    I can quickly write a version of your code that works, but I think you'll learn more by trying to figure out how to fix your problem(s).


    Let me know if you get stuck, I'll be glad to help!

    HTH,
    -Celt

    Comment


    • #3
      1. Everytime someone submits it resets the variable
      Thanks for pointing that out.
      2. Ok, I didn't know PHP was so much like C++! That's good :P
      3- Separate the two parts of your application.
      Done.
      4- in your script, you reference the variable $numberguess, etc.
      I had made sure Register_Globals was turned on.

      5- You will need a way to save the number of guesses each time., by storing the value in a cookie, writing the number of guesses to a file or database, writing a hidden form field with the number of guesses, or passing the number of guesses via the url like http://www.mysite.com/guessingame.php?num_guesses=3
      Since I'm new at PHP, I've only learned how to write things to a file. How would I pass the number of guesses via a url? Or a hidden form field? *I feel dumb*


      Anyway,
      Thanks much for your detailed response. You're so very nice.


      What does HTH mean?

      Comment


      • #4
        HTH = hope that helps


        I'd recommend using the hidden form field..

        Uhm...here's how I would do it, i think

        file: guessing_game.php
        PHP Code:
        <?php
        $correctanswer 
        4572;
        $messsage      "";
        $output        "";
        $html_form     "guessing_game.htm";

        if (isset(
        $_POST["numberguess"])) {
           
        $num_guesses $_POST["num_guesses"];

           if (
        $_POST["numberguess"] == $correctanswer) {
              
        $message  "You got it!\n";
              
        $message .= "You guessed " $num_guesses ." times\n";
           }
           if (
        $_POST["numberguess"] > $correctanswer){
              
        $message "Too high. Try again!\n";
              
        $num_guesses++;
           }
           if (
        $_POST["numberguess"] < $correctanswer){
              
        $message "Too low. Try again!\n";
              
        $num_guesses++;
           }

        } else {
          
        $num_guesses 1;
        }

        $fp_ar file($html_form);
        foreach (
        $fp_ar as $line) {
           
        $output .= $line;
        }

        $output str_replace("<!--{{NUM_GUESSES}}-->",$num_guesses,$output);
        $output str_replace("<!--{{MESSAGE}}-->",$message,$output);
        print 
        $output;

        ?>
        file guessing_game.htm
        Code:
        <html>
        <head>
        <title>Guessing Game</title>
        <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
        </head>
        <body>
        <form name="numberform" method="post" action="guessing_game.php">
        <input type="hidden" name="num_guesses" value="<!--{{NUM_GUESSES}}-->">
          <p align="center"><font face="Geneva, Arial, Helvetica, sans-serif">PHP Guessing 
            Game</font></p>
          <p align="center"><font face="Geneva, Arial, Helvetica, sans-serif">Guess the 
            number! The number is between 0 and 10,000!</font></p>
          <p> <font face="Geneva, Arial, Helvetica, sans-serif">
            <input name="numberguess" type="text" id="numberguess" value="0" maxlength="4">
            <input type="submit" name="numberSubmit" value="Submit">
            </font></p>
        </form>
        <font face="Geneva, Arial, Helvetica, sans-serif">
        <!--{{MESSAGE}}-->
        </font>
        </body>
        </html>
        What I did was essentially place a section of code in the form of a comment into the display page. I then substitute it for the number of guesses.


        I haven't tested the code, but I think it is correct.
        Last edited by Celtboy; Feb 8, 2004, 05:36 PM.

        Comment


        • #5
          Thanks, I did all the code as you mentioned (and fixed some minor erros), but the result of me guessing the correct answer is:
          "You guessed times", without a number in between "guessed" and "times". Any thoughts?

          Comment


          • #6
            ok, using the following:

            file: guess.php
            PHP Code:
            <?php
            $correctanswer 
            4572;
            $message      "";
            $output        "";
            $html_form     "guess.htm";

            if (isset(
            $_POST["numberguess"])) {
               
            $num_guesses $_POST["num_guesses"];

               if (
            $_POST["numberguess"] == $correctanswer) {
                  
            $message  "You got it!\n";
                  
            $message .= "You guessed " $num_guesses ." times\n";
               }
               if (
            $_POST["numberguess"] > $correctanswer){
                  
            $message "Too high. Try again!\n";
                  
            $num_guesses++;
               }
               if (
            $_POST["numberguess"] < $correctanswer){
                  
            $message "Too low. Try again!\n";
                  
            $num_guesses++;
               }

            } else {
              
            $num_guesses 1;
            }

            $fp_ar file($html_form);
            foreach (
            $fp_ar as $line) {
               
            $output .= $line;
            }

            $output str_replace("<!--{{NUM_GUESSES}}-->",$num_guesses,$output);
            $output str_replace("<!--{{MESSAGE}}-->",$message,$output);
            print 
            $output;

            ?
            guess.htm:
            Code:
            <html>
            <head>
            <title>Guessing Game</title>
            <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
            </head>
            <body>
            <form name="numberform" method="post" action="guess.php">
            <input type="hidden" name="num_guesses" value="<!--{{NUM_GUESSES}}-->">
              <p align="center"><font face="Geneva, Arial, Helvetica, sans-serif">PHP Guessing 
                Game</font></p>
              <p align="center"><font face="Geneva, Arial, Helvetica, sans-serif">Guess the 
                number! The number is between 0 and 10,000!</font></p>
              <p> <font face="Geneva, Arial, Helvetica, sans-serif">
                <input name="numberguess" type="text" id="numberguess" value="0" maxlength="4">
                <input type="submit" name="numberSubmit" value="Submit">
                </font></p>
            </form>
            <font face="Geneva, Arial, Helvetica, sans-serif">
            <!--{{MESSAGE}}-->
            </font>
            </body>
            </html>

            If I access guess.php, it works fine.

            Comment

            Working...
            X