예제 #1
0
        public void UpdateAlive()
        {
            var l = board.GetAlive();

            var alive = new List <IDynamicObject>();

            /// for each object alive we calculate the strength of the object.
            /// if the strength is 0 ore below, we set the state as State.Dead and we remove it from the tile.
            /// we return only the non-dead objects on the board.
            foreach (IDynamicObject obj in l)
            {
                obj.AddStrength(-info.ObjectStrengthDecay);
                if (obj.State != State.Dead && obj.Strength > 0)
                {
                    obj.Age++;
                    alive.Add(obj);
                }
                else
                {
                    producerConsumer.Produce(String.Format("Object number {0} died!", obj.Id));
                    obj.SetState(State.Dead);
                    board.ClearDynamicObjectOnTile(obj.X, obj.Y);
                }
            }
            board.SetAlive(alive);
        }
예제 #2
0
        public void Test_GenetareFood()
        {
            //initialize
            IInfo       info = A.Fake <IInfo>();
            IRandomTest rnd  = A.Fake <IRandomTest>();
            IProducerConsumerMessages <string> producerConsumer = A.Fake <IProducerConsumerMessages <string> >();

            int count = 0;

            int[] values = new int[50];
            Tile[,] tiles = new Tile[5, 5];


            A.CallTo(() => info.Length).Returns(5);
            A.CallTo(() => info.Hight).Returns(5);
            A.CallTo(() => info.ID).Returns(0);
            A.CallTo(() => producerConsumer.Produce(A <string> .Ignored));
            A.CallTo(() => rnd.Next(A <int> .Ignored, A <int> .Ignored)).ReturnsNextFromSequence(values);
            A.CallTo(() => info.FoodPerDay).Returns(10);

            for (int i = 0; i < info.Length; i++)
            {
                for (int j = 0; j < info.Hight; j++)
                {
                    if (count < 50)
                    {
                        values[count] = i;
                        count++;
                        values[count] = j;
                        count++;
                    }
                    tiles[i, j] = new Tile();
                }
            }



            IBoard board = new Board();

            board.TestBoard(tiles, info, rnd, producerConsumer);
            board.GenetareFood();

            count = 0;
            for (int i = 0; i < info.Length; i++)
            {
                for (int j = 0; j < info.Hight; j++)
                {
                    if (tiles[i, j].StaticObject != null)
                    {
                        count++;
                    }
                }
            }

            bool ans = count <= 10 && count >= 1;

            Assert.AreEqual(true, ans);
        }
예제 #3
0
        public void Test_GenerateInitialObjects()
        {
            //initialize
            IInfo       info = A.Fake <IInfo>();
            IRandomTest rnd  = A.Fake <IRandomTest>();
            IProducerConsumerMessages <string> producerConsumer = A.Fake <IProducerConsumerMessages <string> >();

            int count = 0;

            int[] values = new int[50];
            Tile[,] tiles = new Tile[5, 5];

            int id = 0;

            A.CallTo(() => info.Length).Returns(5);
            A.CallTo(() => info.Hight).Returns(5);
            A.CallTo(() => info.ObjectStartStrengthHigh).Returns(2);
            A.CallTo(() => info.ObjectStartStrengthLow).Returns(1);
            A.CallTo(() => info.ID).ReturnsLazily(() => { int val = count; count++; return(val); });
            A.CallTo(() => producerConsumer.Produce(A <string> .Ignored));

            for (int i = 0; i < info.Length; i++)
            {
                for (int j = 0; j < info.Hight; j++)
                {
                    if (count < 50)
                    {
                        values[count] = i;
                        count++;
                        values[count] = j;
                        count++;
                    }
                    tiles[i, j] = new Tile();
                }
            }

            A.CallTo(() => rnd.Next(A <int> .Ignored, A <int> .Ignored)).ReturnsNextFromSequence(values);

            IBoard board = new Board();

            board.TestBoard(tiles, info, rnd, producerConsumer);

            board.GenerateInitialObjects(10);
            //assert
            count = 0;
            for (int i = 0; i < info.Length; i++)
            {
                for (int j = 0; j < info.Hight; j++)
                {
                    if (tiles[i, j].DynamicObject != null)
                    {
                        count++;
                    }
                }
            }
            Assert.AreEqual(10, count);
        }
예제 #4
0
        public IDynamicObject TryCreate(int x, int y)
        {
            if (x >= info.Length || x < 0 || y >= info.Hight || y < 0)
            {
                throw new ArgumentException("Trying to move outside of the board");
            }

            ITile tile = tiles[x, y];

            //catch the lock at (x,y) with upgradable mode
            tile.Lock.EnterUpgradeableReadLock();
            try
            {
                // first we check if there is an dynamic object at (x,y) already.
                if (tile.DynamicObject == null)
                {
                    // if there isn't an object we get all the dynamic objects around it
                    var ants = GetNearObjects(x, y, 1);

                    // only if there isn't any objects around (x,y) we create a new dynamic object there.
                    if (ants.Count == 0)
                    {
                        // we upgrade the lock to write mode
                        tile.Lock.EnterWriteLock();
                        try
                        {
                            // creates a new and and return it.
                            IDynamicObject obj = GetNewObject(x, y);
                            producerConsumer.Produce(String.Format("Id: {2} was created at position {0},{1}", x, y, obj.Id));
                            tile.DynamicObject = obj;
                            aliveObjects.Add(obj.Id, obj);
                            return(obj);
                        }
                        finally { tile.Lock.ExitWriteLock(); }
                    }
                    else
                    {
                        return(null);
                    }
                }
                else
                {
                    return(null);
                }
            }
            finally
            {
                tile.Lock.ExitUpgradeableReadLock();
            }
        }