English 中文(简体)
建立基于PHP的专家系统/决定
原标题:Building an expert system/decision based in PHP

我试图解决的问题是,我有16种不同的动物,我需要写一个PHP脚本,该脚本向用户 4 是/ no 问问题,每个问题缩小可用动物的范围,直到最终显示答案。这意味着下一个问题将取决于用户在前一个问题中回答什么。你是否知道我如何做到这一点,而不使用大量如果有的话的语句。

下面是我至今为止所做的,还没有完成,但如果我继续使用如果我最后会用太多的语句的话,必须有一个更好的方法。有人建议我使用阵列内的阵列,但这对我没有帮助。任何帮助都会非常感激。

<?php
session_set_cookie_params(2592000);
session_start();
?>

<html>
<head>
<title>Creature Guessing Game</title>
</head>
<body>
<h1>Creature Guessing Game</h1>
<p> Welcome to the creature guessing game! </p>
<p>Click the button 

below to start or restart the game </p>

<form method="post" action="Creatures.php">
<input type= submit  name= start  value= Start Game  />
</form>
 <?php
//If form not submitted, display form.
if (!isset($_POST[ start ])){
?>
<?php
} //If form is submitted, process input

else{ //Question 1
echo "<p>Does the creature live mainly on the land?</p>";
echo "<form method= post  action= Creatures.php >
<input type= submit  name= yes1  value= Yes  />
<input type= submit  name= no1  value= No  />
</form>";
}
if ($_POST[ yes1 ]){ //Q1 - Yes
echo "<p>Does the creature have wings?</p>";
echo "<form method= post  action= Creatures.php >
<input type= submit  name= yes2  value= Yes  />
<input type= submit  name= no2  value= No  />
</form>";
}
elseif($_POST[ no1 ]){ //Q1 - No
echo "<p>Does the creature live in the water?</p>";
echo "<form method= post  action= Creatures.php >
<input type= submit  name= yes3  value= Yes  />
<input type= submit  name= no3  value= No  />
</form>";
}
if ($_POST[ yes2 ]){ //Q1 - Yes and Q2 - Yes
echo "<p>Can the creature fly?</p>";
echo "<form method= post  action= Creatures.php >
<input type= submit  name= yes4  value= Yes  />
<input type= submit  name= no4  value= No  />
</form>";
}
elseif($_POST[ no2 ]){ //Q1 - Yes and Q2 - No
echo "<p>Is the creature an insect?</p>";
echo "<form method= post  action= Creatures.php >
<input type= submit  name= yes5  value= Yes  />
<input type= submit  name= no5  value= No  />
</form>";
}
if ($_POST[ yes3 ]){ //Q1 - No and Q2 - Yes
echo "<p>Is the creature a reptile?</p>";
echo "<form method= post  action= Creatures.php >
<input type= submit  name= yes6  value= Yes  />
<input type= submit  name= no6  value= No  />
</form>";
}
if ($_POST[ no3 ]){ //Q1 - No and Q2 - No
echo "<p>Does the creature have feathers?</p>";
echo "<form method= post  action= Creatures.php >
<input type= submit  name= yes7  value= Yes  />
<input type= submit  name= no7  value= No  />
</form>";
}
if ($_POST[ yes4 ]){ //Q1 - Yes and Q2 - Yes and Q3 - Yes
echo "<p>Is the creature your thinking of white?</p>";
echo "<form method= post  action= Creatures.php >
<input type= submit  name= yes8  value= Yes  />
<input type= submit  name= no8  value= No  />
</form>";
}
if ($_POST[ yes8 ]){ //Answer 1: Q1 - Yes and Q2 - Yes and Q3 - Yes and Q4 - Yes 
echo "<p>Its a goose!</p>";
}
if ($_POST[ no8 ]){ //Answer 2: Q1 - Yes and Q2 - Yes and Q3 - Yes and Q4 - No
echo "<p>Its a hawk!</p>";
}
?>
</body>
</html>
问题回答

这里不是php 开发者,但您需要做的是 Animal 类或具有一组属性的对象。 这些属性应该是 Boolean (ret/false) 值, 直接映射回您询问的问题。 这些 Animal 对象应该被放入 Collection (a List set ) 。 每一个问题处理后都引用一个函数, 将所有布林属性与用户提供的值不符的动物全部移走。 例如 :

Animal cat = new Animal();  
cat.canWalkOnLand = true;  

userDoesAnimalWalkOnLand = false;  
for(Animal in AnimalCollection)  
{  
    if(Animal.canWalkOnLand != userDoesAnimalWalkOnLand)  
    {  
       AnimalCollection.remove(Animal);
    }  
}  

这应该足以让你开始。 ( Verbose 变量名称可以帮助您理解)

您可以创建一系列的问题, 并且使用一个切换语句和一个小函数, 反复重复同样的废话, 将问题作为一个参数来接受, 它会清除很多代码 。

至于扩大这个游戏的范围, 包括你列出的两只动物, 除了我的建议之外, Woot4Moo 的建议将会得到高度推荐。





相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签