4

三角形を生成する非常に人気のあるプログラム (名前は忘れました) があります。このプログラムでは、両側に質問または回答があり、各三角形は 1 つの三角形の回答が別の三角形の質問と一致するように適合し、正しく組み合わせると一致します。より大きな形状 (通常は正六角形) を作成します。

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

注: 上記のコードは、すべての質問が「解決」された単純化されたバージョン用であり、2 つの側面を一致させるだけでした。

コードを実行すると、次の出力が得られます。

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 に答える