4

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

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

$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++;
        }
    }
}

注意:上面的代码是一个简化版本,其中所有问题都“解决”了,它只是匹配两侧。

运行我的代码后,我得到以下输出:

1 matches with triangle 1
1 matches with triangle 2
2 matches with triangle 1
2 matches with triangle 3
3 matches with triangle 1
3 matches with triangle 4
1 matches with triangle 1
1 matches with triangle 2
2 matches with triangle 1
2 matches with triangle 3
3 matches with triangle 1
3 matches with triangle 4

问题是,$row这只告诉我三角形的边,而不是实际的三角形。所以我的问题是:

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

我希望问题很清楚,但是请随时提出任何问题。

此外,理想情况下,一旦匹配Ta-Sb with Tc-Sd,它不应该匹配Tc-Sd with Ta-Sb。这也可能吗?

4

2 回答 2

1

我发现用对象而不是数组来解决这些类型的问题更容易。太复杂了,无法记住每个数组级别的含义以及它们如何排列。所以我可能会做这样的事情:

<?
// 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]->Sides[1]->state (意味着该三角形的那一侧应该显示答案而不是问题)

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

$Polygon[1]->matches[2] (将持有与多边形 1 的边 2 匹配的三角形的键)

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

在你用值填充它们之后 - 要获得匹配,你只需遍历每个多边形并输出你需要的任何东西。

希望这可以帮助!

于 2013-09-13T18:35:51.047 回答
0

这有帮助吗?

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++;
        }
    }
}
于 2013-09-13T17:26:15.717 回答