2013-05-01 106 views
0

我需要獲取具有一個bool true的2D數組的元素。 這是唯一一個擁有此布爾值的人。如何獲得具有特定值的數組的元素

public void getCurrentTile() 
{ 
    for (int x = 0; x < 75; x++) 
    { 
     for (int y = 0; y < 75; y++) 
     { 
      if (((Tile)grid[y, x]).lit) 
      { 
       current = (Tile)grid[y, x]; 
      } 
     } 
    } 
} 

這是我現在的代碼。它似乎無法正常工作。它只在一個瓦片點亮時返回當前值。當另一個瓦片點亮時,它不會返回瓦片。

典瓷磚:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Drawing; 
using Microsoft.Xna.Framework; 
using Microsoft.Xna.Framework.Audio; 
using Microsoft.Xna.Framework.Content; 
using Microsoft.Xna.Framework.GamerServices; 
using Microsoft.Xna.Framework.Graphics; 
using Microsoft.Xna.Framework.Input; 
using Microsoft.Xna.Framework.Media; 


namespace SimMedieval 
{ 
    public class Tile 
    { 
     public int size; 
     public String path; 
     public Texture2D texture; 
     public bool lit = false; 
     public bool lightable; 
     MouseState mouseState; 
     public System.Drawing.Color color; 
     public int posX; 
     public int posY; 

     public Tile(int s, String texturepath, bool light) 
     { 
      size = s; 
      path = texturepath; 
      lightable = light; 

     } 

     public void load(Game game, Game1 game1, System.Drawing.Color colour) 
     { 
      color = colour; 
      texture = game.Content.Load<Texture2D>(path); 
      game1.tiles.Add(this); 
     } 
     public void render(SpriteBatch sprites,int x, int y, Camera cam, Tile[,] grid) 
     { 
      mouseState = Mouse.GetState(); 
      float camPosX = (x * size) + cam.posX; 
      float camPosY = (y * size) + cam.posY; 
      if (-20 < camPosX && camPosX < 960 && -20 < camPosY && camPosY < 640) 
      { 
       if (new Microsoft.Xna.Framework.Rectangle(x, y, 1, 1).Contains((mouseState.X/size) - ((int)Math.Round(cam.posX)/size), (mouseState.Y/size) - ((int)Math.Round(cam.posY)/size))) 
       { 
        lit = true; 
        grid[y, x] = this; 
        sprites.Draw(texture, new Vector2((x * size) + cam.posX, (y * size) + cam.posY), Microsoft.Xna.Framework.Color.Coral); 
       } 
       else 
       { 
        lit = false; 
        grid[y, x] = this; 
        sprites.Draw(texture, new Vector2((x * size) + cam.posX, (y * size) + cam.posY), Microsoft.Xna.Framework.Color.White); 
       } 
      } 
     } 

     public void spawn(int x, int y, Tile[,] grid) 
     { 
      grid[y, x] = this; 
      posX = x; 
      posY = y; 
     } 


    } 


} 
+1

我不明白。一方面你說'這是唯一一個具有這個布爾值的',但是你會說'當另一個瓦片點亮時'。那麼可以有多個瓷磚嗎? – 2013-05-01 08:47:31

+0

是的,當你選擇一個新的電流或者類似的東西時,你可能會忘記關閉一個磁貼。或者也許真的沒有點亮的瓷磚。檢查這兩個答案更好的方法(總結一下 - 實際上你應該返回找到的tile,如果沒有發現,則返回null,並處理來自調用者的結果,而不是從函數內分配) – SimpleVar 2013-05-01 08:50:45

+0

讓我來解釋一下。總是有一塊瓦片點亮。從來沒有一個。但是我的代碼只是在瓦片時返回一個瓦片,讓我們說,#20點亮。但是,當#13號燈亮起,#20號燈沒有返回時。 – Hobbit9797 2013-05-01 09:07:01

回答

2

這應該這樣做,我想:

public Tile getCurrentTile() 
{ 
    for (int x = 0; x < 75; x++) 
     for (int y = 0; y < 75; y++) 
      if (((Tile)grid[y, x]).lit) 
       return (Tile)grid[y, x]; 

    return null; // Return null if not found. 
} 

而不是設置current搜索功能裏面,它更清晰,使其返回找到的項目,或如果找不到,則爲null

然後調用代碼看起來會像這樣:

var item = getCurrentTile(); 

if (item != null) 
    current = item; 
else 
    // Code to handle no current tile being found. 

或者(和更簡單),你可以只使用Linq:

Tile item = grid.Cast<Tile>().FirstOrDefault(cell => cell.lit); 

if (item != null) 
    current = item; 
else 
    // Code to handle no current tile being found. 

這是有效的,因爲它會盡快停止迭代它找到第一個項目。

如果你想,如果沒有在所有發現拋出一個異常,這樣做:

Tile item = grid.Cast<Tile>().First(cell => cell.lit); 

這也將盡快爲項目找到停止迭代。這是我使用的,因爲它是最高效的,它也表達了其中一個必須與謂詞匹配的想法。這意味着你知道返回的item永遠不會爲空 - 但是,如果的元素與謂詞匹配,它當然會拋出異常。

但是,如果你想拋出一個異常,如果沒有或一個以上的被發現,你可以這樣做:

Tile item = grid.Cast<Tile>().Single(cell => cell.lit); 

這將始終貫穿所有元素迭代,因爲這是唯一的方法它可以檢查是否有多個元素與謂詞匹配。

0

您可以使用Linq來查找您想要的項目。但首先你需要將二維數組轉換爲IEnumerable<T>但是如何?如果您的grid包含Tile的對象,請嘗試下面簡單的一行解決方案。

Tile current= (from Tile item in array 
        where item!=null && item.lit select item).FirstOrDefault(); 
+0

如何從當前的Tile轉換爲Tile? – Hobbit9797 2013-05-01 09:10:44

+0

@ Hobbit9797可能有許多Tiles點亮。 – Damith 2013-05-01 09:30:30

相關問題