2013-05-01 121 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

相关问题