There is quite a popular program (I forget the name) which generates triangles, where on each side there is a question or answer, and each triangle fits together such that the answer on one triangle matches the question on another, and when put together correctly it creates a larger shape (usually a regular hexagon).
I am trying to write a script, where $t
is a 2D array containing the cards:
$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
);
What now need it to do, is say, for example, "T1-S1 matches with T2, T1-S2 matches with T3, T1-S3 matches with T4". I have tried, and what I have so far is below:
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++;
}
}
}
Note: The code above is for a simplified version where all the questions were 'solved', and it was just matching the two sides.
After running my code, I get this output:
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
The problem is, that $row
only tells me the side of a triangle, not the actual triangle. So my question is this:
How do I make my script work, such that it outputs "Ta-Sb matches with Tc-Sd" where a is the triangle, b is the side, and c is the triangle it matches with, and d is the side it matches with, assuming that in each array the values for the sides are in order?
I hope that the question is clear, however feel free to ask any questions.
Also, ideally once it has matched Ta-Sb with Tc-Sd
, it should not match Tc-Sd with Ta-Sb
. Is that possible too?