1. Create a script that presents a word guessing game. (PHP / HTML )Allow users
ID: 3820317 • Letter: 1
Question
1. Create a script that presents a word guessing game. (PHP / HTML )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
function guessWord($toCheck) {
global $solution; /
$goodpos = array();
$badpos = array();
$newToGuess = array();
$newSolution = array();
for ($k = 0, $m = strlen($toGuess); $k < $j; $k++)
{
if ($toGuess[$k] == $solution[$k]) {
$goodpos[] = $k;
$newSolution[$k] = "*";
} else {
$newToGuess[] = $toGuess[$k];
$newSolution[$k] = $solution[$k];
}
}
for ($k = 0, $m = count($newSolution); $i <= $j; $k++) {
if (!(in_array($newToGuess[$k], $newSolution)))
{
$badpos[] = $k;
$newSolution[$k] = "*";
}
}
for ($k = 0, $m = strlen($toGuess), $k = 0; $k< $j; $k++)
{
if (in_array($k,$goodpos))
{
$colored .= "<span class='pink'>";
$colored .= $toCheck[$k];
$colored .= "</span>";
}
else if (in_array($i,$badpos))
{
$colored .= "<span class='red'>";
$colored .= $toCheck[$k];
$colored .= "</span>";
} else
{
$colored .= $toCheck[$k];
}
}
$output = '<div id="feedbackHash">';
$output .= '<h2>Solution was : ' . $solution . '</h2>';
$output .= '<h2>Color corrected: ' . $colored . '</h2>';
$output .= 'Correct letters in the right position : ' . count($goodpos) . '<br>';
$output .= 'Correct letters in the wrong position : ' . count($badpos) . '<br>';
$output .= '</div>';
return $output;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.