我正在制作一个刽子手游戏,如果有人能告诉我一个快速的方法,而不是破坏我的代码,那么我希望从一系列单词中选择单词。我想摆脱(words.list)并用数组替换它
<?php
$letters = array('a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z');
if (empty($_POST)) {
$words = explode("\n", file_get_contents('words.list'));
$right = array_fill_keys($letters, '.');
$wrong = array();
shuffle($words);
$word = strtolower($words[0]);
$rightstr = serialize($right);
$wrongstr = serialize($wrong);
$wordletters = str_split($word);
$show = '';
foreach ($wordletters as $letter) {
$show .= $right[$letter];
}
} else {
$word = $_POST['word'];
$guess = strtolower($_POST['guess']);
$right = unserialize($_POST['rightstr']);
$wrong = unserialize($_POST['wrongstr']);
$wordletters = str_split($word);
if (stristr($word, $guess)) {
$show = '';
$right[$guess] = $guess;
$wordletters = str_split($word);
foreach ($wordletters as $letter) {
$show .= $right[$letter];
}
} else {
$show = '';
$wrong[$guess] = $guess;
if (count($wrong) == 6) {
$show = $word;
} else {
foreach ($wordletters as $letter) {
$show .= $right[$letter];
}
}
}
$rightstr = serialize($right);
$wrongstr = serialize($wrong);
}
?>
Bad Guesses : <?php echo implode(', ', $wrong) ?><br />
<?php echo $show ?><br />
<form method='post'>
<input name='guess' />
<input type='hidden' name='word' value='<?php echo $word ?>' />
<input type='hidden' name='rightstr' value='<?php echo $rightstr ?>' />
<input type='hidden' name='wrongstr' value='<?php echo $wrongstr ?>' />
<input type='submit' value='guess' />
</form>
<a href="hangman.php">Start Over</a>
答案 0 :(得分:0)
这可以通过php的let viewImage = self.view.image()
首先创建itial数组
var_export
存储
$words = explode("\n", file_get_contents('words.list'));
你可以使用
file_put_contents( 'array.php', '<?php return '.var_export( $words, true ).";\n" );
以供将来访问