麻烦PHP字猜猜游戏

时间:2014-05-12 03:28:45

标签: php arrays

<?php
    if(isset($_POST['submit'])) {
        $guess = $_POST['guess'];
        $count = isset($_POST['count']) ? $_POST['count']:0;
        $count++;
    }
?>
<!DOCTYPE html>
<html>
<head>
<title>Guess the Word Game</title>
</head>
<body>

<div id = "header">
</div>

<div id = "word">
<?php

//create array
$words = array("aardvarks", "determine", "different", "greatness", "miserable");

$random = array_rand($words);
//choose a random word from array
$newWord = $words[$random];

$change = str_split($newWord);
//prints out array
/*foreach($change as $list) {
    echo $list;
}*/
$masked = str_replace($change, '*', $change);
//prints masked array
foreach($masked as $hide) {
echo $hide;
}

?>
</div>

<div id = "guess">

<form name="guessLetter" method="POST" action=""
<p>Please input your guess below</p>
<input type="text" name="guess" id="guess"/><br />

<input type="hidden" name="count" id="count" value="<?php echo $count; ?>" />

<?php

?>

<input type="submit" name="submit" id="submit" value="Submit" />

</form>

<?php

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

    echo "Guess: ".$guess."<br />";
    echo "Count: ".$count;
}

?>

</div>
</body>
</html>

大家好

我对PHP和创建猜词游戏的过程相对较新。

我创建了一个单词数组,然后将它们随机化。一旦选择了一个单词,我就将单词拆分成一个数组。然后,我为屏幕上显示的单词的屏蔽版本创建了另一个数组。

我一直很好,但我现在需要使用一个循环迭代数组来查看猜到的字母是否真的在单词中。我还想告诉用户这封信出现了多少次,并且还用实际的字母更新了这个单词的蒙版版本。

到目前为止,我已经包含了我的代码,并且非常感谢一些帮助,因为我被卡住了!

1 个答案:

答案 0 :(得分:0)

  

我现在需要使用一个循环来遍历数组以查看猜到的字母是否实际上在单词中

$letter_is_in_word = stristr($selected_word, $guess);
// returns either false if not found, or a section of the string 
// where it was found ($letter_is_in_word == true)
  

我还想告诉用户这封信出现的次数

$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 : '*';
}