jeudi 24 décembre 2015

Verify the figure gap between 2 numbers and a random number in PHP

I have a little problem with PHP. I create a little game in PHP, in which two players must choose a number, and the one who is closest wins.

Here are the rules:

  • Player 1 chooses the minimum number (to generate the random number to find) CHECK

  • Player 2 chooses the maximum number (to generate the random number to find) CHECK

  • The two players pick their numbers (between the minimum and maximum)

  • The player who is closest to the randomly generated number (between the minimum and maxium), win the game

This is my script

<form action="" method="post">
    <input name="min" type="number" placeholder="Min" />
    <input name="max" type="number" placeholder="Max" />
    <input name="player1" type="number" placeholder="player1" />
    <input name="player2" type="number" placeholder="player2" />
    <input name="submit" type="submit" />
</form>

<?php

    // Variables

    $min = $_POST['min'];
    $max = $_POST['max'];
    $player1 = $_POST['player1'];
    $player2 = $_POST['player2'];


    // Get Random Number

    function getRandomNumber($min, $max) {

        return mt_rand($min, $max);

    }


    // Submit Form

    if (isset($_POST['submit'])) {

        echo "The random number is " . getRandomNumber($min, $max) . "<br />";
        echo "Player 1: " . $player1 . "<br />";
        echo "Player 2: " . $player2 . "<br />";

    }

?>

Thank you in advance for your response.




Aucun commentaire:

Enregistrer un commentaire