Exemplo n.º 1
0
    public Future <Dictionary <Vector2, int> > ComputeNextState(Dictionary <Vector2, int> lastState, Vector2 bounds)
    {
        Future <Dictionary <Vector2, int> > future = new Future <Dictionary <Vector2, int> >();

        future.Process(() =>
        {
            Dictionary <Vector2, int> states = new Dictionary <Vector2, int>();

            foreach (Vector2 key in lastState.Keys)
            {
                int value = 0;
                lastState.TryGetValue(key, out value);
                int[] count = new int[Enum.GetNames(typeof(States)).Length];

                float[] xs = new float[]
                {
                    MathHelper.mod(key.x + 1, bounds.x),
                    MathHelper.mod(key.x + 1, bounds.x),
                    key.x,
                    key.x,
                    MathHelper.mod(key.x + 1, bounds.x),
                    MathHelper.mod(key.x - 1, bounds.x),
                    MathHelper.mod(key.x - 1, bounds.x),
                    MathHelper.mod(key.x - 1, bounds.x)
                };

                float[] ys = new float[]
                {
                    key.y,
                    MathHelper.mod(key.y + 1, bounds.y),
                    MathHelper.mod(key.y + 1, bounds.y),
                    MathHelper.mod(key.y - 1, bounds.y),
                    MathHelper.mod(key.y - 1, bounds.y),
                    key.y,
                    MathHelper.mod(key.y + 1, bounds.y),
                    MathHelper.mod(key.y - 1, bounds.y)
                };

                for (int i = 0; i < 8; i++)
                {
                    Vector2 v = new Vector2(xs[i], ys[i]);
                    int a     = (int)States.Dead;

                    bool success = lastState.TryGetValue(v, out a);

                    if (success)
                    {
                        count[a] += 1;
                    }
                }

                if (value == (int)States.Alive)
                {
                    value = Death.Process(count, value);
                }
                else
                {
                    value = Birth.Process(count, value);
                }

                states.Add(key, value);
            }
            return(states);
        });

        return(future);
    }