像java

时间:2016-05-30 16:56:05

标签: java collections

我正在创建一个游戏,玩家点击gui组件A,如果有垂直和水平相同颜色的组件,请将其删除。要删除的minime数量是2。 所以在这样的网格中: n为空,####

                    A B A C
                    A B C C
                    B C A A
                    C A B B

如果用户点击左上角的C,网格将如下所示:####

                    A B n n
                    A B A n
                    B C A A
                    C A B B

另一个例子:####

A B A C
A B C C
B C A C
A B C C

点击任意4列和3列的C后:####

A B n n
A B n n
B C A n
A B A n

所以我的问题,但可能只是意见,但你如何用Java表示这个数据结构? 是链接列表吗?

1 个答案:

答案 0 :(得分:0)

为此,我将使用二维ArrayList,甚至是用于构成网格的数据类型的二维数组。通过使用二维,导航到相邻的项目是非常简单的:

声明:

public class Foo : MonoBehaviour {
    #region Fields
    private readonly int iterations = 1000000;
    private readonly int threadNum = 1;
    private int iterationsCompleted = 0;
    #endregion

    void Start () {
        Stopwatch stopWatch = new Stopwatch();
        stopWatch.Start();
        Multithread();
        stopWatch.Stop();
        UnityEngine.Debug.Log(stopWatch.Elapsed.TotalMilliseconds);
    }

    private void Multithread() {
        for (int i = 0; i < threadNum; i++) {
            Hash hash = new Hash();
            new Thread(() => {
                while (Interlocked.Increment(ref iterationsCompleted) < iterations) {
                    hash.Get(0, 0, 0);
                }
                UnityEngine.Debug.Log("Finished thread");
            }).Start();
        }
        while (iterationsCompleted < iterations);
    }
}

public class Hash {
    #region Fields
    // FNV parameters can be found at http://www.isthe.com/chongo/tech/comp/fnv/#FNV-param
    private const uint _FNVPrime = 16777619;
    private const uint _FNVOffset = 2166136261;
    private const uint _FNVMask8 = (1<<8)-1;
    #endregion

    #region Class Methods
    private static uint FNV1A(uint[] data) {
        uint hash = _FNVOffset;
        int dataSize = data.Length * sizeof(UInt32);
        byte[] byteArray = new byte[dataSize];
        Buffer.BlockCopy(data, 0, byteArray, 0, dataSize);
        for (int i = 0; i < dataSize; i++) {
            hash = hash ^ byteArray[i];
            hash = hash * _FNVPrime;
        }
        return hash;
    }

    public uint Get(int x, int y, uint seed) {
        uint[] data = new uint[3] { (uint)x, (uint)y, seed };
        //return FNV1A(data);
        return 0;
    }
    #endregion
}

获取相邻单元格(x是原始单元格的x值,y是原始单元格的y值):

T[width][height] grid;
// TODO: fill grid with items