2016-04-09 13 views
3

私はこれについていくつかの助けを得ることを望んでいました。私が抱えている唯一の問題は、このコードが効率的ではないことです。基本的に私はタイルのセットを持っており、それらの周りのタイルの組み合わせに基づいてどのようなタイルをインスタンス化するかを決定する必要があります。下のコードでは、チェックしているタイルの位置がマップ内のキーです(exマップ[右]は右のタイルです、topLeftは左上の対角線に隣接するタイル、top2などはタイルです)その方向に2つのスペース)。当該if-elseif-elseステートメントを縮約して比較を保存する

コード:私は代わりに、それぞれの位置を確認0-2からそれに番号を割り当て、小数にベース3からその番号を変換するだろうと思ったので、私はテーブルを作った

if (map[left].Type == TileType.WALL && map[top].Type == TileType.WALL && map[right].Type == TileType.NOTHING && map[bottom].Type == TileType.NOTHING) 
        { 
         Instantiate(wallEdgeTopLeftInternalCornerTile, t.Position, Quaternion.identity); 
        } 
        else if (map[left].Type == TileType.NOTHING && map[top].Type == TileType.WALL && map[right].Type == TileType.WALL && map[bottom].Type == TileType.NOTHING) 
        { 
         Instantiate(wallEdgeTopRightInternalCornerTile, t.Position, Quaternion.identity); 
        } 
        else if (map[left].Type == TileType.WALL && map[top].Type == TileType.NOTHING && map[right].Type == TileType.NOTHING && map[bottom].Type == TileType.WALL) 
        { 
         Instantiate(wallEdgeBottomLeftInternalCornerTile, t.Position, Quaternion.identity); 
        } 
        else if (map[left].Type == TileType.NOTHING && map[top].Type == TileType.NOTHING && map[right].Type == TileType.WALL && map[bottom].Type == TileType.WALL) 
        { 
         Instantiate(wallEdgeBottomRightInternalCornerTile, t.Position, Quaternion.identity); 
        } 
        else if (map[left].Type == TileType.WALL && map[top].Type == TileType.FLOOR && map[right].Type == TileType.FLOOR && map[bottom].Type == TileType.WALL) 
        { 
         Instantiate(wallEdgeCornerTopRightTile, t.Position, Quaternion.identity); 
        } 
        else if (map[left].Type == TileType.FLOOR && map[top].Type == TileType.FLOOR && map[right].Type == TileType.WALL && map[bottom].Type == TileType.WALL) 
        { 
         Instantiate(wallEdgeCornerTopLeftTile, t.Position, Quaternion.identity); 
        } 

        else if (map[left].Type == TileType.FLOOR && map[top].Type == TileType.WALL && map[right].Type == TileType.WALL && map[bottom].Type == TileType.WALL && map[topRight].Type == TileType.NOTHING) 
        { 
         Instantiate(wallEdgeCornerBottomLeftTile, t.Position, Quaternion.identity); 
        } 
        else if (map[left].Type == TileType.WALL && map[top].Type == TileType.WALL && map[right].Type == TileType.FLOOR && map[bottom].Type == TileType.WALL && map[topLeft].Type == TileType.NOTHING) 
        { 
         Instantiate(wallEdgeCornerBottomRightTile, t.Position, Quaternion.identity); 
        } 

        else if ((map[left].Type == TileType.WALL && map[top].Type == TileType.WALL && map[right].Type == TileType.WALL && map[bottom].Type == TileType.WALL && map[top2].Type == TileType.NOTHING && map[left2].Type == TileType.NOTHING) || (map[left].Type == TileType.FLOOR && map[top].Type == TileType.WALL && map[right].Type == TileType.WALL && map[bottom].Type == TileType.WALL && map[top2].Type == TileType.WALL && map[left2].Type == TileType.FLOOR && map[bottom2].Type == TileType.WALL)) 
        { 
         Instantiate(wallTopLeftTile, t.Position, Quaternion.identity); 
        } 
        else if ((map[left].Type == TileType.WALL && map[top].Type == TileType.WALL && map[right].Type == TileType.WALL && map[bottom].Type == TileType.WALL && map[top2].Type == TileType.NOTHING && map[right2].Type == TileType.NOTHING) || (map[right].Type == TileType.FLOOR && map[top].Type == TileType.WALL && map[left].Type == TileType.WALL && map[bottom].Type == TileType.WALL && map[top2].Type == TileType.WALL && map[right2].Type == TileType.FLOOR && map[bottom2].Type == TileType.WALL)) 
        { 
         Instantiate(wallTopRightTile, t.Position, Quaternion.identity); 
        } 

        else if ((map[left].Type == TileType.WALL && map[top].Type == TileType.WALL && map[right].Type == TileType.WALL && map[bottom].Type == TileType.FLOOR && map[top2].Type == TileType.WALL && map[left2].Type == TileType.NOTHING) || (map[left].Type == TileType.FLOOR && map[top].Type == TileType.WALL && map[right].Type == TileType.WALL && map[bottom].Type == TileType.FLOOR && map[top2].Type == TileType.WALL && map[left2].Type == TileType.FLOOR && map[bottom2].Type == TileType.FLOOR)) 
        { 
         Instantiate(wallBottomLeftTile, t.Position, Quaternion.identity); 
        } 
        else if ((map[left].Type == TileType.WALL && map[top].Type == TileType.WALL && map[right].Type == TileType.WALL && map[bottom].Type == TileType.FLOOR && map[top2].Type == TileType.WALL && map[right2].Type == TileType.NOTHING) || (map[right].Type == TileType.FLOOR && map[top].Type == TileType.WALL && map[left].Type == TileType.WALL && map[bottom].Type == TileType.FLOOR && map[top2].Type == TileType.WALL && map[right2].Type == TileType.FLOOR && map[bottom2].Type == TileType.FLOOR)) 
        { 
         Instantiate(wallBottomRightTile, t.Position, Quaternion.identity); 
        } 
        else if ((map[left].Type == TileType.WALL && map[top].Type == TileType.WALL && map[right].Type == TileType.WALL && map[bottom].Type == TileType.WALL && map[left2].Type == TileType.NOTHING && map[bottom2].Type == TileType.FLOOR) || (map[left].Type == TileType.FLOOR && map[top].Type == TileType.WALL && map[right].Type == TileType.WALL && map[bottom].Type == TileType.WALL && map[left2].Type == TileType.FLOOR && map[bottom2].Type == TileType.FLOOR)) 
        { 
         Instantiate(wallMidLeftTile, t.Position, Quaternion.identity); 
        } 
        else if ((map[left].Type == TileType.WALL && map[top].Type == TileType.WALL && map[right].Type == TileType.WALL && map[bottom].Type == TileType.WALL && map[right2].Type == TileType.NOTHING && map[bottom2].Type == TileType.FLOOR) || (map[left].Type == TileType.WALL && map[top].Type == TileType.WALL && map[right].Type == TileType.FLOOR && map[bottom].Type == TileType.WALL && map[right2].Type == TileType.FLOOR && map[bottom2].Type == TileType.FLOOR)) 
        { 
         Instantiate(wallMidRightTile, t.Position, Quaternion.identity); 
        } 
        else if ((map[left].Type == TileType.WALL && map[top].Type == TileType.WALL && map[right].Type == TileType.WALL && map[bottom].Type == TileType.WALL && map[top2].Type == TileType.NOTHING && map[bottom2].Type == TileType.WALL)) 
        { 
         Instantiate(wallTopTile, t.Position, Quaternion.identity); 
        } 
        else if ((map[left].Type == TileType.WALL && map[top].Type == TileType.WALL && map[right].Type == TileType.WALL && map[bottom].Type == TileType.FLOOR && map[top2].Type == TileType.WALL && map[bottom2].Type == TileType.FLOOR)) 
        { 
         Instantiate(wallBottomTile, t.Position, Quaternion.identity); 
        } 
        else if (map[top].Type == TileType.NOTHING && map[bottom].Type == TileType.WALL) 
        { 
         Instantiate(wallEdgeBottomTile, t.Position, Quaternion.identity); 
        } 
        else if ((map[left].Type == TileType.NOTHING && map[right].Type == TileType.FLOOR) || (map[left].Type == TileType.NOTHING && map[right].Type == TileType.WALL && map[top].Type != TileType.NOTHING)) 
        { 
         Instantiate(wallEdgeRightTile, t.Position, Quaternion.identity); 
        } 
        else if ((map[left].Type == TileType.FLOOR && map[right].Type == TileType.NOTHING) || (map[left].Type == TileType.WALL && map[right].Type == TileType.NOTHING && map[top].Type != TileType.NOTHING)) 
        { 
         Instantiate(wallEdgeLeftTile, t.Position, Quaternion.identity); 
        } 
        else if (map[bottom].Type == TileType.NOTHING && map[top].Type == TileType.FLOOR) 
        { 
         Instantiate(wallEdgeTopTile, t.Position, Quaternion.identity); 
        } 
        else if ((map[left].Type == TileType.WALL && map[top].Type == TileType.WALL && map[right].Type == TileType.WALL && map[bottom].Type == TileType.WALL && map[top2].Type == TileType.WALL && map[bottom2].Type == TileType.FLOOR)) 
        { 
         Instantiate(wallMiddleTile, t.Position, Quaternion.identity); 
        } 
        else 
        { 
         // Should never get here, so if a white Tile is seen, something went wrong! 
         Instantiate(whiteTile, t.Position, Quaternion.identity); 
        } 

