用PHP匹配卡片

用PHP匹配卡片,php,math,match,Php,Math,Match,有一个相当流行的程序(我忘了它的名字),它生成三角形,每边都有一个问题或答案,每个三角形组合在一起,使得一个三角形上的答案与另一个三角形上的问题匹配,当正确组合在一起时,它会生成一个更大的形状(通常是正六边形) 我正在尝试编写一个脚本,其中$t是一个包含卡片的2D数组: $t = array(); // $t['1'] represents the 'center' triangle in this basic example $t['1'] = array( '1', // One

有一个相当流行的程序(我忘了它的名字),它生成三角形,每边都有一个问题或答案,每个三角形组合在一起,使得一个三角形上的答案与另一个三角形上的问题匹配,当正确组合在一起时,它会生成一个更大的形状(通常是正六边形)

我正在尝试编写一个脚本,其中
$t
是一个包含卡片的2D数组:

$t = array();

// $t['1'] represents the 'center' triangle in this basic example
$t['1'] = array(
    '1', // One side of T1, which is an answer
    '3-1', // Another side, this is a question
    '2+1' // Final side, another question
);

// These cards go around the outside of the center card
$t['2'] = array(
    '2-1' // This is a question on one side of T2, the other sides are blank
);
$t['3'] = array(
    '2' // This is an answer on one side of T3, the other sides are blank
);
$t['4'] = array(
    '3' // This is an answer on one side of T4, the other sides are blank
);
现在需要它做的是,比如说,“T1-S1与T2匹配,T1-S2与T3匹配,T1-S3与T4匹配”。我已经尝试过了,目前为止我所做的如下:

foreach ($t as $array) {
    foreach ($array as $row) {
        $i = 0;
        while ($i <= 4) {
            if(in_array($row, $t[$i])){
                echo $row . ' matches with triangle ' . $i . '<br />';
            }
            $i++;
        }
    }
}
问题是,
$row
只告诉我三角形的边,而不是实际的三角形。所以我的问题是:

如何使脚本工作,以便它输出“Ta Sb与Tc Sd匹配”,其中a是三角形,b是边,c是它匹配的三角形,d是它匹配的边,假设在每个数组中边的值是有序的?

我希望问题是明确的,但请随时提出任何问题

此外,理想情况下,一旦将
Ta Sb与Tc Sd
匹配,就不应将
Tc Sd与Ta Sb
匹配。这也可能吗?

这有帮助吗

foreach ($t as $ti => $array) {
    foreach ($array as $ri => $row) {
        $i = 0;
        while ($i <= 4) {
            if(in_array($row, $t[$i])){
                echo $row.' '.$ti.' '.$ri.' matches with triangle ' . $i . '<br />';
            }
            $i++;
        }
    }
}
foreach($t作为$ti=>$array){
foreach($ri=>$row的数组){
$i=0;

虽然($i我发现用对象而不是数组处理这些类型的问题更容易。太复杂了,记不清每个数组级别的含义以及它们是如何排列的。所以我可能会这样做:

<?
// I say Polygon instead of triangle because ideally the logic should scale for squares, octagons, anything! But start with triangles of course.
class Polygon{ 
  var $Sides = array(); // Side objects - there should be 3 of them for a triangle
  var $matches = array(); // holds the ids of the matching polygonn - keys line up with $Sides

  function __construct(){
    $Sides[0] = new Side();
    $Sides[1] = new Side();
    $Sides[2] = new Side();
  }

}

class Side{
  var $Question; // Question object
  var $state; // 'q' or 'a' - does this side show the question or answer?

  function __construct(){
    $Question = new Question();
  }

}

class Question{
  var $id; // database id of the question
  var $question;
  var $answer;
}
?>

要填充:

<?php

$Triangle[0]=new Polygon();
$Triangle[0]->Side[0]->Question->id=1;
$Triangle[0]->Side[0]->Question->question='Yo momma serves more requests than what?';
$Triangle[0]->Side[0]->Question->answer='HTTP';
$Triangle[0]->Side[0]->state='q'; // This side shows the question
$Triangle[0]->matches[0]= 4; // Side 0 of this triangle matches a side of triangle 4

// write a loop that does this for all triangles using whatever your logic is for matching them up

?>

现在,您可以很容易地知道您正在处理的三角形、边、问题或匹配,例如:

$Polygon[2]->边[1]->状态(表示三角形的边应显示答案,而不是问题)

$Polygon[0]->Sides[3]->Question->id(将保存问题的id)

$Polygon[1]->匹配[2](将保留与多边形1的第2边匹配的三角形的关键点)

如果您不习惯对象,这可能会感觉像一次飞跃,但这是一种非常简单的方法,您可以将它们视为美化的数组,暂时忘记对象可以做的所有其他事情

在用值填充它们之后-要获得匹配,只需在每个多边形中循环并输出所需的内容


希望这有帮助!

什么是
$ri
?我不太确定你做了什么。你能试着解释一下,如果可能的话,把它放在
Ta Sb匹配Tc Sd
的格式中吗?你能显示字符串
Ta Sb
Tc Sd
来自哪里吗?好的,而不是你示例中的第6行输出
1 10匹配吗对于三角形2,它应该输出T1-S1与T2-S1匹配的
T1-S1是否被称为?Yeap,正是它。但是,这样看来,您必须说明它与哪个三角形匹配(
$triangle[0]->匹配[0]=4;
)我不知道你是否必须这样做,但这似乎是一件很方便的事情来跟踪。如果你省略了匹配项,你可能只需要使用问题id来查看双方是否匹配。这只是一种组织你正在跟踪的内容的方法,这样就很容易处理-如何使用这些对象取决于你。哦,我理解。我知道您应该建议将此作为一个整体解决方案,而不仅仅是一种帮助。它需要能够做的一件事是计算一个数学字符串(
1+2
)。有没有比循环使用
更好的方法来尝试eval('$ans=string)
?您不应该使用eval().我在这里找到了有关您应该做什么的信息:
<?php

$Triangle[0]=new Polygon();
$Triangle[0]->Side[0]->Question->id=1;
$Triangle[0]->Side[0]->Question->question='Yo momma serves more requests than what?';
$Triangle[0]->Side[0]->Question->answer='HTTP';
$Triangle[0]->Side[0]->state='q'; // This side shows the question
$Triangle[0]->matches[0]= 4; // Side 0 of this triangle matches a side of triangle 4

// write a loop that does this for all triangles using whatever your logic is for matching them up

?>