1. Create a script that presents a word guessing game. Allow users to guess the
ID: 3820319 • Letter: 1
Question
1. Create a script that presents a word guessing game. Allow users to guess the word letter-by-letter by entering a character in a form. Start by assigning a secret word to a variable named $secret. After each guess, print the word using asterisks for each remaining letter but fill in the letters that the user guessed correctly. You need to store the user’s guess in a hidden text field name $hidden_guess. For example, if the word you want users to guess the word programming, and the user has successfully guessed the letters g and m, then store ***g**mm**g in the hidden form field $hidden_guess. Use a single script named GuessingGame.php to display and process the form.
Explanation / Answer
<html>
<head>
<title>Word Game</title>
<link rel="stylesheet" href="teams.css" type="text/css" />
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
</head>
<body>
<?php
$word = 'chegg';
$hidden_guess = '*****';
$letter;
echo "<p>Enter a lower case letter and click the Submit button to guess the word</P>";
if (isset($_POST['Submit'])) {
$letter = $_POST['letter'];
for ($i=0; $i < strlen($word); $i++){
if (($letter == $word[$i]) || ($hidden_guess[$i] == $word[$i]))
{
$hidden_guess.=$word[$i];
}
else
$hidden_guess.='*';
}
}
?>
<form name="Word Guess" action="Guess19373.php" method="POST">
<p>Enter a letter: <input type="text" name="letter" value="<?php echo $letter; ?>" /></p>
<p>The secret word is: <input type="hidden" name="secret" value="<?php echo $hidden_guess; ?>" /></p>
<p><input type="submit" name="Submit" value="Give it a guess" /></p>
</form>
</body>
</html>
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.