Hey Y’all.
I am having trouble trying to figure out how to make a Guessing Number Game. Here are some of the requirements I am needing to do:
Inside the PHP code block at the top of the script file, define a function. The function should
compare the two parameters and return a number (-1, 0, or 1) that indicates whether the
first parameter is smaller than, larger than, or the same as the second parameter. Define
the function using following settings:
a. Name: compare_integers
b. Parameters: int1 and int2
c. Return values:
I. -1 if int1 is smaller than int2
II. 0 if int1 is the same as int2
III. 1 if int1 is larger than int2 (I have completed this part)
-Continue the script by creating a PHP variable named random. Set random using an IF …
ELSE statement as follows:
a. If a post data associated with the hidden form field named random exists,
retrieve the data, sanitize it, and then assign it to random.
b. If the post data does not exist, assign random a random number between 1
and 20.
- Check whether a post data associated with a form field named guess exists. Hint: you may
use filter_has_var function. This data is the player’s guess. If the data exists, perform
followings:
a. Retrieve the data, sanitize it, and then store it in a PHP variable named guess.
b. Check to see if guess is in the range between 1 and 20.
c. If guess is in the range, call the compare_integers function and pass guess
and random to the function. Based on the return value, assign a string to a
variable named message.
i. If the return value is -1, set message to “Your guess ‘guess’ was too
low.".
ii. If the return value is 1, set message to “Your guess ‘guess’ was too
high.".
iii. If the return value is 0, set message to “Congratulations! You
guessed the hidden number!!!"
d. If guess is not in the range, set message to “"Invalid guess. Please enter a
number between 1 and 20.” - Modify the tag for the hidden form field random so its value is the data stored in
the PHP variable random.
Here is the code I have so far (the require attributes are from the same project, first part of it):
[php]
<?php $title = "Guessing the number game"; require ('includes/header.php'); function compare_integers($int1, $int2) { if ($int1 < $int2) { return -1; } if ($int1 == $int2) { return 0; } if ($int1 > $int2) { return 1; } } //$random = rand(1, 20); if (!filter_has_var(INPUT_POST, "random")) { $random = filter_input(INPUT_POST, "random", FILTER_SANITIZE_NUMBER_INT); }else { $random = rand(1, 20); } /*if (!filter_has_var(INPUT_POST, "guess")){ (!filter_input(INPUT_POST, "guess", FILTER_SANITIZE_NUMBER_INT); } else { if ('guess' == $random){ } }*/ ?> <?php echo $title ?><body>
<!-- Main body of content -->
<div id="mainbody">
<!-- page specific content starts -->
<h2>Guessing the Number</h2>
<p>Enter a number between 1 and 20, then press the Guess button.</p>
<form action="game.php" method="post">
<p>
<input type="number" name="guess" required />
<input type="submit" value=" Guess " />
</p>
<!-- this hidden field is used to store the system generated random number -->
<input type="hidden" name="random" value="<?php echo $random ?>" />
</form>
<!-- page specific content ends -->
</div>
<?php
require ('includes/footer.php')
?>
</body>
[/php]
I am not needing the answer, but I want to see if I am on the right track and would like some tips, advice, and/or examples on how to achieve the random variable/if… else… part, the guess variable “filter_has_var” part, and the red text part. I am just really lost after trying to figure it out for 2-3 hours and looking at my notes and examples online (it isn’t clicking). Thanks!