、その結果に対してswitch文を使用して、どのタイルをインスタンス化するかを決定します。数字コードは右端にあります。 しかし、それほど多くの組み合わせがありますが、私はより良い、またはより簡単な方法でなければならないように感じています。すべてのアイデアは高く評価されています!

表: Table of conditions

+2

Switchステートメントを使用するhttp://stackoverflow.com/questions/767821/is-else-if-faster-than-switch-case – LumbusterTick

+0

私はチェックする必要があるため、switch文が私の問題を解決するとは思わないただ1つの特定の値ではなく、値の組み合わせの場合。 – user3754585

+1

"インスタンス化([値]、t.Position、Quaternion.identity);" [value]の値を変数に設定すると、このように "Instantiate()"という行が1つしかありません。また、オペランドを繰り返しています(例:map [left] .Type == TileType.WALL)。各等価比較子が1回だけ出現する方法を見つけようとする。 –

答えて

0

は、単にcaparison操作の数を減らすために、私はツリー型の方法であなたと条件を手配お勧めします。ここに簡略化した例があります。

注:私は可読性のために単一の&を使用しています。論理ANDを意味します。

if(a&b&c) {functionW(a);return;} //1 comparison, 2 ANDops 
else if(a&b&!c) {functionX(a);return;}//2 comparison, 4 ANDops 
else if(a&!b&!c) {functionX(b);return;}//3 comparison, 6 ANDops 
else if(a&!b&c) {functionX(c);return;}//4 comparison, 8 ANDops 
else if(!&a&b&c) {functionY(b);return}//5 comparison, 10 ANDops 
else if(!a&!b&c){functionZ(c);return}//6 comparison, 12 ANDops 
else if(!b&!e){functionZ(c);return;}//7 comparison, 13 ANDops 

ツリー形式:で始まる

我々がチェックされている各要素のブランチを取るので、ための最初のチェック:

//out here,first, go all the statements, where it doesn't mater what value a has. We haveonly one condition in our example, but if we had many, we could create a separate "tree" out here. 
if(!b&!e){functionZ(c);return;}//1 comparison, 1 ANDops 
//now we start a branch based on a 
if(a) 
{//in here go all the if statements where a is true 
    //now we create onther tree branch based on b 
    if(b) 
    { 
     if(!c){ functionW(a); return;}// total 4 comparisons to get here. 1 ANDops 
    } 
    else 
    {//all option where b is false 
     if(c) {functionX(c); return;}// total 4 comparisons to get here, 1 ANDops 
     else {functionX(b); return;}// total 4 comparisons to get here, 1 ANDops 
    } 
} 
else 
{//in here go all the if statements where a is false (there are only two, so no need for a branch) 
    if(b&c) {functionY(b);return}// total 3 comparisons to get here. 2 ANDops 
    else if(!b&c){functionZ(c);return}// total 3 comparisons to get here, 3 ANDops 
} 

明らかにあなたは大きな利点を得ることはありません唯一の7つの異なる条件がありますが、このような改善は、あなたが始める条件の量とともに増加します。

関連する問題