i making hangman game , want words chosen array of words if tell me quick way whilst not breaking code great. rid of (words.list) , replace array
<?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 $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 $letter) { $show .= $right[$letter]; } } else { $show = ''; $wrong[$guess] = $guess; if (count($wrong) == 6) { $show = $word; } else { foreach ($wordletters $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>
this can done php's var_export
first create itial array
$words = explode("\n", file_get_contents('words.list'));
storing:
file_put_contents( 'array.php', '<?php return '.var_export( $words, true ).";\n" );
the can use
$words=include('array.php');
future access
Comments
Post a Comment