예제 #1
0
        private void TryCreateObject()
        {
            // get a random position on the board
            int x, y;

            switch (randomOption)
            {
            case RandomOption.RealTime:
                x = MyRandom.Next(0, info.Length);
                y = MyRandom.Next(0, info.Hight);
                break;

            default:
                x = rnd.Next(0, info.Length);
                y = rnd.Next(0, info.Hight);
                break;
            }

            //try to create an object at position (x,y)
            IDynamicObject obj = board.TryCreate(x, y);

            if (obj != null)
            {
                /// if an object was created, add a new enrty in the dictinory and create
                /// a new task for the dynamic object
                AutoResetEvent are = new AutoResetEvent(false);
                ares.TryAdd(obj.Id, are);
                Task.Factory.StartNew(() => DynamicObjectAction(obj), TaskCreationOptions.LongRunning);
                are.Set();
            }
        }
예제 #2
0
        /// <summary>
        /// Try to move the object on the board
        /// See <see cref="IBoardFunctions"/>
        /// <seealso cref="MyRandom"/>
        /// </summary>
        /// <param name="obj">The object trying to move</param>
        public virtual void TryToMove()
        {
            int x, y;

            do
            {
                if (rnd == null)
                {
                    x = MyRandom.Next(Math.Max(0, X - Agility), Math.Min(info.Length, X + 1 + Agility));
                    y = MyRandom.Next(Math.Max(0, Y - Agility), Math.Min(info.Hight, Y + 1 + Agility));
                }
                else
                {
                    x = rnd.Next(Math.Max(0, X - Agility), Math.Min(info.Hight, X + 1 + Agility));
                    y = rnd.Next(Math.Max(0, Y - Agility), Math.Min(info.Hight, Y + 1 + Agility));
                }
            } while (x == X && y == Y);

            int localX = X;
            int localY = Y;

            bool result = BoardFunctions.TryToMove(X, Y, x, y);

            if (result)
            {
                ProducerConsumer.Produce(String.Format("object number {0} moved from ({1},{2}) to ({3},{4})", Id, localX, localY, x, y));
            }
        }
예제 #3
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);
        }
예제 #4
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);
        }
예제 #5
0
        public Tuple <int, int> GetRandomPosition()
        {
            int x = 0, y = 0;

            switch (randomOption)
            {
            case RandomOption.RealTime:
                // get a random position on the board
                x = MyRandom.Next(0, info.Length);
                y = MyRandom.Next(0, info.Hight);
                break;

            case RandomOption.Testing:
                x = rnd.Next(0, info.Length);
                y = rnd.Next(0, info.Hight);
                break;
            }
            return(new Tuple <int, int>(x, y));
        }
예제 #6
0
        public void Test_TryToMove()
        {
            IInfo           info  = A.Fake <IInfo>();
            IBoardFunctions board = A.Fake <IBoardFunctions>();
            IRandomTest     rnd   = A.Fake <IRandomTest>();

            IDynamicObject ant = new Ant(info, rnd)
            {
                X = 0, Y = 0, Agility = 1, BoardFunctions = board
            };

            A.CallTo(() => rnd.Next(A <int> .Ignored, A <int> .Ignored)).Returns(1);

            //gameLogic.ActAliveObject(dynamicObject);

            ant.TryToMove();
            A.CallTo(() => board.TryToMove(0, 0, 1, 1)).MustHaveHappened();
        }