0

1 つの 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];
            }
        }
    }
}

これが私の現在のコードです。正しく動作しないようです。1 つのタイルが点灯している場合にのみ電流を返します。別のタイルが点灯している場合、タイルは返されません。

タイルのコード:

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


    }


}
4

2 に答える 2

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れる値が決して null にならないことを知っていることを意味します。ただし、述語に一致する要素がない場合は、もちろん例外がスローされます。

ただし、例外が見つからないか複数見つかった場合に例外をスローする場合は、次のようにします。

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

複数の要素が述語に一致するかどうかを確認できる唯一の方法であるため、これは常にすべての要素を反復処理します。

于 2013-05-01T08:46:06.547 に答える