Page 1 of 2 12 LastLast
Results 1 to 10 of 16

Thread: Day 8 - Lets Make A Game - Part 1 - Become PHP Expert in 30 days

  1. #1
    Join Date
    Sep 2003
    Posts
    3,040

    Default Day 8 - Lets Make A Game - Part 1 - Become PHP Expert in 30 days

    Lets Make A Game In PHP - Part 1



    We make a simple number guessing game with few lines of PHP and HTML code. The goal of the game is to find the secret number generated by server using rand() function.

    Random Number Generation

    Most games need to do actions based on random conditions . PHP provide rand() function to generate random numbers.

    day_8_ex_1.php


    PHP Code:
    <?php

    echo 'My Random Number is: ' rand(1,100);
    Above code will generate a random number between 1 and 100.

    Modify the script, change numbers 1 and 100, so that you can have random number in different range. For example, if we are making Snakes and Ladders game, we will have 1 to 6 as result when we throw (or rolls) the dice. In that cause, above example will be




    PHP Code:
    <?php

    echo '<h1>Computer as Dice</h1>';
    echo 
    '<p>Use it when your Dice broken.</p>';

    echo 
    'Your number is: ' rand(1,6);


    day_8_ex_2.php

    Time to write our own Number Guessing Game. Try to understand the code, if you don't understand any part of the code, ask in the forum. Don't worry if you don't understand the code, we will be working on it in next few days, you will eventually get used to the code

    PHP Code:
    <html>
    <body>

    <h1>Number Guessing Game.</h1>

    <?php

    if (isset($_GET['secretNumber'])) {
        
    $secretNumber $_GET['secretNumber'];
    } else {
        
    $secretNumber rand(1,10);
    }

    if (isset(
    $_GET['userNumber'])) {
        
    $userNumber $_GET['userNumber'];

        if (
    $secretNumber $userNumber) {
            echo 
    '<h1 style="color:red">Your number is too SMALL.</h1>';
        } else if (
    $secretNumber $userNumber) {
            echo 
    '<h1 style="color:blue">Your number is too BIG.</h1>';
        } else {
            echo 
    '<h1 style="color:green">You win the game.</h1>';
        }
    }

    ?>

    <form method="GET" action="">
        Enter Number: <input name="userNumber" type="text">
        <button type="submit">Check</button>
        <input type="hidden" name="secretNumber" value="<?php echo $secretNumber?>">
    </form>

    <h2>How to Play:</h2>

    <pre>
    * Server will pick a secret number between 1 and 10.
    * You guess what number it is.
    * If your guess is too high or too low, Server will give you a hint.
    * See how many turns it takes you to win!
    </pre>

    </body>
    </html>
    This is simple game, there are multiple way to hack and win this game, this is not meant to play, just for learning.

    Tomorrow we will discuss how to make this game more secure, add some more features like how many try it take you to get the correct answer, this way game will be more interesting.

    Exercise

    * Update the script so that the random number range will be 1 to 100 instead of 1 to 10.





    Become PHP Expert in 30 days
    FreeMarriage.com - Free Online Matrimonial
    FlashWebHost.com - Professional Web Hosting, Designing.

  2. #2
    Join Date
    Sep 2003
    Location
    india
    Posts
    11,527

    Default

    http://php.flashwebhost.com/annie/game.php

    Code:
    <html>
    <body>
    
    <h1>Number Guessing Game.</h1>
    
    <?php
    
      if (isset($_GET['secretNumber'])) {
       $secretNumber = $_GET['secretNumber'];
      } else {
       $secretNumber = rand(1,100);
      }
      if (isset($_GET['userNumber'])) {
      $userNumber = $_GET['userNumber'];
    
      if ($secretNumber > $userNumber) {
      echo '<h1 style="color:Red">Your number is too SMALL.</h1>';
      } else if ($secretNumber < $userNumber) {
       echo '<h1 style="color:DarkOrchid ">Your number is too BIG.</h1>';
      } else {
       echo '<h1 style="color:green">You win the game.</h1>';
      }
       } 
      ?> 
      <form method="GET" action="">
       Enter Number: <input name="userNumber" type="text">
      <button type="submit">Check</button>
      <input type="hidden" name="secretNumber" value="<?php echo $secretNumber; ?>">
       </form>
    
      <h2>How to Play:</h2>
    
      <pre>
      * Server will pick a secret number between 1 and 100.
      * You guess what number it is.
      * If your guess is too high or too low, Server will give you a hint.
      * See how many turns it takes you to win!
      </pre>
    
       </body>
      </html>

  3. #3
    Join Date
    Feb 2005
    Location
    India
    Posts
    11,004

    Default

    http://php.flashwebhost.com/melbin/number_game.php

    PHP Code:
    <html>
    <body>

    <h1>Number Guessing Game.</h1>

    <?php

    if (isset($_GET['secretNumber'])) {
        
    $secretNumber $_GET['secretNumber'];
    } else {
        
    $secretNumber rand(1,100);
    }

    if (isset(
    $_GET['userNumber'])) {
        
    $userNumber $_GET['userNumber'];

        if (
    $secretNumber $userNumber) {
            echo 
    '<h1 style="color:red">Your number is too SMALL.</h1>';
        } else if (
    $secretNumber $userNumber) {
            echo 
    '<h1 style="color:blue">Your number is too BIG.</h1>';
        } else {
            echo 
    '<h1 style="color:green">You win the game.</h1>';
        }
    }

    ?>

    <form method="GET" action="">
        Enter Number: <input name="userNumber" type="text">
        <button type="submit">Check</button>
        <input type="hidden" name="secretNumber" value="<?php echo $secretNumber?>">
    </form>

    <h2>How to Play:</h2>

    <pre>
    * Server will pick a secret number between 1 and 10.
    * You guess what number it is.
    * If your guess is too high or too low, Server will give you a hint.
    * See how many turns it takes you to win!
    </pre>

    </body>
    </html>
    VIDEO WORLD : LATEST HOLLYWOOD || BOLLYWOOD || SOUTH INDIAN VIDEOS || TRAILERS

  4. #4
    Join Date
    Nov 2009
    Posts
    76,596

    Default

    http://php.flashwebhost.com/sherly/n...ssinggames.php

    Code:
    <html>
    <body>
    
    <h1>Number Guessing Game.</h1>
    
    <?php
    
    if (isset($_GET['secretNumber'])) {
        $secretNumber = $_GET['secretNumber'];
    } else {
        $secretNumber = rand(1,500);
    }
    
    if (isset($_GET['userNumber'])) {
        $userNumber = $_GET['userNumber'];
    
        if ($secretNumber > $userNumber) {
            echo '<h1 style="color:Orange">Your number is too SMALL.</h1>';
        } else if ($secretNumber < $userNumber) {
            echo '<h1 style="color:Yellow">Your number is too BIG.</h1>';
        } else {
            echo '<h1 style="color:Violet">You win the game.</h1>';
        }
    }
    
    ?>
    
    <form method="GET" action="">
        Enter Number: <input name="userNumber" type="text">
        <button type="submit">Check</button>
        <input type="hidden" name="secretNumber" value=   "<?php echo $secretNumber; ?>">
    </form>
    
    <h2>How to Play:</h2>
    
    <pre>
    * Server will pick a secret number between 1 and 500.
    * You guess what number it is.
    * If your guess is too high or too low, Server will give you a hint.
    * See how many turns it take you to win!
    </pre>
    
    </body>
    </html>

  5. #5
    Join Date
    Nov 2009
    Location
    kerala
    Posts
    19,076

    Default Guess Winning number Game using PHP

    http://php.flashwebhost.com/sibichan...ing_number.php

    PHP Code:

    <html><body>
    <h1>Winning Number Guessing Game.</h1>
    <?php
    if (isset($_GET['winningNumber'])) {

        
    $winningNumber $_GET['winningNumber'];

    } else {

        
    $winningNumber rand(1,10);}
    if (isset(
    $_GET['luckyNumber'])) {

        
    $luckyNumber $_GET['luckyNumber'];
        if (
    $winningNumber != $luckyNumber){

            echo 
    '<h1 style ="color:#009900">Your lucky number is ' $luckyNumber.'<br> The winning Number is ' $winningNumber .'<br> No winnings this time. Please try again later </h1>';

            echo 
    '<hr>';    }      else {

             echo 
    '<h1 style ="color:#009900"> Your lucky number is ' $luckyNumber.'<br> The winning Number is ' $winningNumber .'<br><span style=color:red>Hurray ! you won the game </h1>';

            echo 
    '<hr>';

        }

    }
    ?>
    <form method="GET" action="">    Enter your Number lucky No (from 1 to 10): <input name="luckyNumber" type="text">    <button type="submit">Check</button>    <input type="hidden" name="winningNumber" value="<?php echo $winningNumber?>"></form>

    </body></html>
    Last edited by Vahaa11; 05-19-2014 at 11:42 AM.

  6. #6
    Join Date
    Nov 2004
    Location
    India
    Posts
    65

    Default

    http://php.flashwebhost.com/stefin/number_guess.php

    Code:
    <html>
    <body>
    
    
    <h1>Guess My Number!</h1>
    
    
    <?php
    
    
    if (isset($_GET['secretNumber'])) {
    $secretNumber = $_GET['secretNumber'];
    } else {
    $secretNumber = rand(10,800);
    }
    
    
    if (isset($_GET['userNumber'])) {
    $userNumber = $_GET['userNumber'];
    
    
    if ($secretNumber > $userNumber) {
    echo '<h1 style="color:LimeGreen">Your guess is too low.</h1>';
    } else if ($secretNumber < $userNumber) {
    echo '<h1 style="color:red ">Your guess is too high.</h1>';
    } else {
    echo '<h1 style="color:green">You win the game.</h1>';
    }
    }
    
    
    ?>
    
    
    <form method="GET" action="">
    Enter letter: <input name="userNumber" type="text">
    <button type="submit">Check</button>
    <input type="hidden" name="secretNumber" value="<?php echo $letter; ?>">
    </form>
    
    
    <h2>Tips for Playing Game:</h2>
    
    
    <pre>
    * Server will pick a secret letter between 10 and 800.
    * You guess what letter it is.
    * If your guess is too high or too low, Server will give you a hint.
    </pre>
    
    
    </body>
    </html>
    Last edited by stefin; 05-19-2014 at 03:53 PM.

  7. #7
    Join Date
    May 2014
    Posts
    21

    Default

    what does this code do?
    Code:
     <input type="hidden" name="secretNumber" value="<?php echo $secretNumber; ?>">
    and i didnt understand how this works.

    and also why is the action left blank? shouldnt be it like action ="fileName.php"?

    http://php.flashwebhost.com/austin/day8.php
    Last edited by Austinwhite; 05-19-2014 at 01:58 PM.

  8. #8
    Join Date
    Sep 2003
    Posts
    3,040

    Default

    Quote Originally Posted by Austinwhite View Post
    what does this code do?
    Code:
     <input type="hidden" name="secretNumber" value="<?php echo $secretNumber; ?>">
    and i didnt understand how this works.

    and also why is the action left blank? shouldnt be it like action ="fileName.php"?

    http://php.flashwebhost.com/austin/day8.php

    INPUT type = hidden means an input field, but user can see it. It is used when we need to submit some data to server, but.. we don't want visitors see it.

    When

    [code]
    <form method="GET" action="">
    [/coe]

    It it submit to itself, that is all data send to the code itself. We use

    Code:
    if (isset($_GET['secretNumber'])) {
    and


    Code:
    if (isset($_GET['userNumber'])) {
    To see if any value is sent to the server.
    Become PHP Expert in 30 days
    FreeMarriage.com - Free Online Matrimonial
    FlashWebHost.com - Professional Web Hosting, Designing.

  9. #9
    Join Date
    Sep 2003
    Posts
    3,040

    Default

    Quote Originally Posted by stefin View Post
    http://php.flashwebhost.com/stefin/number_guess.php

    Code:
    <html>
    <body>
    
    
    <h1>Guess My Number!</h1>
    
    
    <?php
    
    
    if (isset($_GET['secretNumber'])) {
    $secretNumber = $_GET['secretNumber'];
    } else {
    $secretNumber = rand(10,800);
    }
    
    
    if (isset($_GET['userNumber'])) {
    $userNumber = $_GET['userNumber'];
    
    
    if ($secretNumber > $userNumber) {
    echo '<h1 style="color:LimeGreen">Your guess is too low.</h1>';
    } else if ($secretNumber < $userNumber) {
    echo '<h1 style="color:red ">Your guess is too high.</h1>';
    } else {
    echo '<h1 style="color:green">You win the game.</h1>';
    }
    }
    
    
    ?>
    
    
    <form method="GET" action="">
    Enter letter: <input name="userNumber" type="text">
    <button type="submit">Check</button>
    <input type="hidden" name="secretNumber" value="<?php echo $letter; ?>">
    </form>
    
    
    <h2>Tips for Playing Game:</h2>
    
    
    <pre>
    * Server will pick a secret letter between 10 and 800.
    * You guess what letter it is.
    * If your guess is too high or too low, Server will give you a hint.
    </pre>
    
    
    </body>
    </html>

    @stefin, in this code

    Code:
    <input type="hidden" name="secretNumber" value="<?php echo $letter; ?>">
    Should be

    Code:
    <input type="hidden" name="secretNumber" value="<?php echo $secretNumber; ?>">
    This is the number visitor need to guess.

    We generate it with rand() function if that number on first visit to the page using following code.


    Code:
    if (isset($_GET['secretNumber'])) {
        $secretNumber = $_GET['secretNumber'];
    } else {
        $secretNumber = rand(10,800);
    }
    Become PHP Expert in 30 days
    FreeMarriage.com - Free Online Matrimonial
    FlashWebHost.com - Professional Web Hosting, Designing.

  10. #10
    Join Date
    Apr 2005
    Posts
    46,704

    Default

    Code:
    <html>
    <body>
    
    <h1>Number Guessing Game.</h1>
    
    <?php
    
    if (isset($_GET['secretNumber'])) {
        $secretNumber = $_GET['secretNumber'];
    } else {
        $secretNumber = rand(1,20);
    }
    
    if (isset($_GET['userNumber'])) {
        $userNumber = $_GET['userNumber'];
    
        if ($secretNumber > $userNumber) {
            echo '<h1 style="color:blue">Your Guess is too Low.</h1>';
        } else if ($secretNumber < $userNumber) {
            echo '<h1 style="color:Red">Your Guess is too High.</h1>';
        } else {
            echo '<h1 style="color:green">You win the game.</h1>';
        }
    }
    
    ?>
    
    <form method="GET" action="">
        Guess A Number: <input name="userNumber" type="text">
        <button type="submit">Check</button>
        <input type="hidden" name="secretNumber" value="<?php echo $secretNumber; ?>">
    </form>
    
    <h2>How to Play:</h2>
    
    <pre>
    * Server will pick a secret number between 1 and 20.
    * You guess what number it is.
    * If your guess is too high or too low, Server will give you a hint.
    * See how many turns it takes you to win!
    </pre>
    
    </body>
    </html>
    http://php.flashwebhost.com/mini/game.php

Page 1 of 2 12 LastLast

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •