PHP猜字游戏有问题吗 猜猜单词游戏
现在我需要使用一个循环来遍历数组,以查看猜测的字母是否在单词中 我还想告诉用户该字母出现了多少次 。。。并用实际字母更新单词的蒙面版本PHP猜字游戏有问题吗 猜猜单词游戏,php,arrays,Php,Arrays,现在我需要使用一个循环来遍历数组,以查看猜测的字母是否在单词中 我还想告诉用户该字母出现了多少次 。。。并用实际字母更新单词的蒙面版本 这里需要注意的是,当您通过隐藏的输入字段跟踪猜测计数时,您并没有跟踪以前的猜测。我建议您使用一个隐藏的输入字段来存储以前的猜测 在页面顶部: // get your random word $random = array_rand($words); $selected_word = $words[$random]; // set up temporary a
这里需要注意的是,当您通过隐藏的输入字段跟踪猜测计数时,您并没有跟踪以前的猜测。我建议您使用一个隐藏的输入字段来存储以前的猜测 在页面顶部:
// get your random word
$random = array_rand($words);
$selected_word = $words[$random];
// set up temporary array to store output letters or *
$output = array();
// split word into each letter
$letters = str_split($selected_word);
foreach($letters as $letter) {
// if the letter was guessed correctly, add it to the output array,
// otherwise add a *
$output[] = ($letter == $guess) ? $letter : '*';
}
// output the results (implode joins array values together)
echo implode($output);
然后再往下走:
$count = 0;
$guesses = array();
if(isset($_POST['guesses']))
$guesses = explode('|', $_POST['guesses']);
if(isset($_POST['submit'])) {
$guess = trim(strtolower($_POST['guess']));
if(!in_array($guess, $guesses))
$guesses[] = $guess;
$count = isset($_POST['count']) ? (int) $_POST['count'] : 0;
$count++;
}
您的
$count
未初始化
$number_of_occurences = substr_count($selected_word, $guess);
// get your random word
$random = array_rand($words);
$selected_word = $words[$random];
// set up temporary array to store output letters or *
$output = array();
// split word into each letter
$letters = str_split($selected_word);
foreach($letters as $letter) {
// if the letter was guessed correctly, add it to the output array,
// otherwise add a *
$output[] = ($letter == $guess) ? $letter : '*';
}
// output the results (implode joins array values together)
echo implode($output);
$count = 0;
$guesses = array();
if(isset($_POST['guesses']))
$guesses = explode('|', $_POST['guesses']);
if(isset($_POST['submit'])) {
$guess = trim(strtolower($_POST['guess']));
if(!in_array($guess, $guesses))
$guesses[] = $guess;
$count = isset($_POST['count']) ? (int) $_POST['count'] : 0;
$count++;
}
<input type="hidden" name="guesses" id="guesses" value="<?=implode('|', $guesses)?>">
foreach($letters as $letter) {
// if the current letter has already been guessed, add it to
// the output array, otherwise add a *
$output[] = in_array($letter, $guesses) ? $letter : '*';
}