我正在尝试制作一个简单的文字游戏。这个想法是用户单击一个按钮,服务器生成一个字母并将其存储在一个数组中,并且该过程重复十次(我在下面使用的临时解决方法是我一次生成所有 11 个字母)。现在从选择的字母中,用户提出他/她能想到的最长的单词,然后用户提交该单词,计算机检查该单词是否在字典中,如果是,它说“成功”,如果不是,它说“我们的字典中没有出现 xzy 词。” 我不是在寻找所有的检查和安全检查,只是 a) 计算机在用户单击按钮时将字母存储在一个数组中 b) 计算机会检查用户提交的单词是否在预定义的字典中(在单独的 . txt 文件)。
我目前遇到的问题是,每次单击按钮时,我的所有数组都会被新字母覆盖,或者只是空的。我怀疑提交的行为正在重置所有内容,但我似乎无法让程序记住选择了哪些字母,并且不检查我的字典中是否存在单词。如何永久存储东西?代码如下。
<?php
//the array to chose letters from (possible letters)
$PosLetters = [
'a','b','v','g','d',
'đ','e','ž','z','i',
'j','k','l','lj','m',
'n','nj','o','p','r',
's','t','ć','u','f',
'h','c','č','dž','š'
];
//generate 11 letters user can chose from
if (isset($_POST['choose'])) {
for ($i=0; $i < 11; $i++) {
$Rndnumber = mt_rand(0,29);
$Convert = $PosLetters[$Rndnumber];
$Letters[] = $Convert;
}
}
//function that does the check wether user submited word is in my dictionary
function loadFromfile(){
if (isset($_POST['submitword'])) {
//load all the words from file
$vocab = file('http://localhost/igra_slaganje_reci_php/recnik.txt');
//check if user submited word is in the dictionary
if (!empty($_POST['yourword'])) {
$jki = in_array($_POST['yourword'], $vocab);
if ($jki == true) {
echo 'Success';
}else{
echo $_POST['yourword'] . ' doesn't appear in our dictionary.';
}
}
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title></title>
<style type="text/css">
span {
min-width: 50px;
padding: 15px;
margin-right: 15px;
border: 3px solid red;
font-size: 25px;
}
</style>
</head>
<body>
<form method="post">
<input type="submit" name="choose" value="odaberi slovo">
</form>
<div class="letters">
<?php
//put chosen letters each into it's own span
foreach ($Letters as $key => $value) {
echo '<span>' . $value . '</span>';
}
?>
</div>
<div class="subbmitedword">
<form method="post">
<input type="text" name="yourword">
<input type="submit" name="submitword" value="submit word">
</form>
<?php
loadFromfile();
?>
</div>
</body>
</html>