Esempio n. 1
0
        public string ElementsToString()
        {
            if (Empty)
            {
                return("");
            }

            var elements = new List <string>();

            if (HasRobot)
            {
                elements.Add(Robot.ToString());
            }
            if (HasCorral)
            {
                elements.Add(Corral.ToString());
            }
            if (HasKid)
            {
                elements.Add(Kid.ToString());
            }
            if (HasDirt)
            {
                elements.Add(Dirt.ToString());
            }
            if (HasObstacle)
            {
                elements.Add(Obstacle.ToString());
            }

            var result = "[" + elements[0];

            for (int i = 1; i < elements.Count; i++)
            {
                result += ", " + elements[i];
            }
            result += "]";

            return(result);
        }
Esempio n. 2
0
 public Cell(Corral corral)
 {
     Corral = corral;
 }
Esempio n. 3
0
        public Environment(int M, int N, int Kids, double percentDirt, double percObstacles, RobotType robot, long milliSeconds)
        {
            if (Kids < 1)
            {
                throw new Exception("There has to be at least one kid");
            }
            if (M < 0 || N < 0)
            {
                throw new Exception("The Dimentions of the Map can't be negative");
            }
            if (percentDirt >= 60)
            {
                throw new Exception("The dirt has to be under 60%");
            }

            //Checking everything fits on the map
            var cells      = M * N;
            var kidCorrals = 2 * Kids;
            var newDirt    = (int)Math.Round(percentDirt * cells / 100);
            var newObst    = (int)Math.Round(percObstacles * cells / 100);

            if (cells < kidCorrals + newDirt + newObst + 1)
            {
                throw new Exception("The Map can't fit all the elements");
            }

            //Initializing Local Variables
            Count            = 0;
            Done             = false;
            Success          = false;
            Fired            = false;
            TotalKids        = Kids;
            env              = new Cell[M, N];
            r                = new Random(DateTime.Now.Millisecond);
            waitMilliSeconds = milliSeconds;
            stopwatch        = new Stopwatch();
            kids             = new List <Kid>();
            corrals          = new List <Corral>();

            //Adding the Corrals to the Map
            var corralCount = Kids;
            var width       = (int)Math.Sqrt(Kids);
            var height      = Kids / width;
            var remains     = Kids % width;
            var extra       = remains > 0 ? 1 : 0;

            var startX = r.Next(M);
            var startY = r.Next(N);

            startX = startX + width + extra < M ? startX : M - width - extra - 1;
            startY = startY + height < N ? startY : N - height - 1;

            for (int i = 0; i < width + extra; i++)
            {
                for (int j = 0; j < height && corralCount > 0; j++)
                {
                    var corral = new Corral(startX + i, startY + j);
                    env[startX + i, startY + j] = new Cell(corral);
                    corrals.Add(corral);
                }
            }

            //Saving the Rest of Available Positions
            var pos = new List <(int x, int y)>();

            for (int i = 0; i < M; i++)
            {
                for (int j = 0; j < N; j++)
                {
                    if (env[i, j] == null)
                    {
                        pos.Add((i, j));
                    }
                }
            }

            //Adding Obstacles
            while (newObst > 0)
            {
                var i        = r.Next(pos.Count);
                var obstacle = new Obstacle(pos[i].x, pos[i].y);
                env[pos[i].x, pos[i].y] = new Cell(obstacle);
                pos.RemoveAt(i);
                newObst--;
            }

            //Adding Dirt
            while (newDirt > 0)
            {
                var i    = r.Next(pos.Count);
                var dirt = new Dirt(pos[i].x, pos[i].y);
                env[pos[i].x, pos[i].y] = new Cell(dirt);
                pos.RemoveAt(i);
                newDirt--;
            }

            //Add Kids
            var kidCount = 1;

            while (kidCount <= Kids)
            {
                var i   = r.Next(pos.Count);
                var kid = new Kid(pos[i].x, pos[i].y, kidCount);
                env[pos[i].x, pos[i].y] = new Cell(kid);
                kids.Add(kid);
                pos.RemoveAt(i);
                kidCount++;
            }

            //Add Agent
            var t = r.Next(pos.Count);

            if (robot == RobotType.AgentA)
            {
                agent = new AgentA(pos[t].x, pos[t].y, env);
            }
            else
            {
                agent = new AgentB(pos[t].x, pos[t].y, env);
            }
            env[agent.PosX, agent.PosY] = new Cell(agent);
        }
Esempio n. 4
0
        protected Environment(RobotType robot, Cell[,] enviroment, long milliSeconds)
        {
            //Copying Data from one Enviroment to the Other
            kids    = new List <Kid>();
            corrals = new List <Corral>();
            env     = new Cell[enviroment.GetLength(0), enviroment.GetLength(1)];
            var foundAgent = false;
            int agentPosX = -1, agentPosY = -1;

            for (int i = 0; i < enviroment.GetLength(0); i++)
            {
                for (int j = 0; j < enviroment.GetLength(1); j++)
                {
                    if (enviroment[i, j] == null)
                    {
                        continue;
                    }
                    else
                    {
                        if (enviroment[i, j].HasObstacle)
                        {
                            var obs = new Obstacle(i, j);
                            env[i, j] = new Cell(obs);
                        }
                        else if (enviroment[i, j].HasDirt)
                        {
                            var dirt = new Dirt(i, j);
                            env[i, j] = new Cell(dirt);
                        }
                        else if (enviroment[i, j].HasCorral)
                        {
                            var corral = new Corral(i, j);
                            env[i, j] = new Cell(corral);
                            corrals.Add(corral);
                        }
                        else if (enviroment[i, j].HasKid)
                        {
                            var kid = new Kid(i, j, enviroment[i, j].Kid.Id);
                            env[i, j] = new Cell(kid);
                            kids.Add(kid);
                        }
                        else if (enviroment[i, j].HasRobot)
                        {
                            foundAgent = true;
                            agentPosX  = i;
                            agentPosY  = j;
                        }
                    }
                }
            }

            //Initializing Local Variables
            r                = new Random(DateTime.Now.Millisecond);
            Count            = 0;
            Done             = false;
            Success          = false;
            Fired            = false;
            TotalKids        = kids.Count;
            waitMilliSeconds = milliSeconds;
            stopwatch        = new Stopwatch();

            //Creating Agent
            if (!foundAgent)
            {
                throw new Exception("Can't Copy a Simulation without the Agent");
            }
            if (robot == RobotType.AgentA)
            {
                agent = new AgentA(agentPosX, agentPosY, env);
            }
            else
            {
                agent = new AgentB(agentPosX, agentPosY, env);
            }
            env[agent.PosX, agent.PosY] = new Cell(agent);
        }