2016-04-09 17 views
3

Bu konuda biraz yardım almayı umuyordum. Tek gerçek sorun şu ki bu kod olabildiğince verimli değil. Temelde bir dizi fayans var ve etrafındaki fayansların kombinasyonuna dayanarak ne tür fayansların oluşturulacağına karar vermem gerekiyor. Aşağıdaki kodda, kontrol ettiğim karonun konumu benim haritamdaki anahtardır (ex map [sağ] sağdaki kiremittir, topLeft üst köşeye ve üst2'ye çapraz olarak bitişik karodur. Vb.) bu yönde iki boşluk. Söz konusuBir if-elseif-else ifadesiyle yapılan karşılaştırmaları kaydetmek için

kod: Ben bunun yerine, her konumunu kontrol 0-2 it a numara atamak, ondalık için tabandan 3 bu sayıyı dönüştürmek düşündüm çünkü bir tablo yapmıştı

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); 
        } 

, Daha sonra, hangi çini oluşturulacağını belirlemek için sonuç üzerinde bir anahtar deyimi kullanın. Sayı kodları çok doğru. Ama çok fazla kombinasyon var, daha iyi ya da daha kolay bir yol olmalı diye düşünüyorum. Herhangi bir fikir takdir edilmektedir!

Tablo: Table of conditions

+2

Bir Anahtar deyimi kullanın http://stackoverflow.com/questions/767821/is-else-if-faster-than-switch-case – LumbusterTick

+0

Bir anahtar ifadesinin sorunumu çözeceğimi düşünmüyorum çünkü denetlemem gerekiyor sadece belirli bir değerden ziyade değer kombinasyonları için. – user3754585

+1

Taşı "Instantiate ([value], t.Position, Quaternion.identity)"; kontrol yapısının sonuna. [value] değerini bir değişkene ayarlayın, bu şekilde sadece "Instantiate()" ile bir satırınız olur. Ayrıca, işlenenleri tekrarlıyorsunuz (ör. Harita [sol] .Type == TileType.WALL). Her bir eşitlik karşılaştırıcısının sadece bir kez gerçekleştiği yeri bulmaya çalışın. –

cevap

0

basitçe caparison operasyonların sayısını azaltmak için, ben bir ağaç tipi moda sizin VE çıkarabilseniz düzenlemek öneririm. burada basitleştirilmiş bir örnek.

not: Ben okunabilirlik için tek & kullanıyorum-mantıksal AND demek.

ile başlayan:

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 

Ağaç biçimi: Bir yönelik bir kontrol etiğimizde her element için dalı, bu nedenle ilk kontrolü atın: Açıkçası

//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 
} 

Eğer çok büyük bir avantaj alamadım Sadece 7 farklı koşulla, ancak bu gelişme, başladığınız koşulların miktarı ile büyür.