コード例 #1
0
        public void RainTest()
        {
            XmlReaderWriter reader = new XmlReaderWriter();

            reader.ReadMe("..\\..\\tests\\test-RAIN-anthill.xml");

            Simulation tmp_isw = new Simulation(new Map(AntHillConfig.mapColCount, AntHillConfig.mapRowCount, AntHillConfig.tiles));

            AHGraphics.Init();

            Assert.IsNotNull(tmp_isw, "Simulation is NULL problem!!!");

            Spider test_spider = new Spider(new Position(61, 71));

            tmp_isw.spiders.AddLast(test_spider);
            Ant test_ant1 = new Warrior(new Position(62, 71));
            Ant test_ant2 = new Worker(new Position(61, 72));

            tmp_isw.ants.AddLast(test_ant1);
            tmp_isw.ants.AddLast(test_ant2);

            Rain test_rain = new Rain(new Position(60, 70));

            Assert.IsNotNull(test_rain, "Rain is NULL problem!!!");

            Assert.IsTrue(test_rain.IsRainOver(60, 70), "Rain isn't exist or Rain.IsRainOver problem");
            Assert.IsTrue(test_rain.IsRainOver(62, 72), "Rain is too small or Rain.IsRainOver problem");
            //Assert.IsTrue(test_rain.IsRainOver(58, 68), "Rain is too small or Rain.IsRainOver problem");
            Assert.IsTrue(test_rain.IsRainOver(63, 73), "Rain is too big or Rain.IsRainOver problem");
            Assert.IsFalse(test_rain.IsRainOver(57, 67), "Rain is too big or Rain.IsRainOver problem");

            Assert.AreEqual(new Position(60, 70), test_rain.Position, "Rain.Position problem");
            Assert.IsTrue((test_rain.TimeToLive >= 0) && (test_rain.TimeToLive < AntHillConfig.rainMaxDuration + 1), "Rain.TimeToLive range problem");
            int tmp = test_rain.TimeToLive;

            Assert.AreEqual(tmp, test_rain.TimeToLive, "Rain.TimeToLive problem should be {0}, but is {1}", tmp, test_rain.TimeToLive);

            Assert.IsTrue(tmp_isw.spiders.Contains(test_spider), "Find spider problem");
            Assert.IsTrue(tmp_isw.ants.Contains(test_ant1), "Find warrior problem");
            Assert.IsTrue(tmp_isw.ants.Contains(test_ant2), "Find worker problem");

            Assert.IsNotNull(test_rain, "Rain is NULL problem!!!");

            test_rain.Maintain((ISimulationWorld)tmp_isw);
            Assert.AreEqual(tmp - 1, test_rain.TimeToLive, "Rain.Maintain problem should be {0}, but is {1}", tmp - 1, test_rain.TimeToLive);

            Assert.IsFalse(tmp_isw.spiders.Contains(test_spider), "Rain destroy spiders problem");
            Assert.IsFalse(tmp_isw.ants.Contains(test_ant1), "Rain destroy warriors problem");
            Assert.IsFalse(tmp_isw.ants.Contains(test_ant2), "Rain destroy workers problem");
        }
コード例 #2
0
        /// <summary>
        /// This is the most important function - activity diagram
        /// </summary>
        bool ISimulationUser.DoTurn()
        {
            if (queen == null)
            {
                return(false);
            }
            _turnCounter++;
            if (Randomizer.NextDouble() <= AntHillConfig.spiderProbability)
            {
                this.CreateSpider(Map.GetRandomTile(TileType.Outdoor).Position);
            }

            if (Randomizer.NextDouble() <= AntHillConfig.foodProbability)
            {
                this.CreateFood(Map.GetRandomTile(TileType.Outdoor).Position, GetRandomFoodQuantity());
            }

            if ((rain == null) && (Randomizer.NextDouble() <= AntHillConfig.rainProbability))
            {
                this.CreateRain(GetRandomPositionForRain());
            }

            if (rain != null)
            {
                rain.Maintain(this);
            }

            LinkedListNode <Message> msg = messages.First;
            LinkedListNode <Message> msgT;

            while (msg != null)
            {
                msg.Value.Maintain(this);
                if (msg.Value.Empty)
                {
                    msgT = msg;
                    msg  = msg.Next;
                    messages.Remove(msgT);
                }
                else
                {
                    msg = msg.Next;
                }
            }

            LinkedListNode <Ant> ant = ants.First;
            LinkedListNode <Ant> antTemp;

            while (ant != null)
            {
                if (!ant.Value.Maintain(this))
                {
                    antTemp = ant;
                    ant     = ant.Next;
                    ants.Remove(antTemp);
                }
                else
                {
                    ant = ant.Next;
                }
            }

            LinkedListNode <Spider> spider = spiders.First;
            LinkedListNode <Spider> spiderTemp;

            while (spider != null)
            {
                if (!spider.Value.Maintain(this))
                {
                    spiderTemp = spider;
                    spider     = spider.Next;
                    spiders.Remove(spiderTemp);
                }
                else
                {
                    spider = spider.Next;
                }
            }

            LinkedListNode <Egg> egg = eggs.First;
            LinkedListNode <Egg> eggTemp;

            while (egg != null)
            {
                if (!egg.Value.Maintain(this))
                {
                    eggTemp = egg;
                    egg     = egg.Next;
                    eggs.Remove(eggTemp);
                }
                else
                {
                    egg = egg.Next;
                }
            }

            if (queen != null && !queen.Maintain(this))
            {
                queen = null;
                return(false);
            }
            return(true);
        }