Exemplo n.º 1
0
        public static TreeNode BuildTree(Color[,] board)
        {
            MapNode head = BuildMap(board);
            TreeNode root = new TreeNode(null, head.Color);

            Queue<MapTreeKeyValuePair> frontLine = new Queue<MapTreeKeyValuePair>();
            ISet<MapNode> visited = new HashSet<MapNode>();
            frontLine.Enqueue(new MapTreeKeyValuePair{MapNode = head, TreeNode = root});
            visited.Add(head);

            while (frontLine.Count > 0)
            {
                MapTreeKeyValuePair mapTree = frontLine.Dequeue();
                foreach (MapNode neighbor in mapTree.MapNode.GetNeighbors())
                {
                    if(!visited.Contains(neighbor))
                    {
                        TreeNode childTreeNode = new TreeNode(mapTree.TreeNode, neighbor.Color);
                        //Claim this map node as your child
                        mapTree.TreeNode.AddChildern(childTreeNode);
                        //mark map node as visited, no one can claim this map node again
                        visited.Add(neighbor);
                        //queue it up to find it's children
                        frontLine.Enqueue(new MapTreeKeyValuePair { MapNode = neighbor, TreeNode = childTreeNode });
                    }
                }
            }

            return root;
        }
Exemplo n.º 2
0
        private void AvoidObstacles()
        {
            const int mapBorderWidth = 4;
            int horLength = map.GetLength(0);
            int vertLength = map.GetLength(1);
            Point currentPosition = Point.Empty;
            Point destination = Point.Empty;
            for (int i = 0; i < horLength; i++)
            {
                for (int j = 0; j < vertLength; j++)
                {
                    if (this.mapWithIdealPath[i, j] == MyRoadType.CurrentPosition)
                    {
                        currentPosition = new Point(i, j);
                    }

                    if (this.mapWithIdealPath[i, j] == MyRoadType.Destination)
                    {
                        destination = new Point(i, j);
                    }
                }
            }

            bool[,] whatBecameIdeal = new bool[horLength, vertLength];
            Action<int, int> makeAllSimilarIdeal = (x, y) =>
            {
                if (x > 0 && y > 0 && this.map[x - 1, y - 1] == MyTileType.Green
                    && this.mapWithIdealPath[x - 1, y - 1] == MyRoadType.Empty
                    && !whatBecameIdeal[x - 1, y - 1])
                {
                    whatBecameIdeal[x - 1, y - 1] = true;
                }

                if (y > 0 && this.map[x, y - 1] == MyTileType.Green
                    && this.mapWithIdealPath[x, y - 1] == MyRoadType.Empty
                    && !whatBecameIdeal[x, y - 1])
                {
                    whatBecameIdeal[x, y - 1] = true;
                }

                if (x < horLength - 1 && y > 0 && this.map[x + 1, y - 1] == MyTileType.Green
                    && this.mapWithIdealPath[x + 1, y - 1] == MyRoadType.Empty
                    && !whatBecameIdeal[x + 1, y - 1])
                {
                    whatBecameIdeal[x + 1, y - 1] = true;
                }

                if (x > 0 && this.map[x - 1, y] == MyTileType.Green
                    && this.mapWithIdealPath[x - 1, y] == MyRoadType.Empty
                    && !whatBecameIdeal[x - 1, y])
                {
                    whatBecameIdeal[x - 1, y] = true;
                }

                if (x < horLength - 1 && this.map[x + 1, y] == MyTileType.Green
                    && this.mapWithIdealPath[x + 1, y] == MyRoadType.Empty
                    && !whatBecameIdeal[x + 1, y])
                {
                    whatBecameIdeal[x + 1, y] = true;
                }

                if (x > 0 && y < vertLength - 1 && this.map[x - 1, y + 1] == MyTileType.Green
                    && this.mapWithIdealPath[x - 1, y + 1] == MyRoadType.Empty
                    && !whatBecameIdeal[x - 1, y + 1])
                {
                    whatBecameIdeal[x - 1, y + 1] = true;
                }

                if (y < vertLength - 1 && this.map[x, y + 1] == MyTileType.Green
                    && this.mapWithIdealPath[x, y + 1] == MyRoadType.Empty
                    && !whatBecameIdeal[x, y + 1])
                {
                    whatBecameIdeal[x, y + 1] = true;
                }

                if (x < horLength - 1 && y < vertLength - 1 && this.map[x + 1, y + 1] == MyTileType.Green
                    && this.mapWithIdealPath[x + 1, y + 1] == MyRoadType.Empty
                    && !whatBecameIdeal[x + 1, y + 1])
                {
                    whatBecameIdeal[x + 1, y + 1] = true;
                }
            };

            bool[,] handledReds = new bool[horLength, vertLength];
            bool verticalRoad = Math.Abs(currentPosition.Y - destination.Y)
                                > Math.Abs(currentPosition.X - destination.X);
            while (true)
            {
                bool[,] newReds = new bool[horLength, vertLength];
                bool badTileFound = false;
                Point firstBad = Point.Empty;
                for (int i = mapBorderWidth; i < horLength - mapBorderWidth && !badTileFound; i++)
                {
                    for (int j = mapBorderWidth; j < vertLength - mapBorderWidth; j++)
                    {
                        if (this.mapWithIdealPath[i, j] != MyRoadType.Empty && map[i, j] == MyTileType.Red && !handledReds[i, j])
                        {
                            badTileFound = true;
                            firstBad.X = i;
                            firstBad.Y = j;
                            break;
                        }
                    }
                }

                if (!badTileFound)
                {
                    break;
                }

                Queue<Point> badPoints = new Queue<Point>();
                badPoints.Enqueue(firstBad);
                while (badPoints.Count > 0)
                {
                    Point bad = badPoints.Dequeue();
                    if (!newReds[bad.X, bad.Y])
                    {
                        if (Math.Abs(bad.X - firstBad.X) > 80 || Math.Abs(bad.Y - firstBad.Y) > 80)
                        {
                            continue;
                        }

                        newReds[bad.X, bad.Y] = true;
                        handledReds[bad.X, bad.Y] = true;
                        if (bad.X > mapBorderWidth && bad.Y >= mapBorderWidth && bad.X < horLength - mapBorderWidth
                            && bad.Y < vertLength - mapBorderWidth && this.map[bad.X - 1, bad.Y] == MyTileType.Red
                            && !newReds[bad.X - 1, bad.Y])
                        {
                            badPoints.Enqueue(new Point(bad.X - 1, bad.Y));
                        }

                        if (bad.X < horLength - 1 - mapBorderWidth && bad.X >= mapBorderWidth && bad.Y >= mapBorderWidth
                            && bad.Y < vertLength - mapBorderWidth && this.map[bad.X + 1, bad.Y] == MyTileType.Red
                            && !newReds[bad.X + 1, bad.Y])
                        {
                            badPoints.Enqueue(new Point(bad.X + 1, bad.Y));
                        }

                        if (bad.Y > mapBorderWidth && bad.X >= mapBorderWidth && bad.X < horLength - mapBorderWidth
                            && bad.Y < vertLength - mapBorderWidth && this.map[bad.X, bad.Y - 1] == MyTileType.Red
                            && !newReds[bad.X, bad.Y - 1])
                        {
                            badPoints.Enqueue(new Point(bad.X, bad.Y - 1));
                        }

                        if (bad.Y < vertLength - 1 - mapBorderWidth && bad.Y >= mapBorderWidth && bad.X >= mapBorderWidth
                            && bad.X < horLength - mapBorderWidth && this.map[bad.X, bad.Y + 1] == MyTileType.Red
                            && !newReds[bad.X, bad.Y + 1])
                        {
                            badPoints.Enqueue(new Point(bad.X, bad.Y + 1));
                        }
                    }
                }

                if (verticalRoad)
                {
                    int maxLeft = 0, maxRight = 0;
                    for (int j = mapBorderWidth; j < vertLength - mapBorderWidth; j++)
                    {
                        int idealX = -1;
                        int correction = 0;
                        for (int i = mapBorderWidth; i < horLength - mapBorderWidth; i++)
                        {
                            if (this.mapWithIdealPath[i, j] != MyRoadType.Empty)
                            {
                                idealX = i;
                                break;
                            }
                        }

                        if (idealX == -1)
                        {
                            if (Math.Abs(j - currentPosition.Y) < Math.Abs(j - destination.Y))
                            {
                                idealX = currentPosition.X;
                                correction = Math.Abs(j - currentPosition.Y);
                            }
                            else
                            {
                                idealX = destination.X;
                                correction = Math.Abs(j - destination.Y);
                            }
                        }

                        for (int i = mapBorderWidth; i < horLength - mapBorderWidth; i++)
                        {
                            if (newReds[i, j])
                            {
                                if (i < idealX)
                                {
                                    maxLeft = Math.Max(maxLeft, idealX - i + correction);
                                }
                                else if (i > idealX)
                                {
                                    maxRight = Math.Max(maxRight, i - idealX + correction);
                                }
                            }
                        }
                    }

                    for (int j = mapBorderWidth; j < vertLength - mapBorderWidth; j++)
                    {
                        int idealX = -1;
                        for (int i = mapBorderWidth; i < horLength - mapBorderWidth; i++)
                        {
                            if (this.mapWithIdealPath[i, j] != MyRoadType.Empty)
                            {
                                idealX = i;
                                break;
                            }
                        }

                        if (idealX == -1)
                        {
                            idealX = Math.Abs(j - currentPosition.Y) < Math.Abs(j - destination.Y)
                                         ? currentPosition.X
                                         : destination.X;
                        }

                        for (int i = mapBorderWidth; i < horLength - mapBorderWidth; i++)
                        {
                            if (newReds[i, j])
                            {
                                if (i < idealX && maxLeft <= maxRight)
                                {
                                    makeAllSimilarIdeal(i, j);
                                }
                                else if (i > idealX && maxLeft > maxRight)
                                {
                                    makeAllSimilarIdeal(i, j);
                                }
                            }
                        }
                    }
                }
                else
                {
                    int maxTop = 0, maxBottom = 0;
                    for (int i = mapBorderWidth; i < horLength - mapBorderWidth; i++)
                    {
                        int idealY = -1;
                        int correction = 0;
                        for (int j = mapBorderWidth; j < vertLength - mapBorderWidth; j++)
                        {
                            if (this.mapWithIdealPath[i, j] != MyRoadType.Empty)
                            {
                                idealY = j;
                                break;
                            }
                        }

                        if (idealY == -1)
                        {
                            if (Math.Abs(i - currentPosition.X) < Math.Abs(i - destination.X))
                            {
                                idealY = currentPosition.Y;
                                correction = Math.Abs(i - currentPosition.X);
                            }
                            else
                            {
                                idealY = destination.Y;
                                correction = Math.Abs(i - destination.X);
                            }
                        }

                        for (int j = mapBorderWidth; j < vertLength - mapBorderWidth; j++)
                        {
                            if (newReds[i, j])
                            {
                                if (j < idealY)
                                {
                                    maxTop = Math.Max(maxTop, idealY - j + correction);
                                }
                                else if (j > idealY)
                                {
                                    maxBottom = Math.Max(maxBottom, j - idealY + correction);
                                }
                            }
                        }
                    }

                    for (int i = mapBorderWidth; i < horLength - mapBorderWidth; i++)
                    {
                        int idealY = -1;
                        for (int j = mapBorderWidth; j < vertLength - mapBorderWidth; j++)
                        {
                            if (this.mapWithIdealPath[i, j] != MyRoadType.Empty)
                            {
                                idealY = j;
                                break;
                            }
                        }

                        if (idealY == -1)
                        {
                            idealY = Math.Abs(i - currentPosition.X) < Math.Abs(i - destination.X)
                                         ? currentPosition.Y
                                         : destination.Y;
                        }

                        for (int j = mapBorderWidth; j < vertLength - mapBorderWidth; j++)
                        {
                            if (newReds[i, j])
                            {
                                if (j < idealY && maxTop <= maxBottom)
                                {
                                    makeAllSimilarIdeal(i, j);
                                }
                                else if (j > idealY && maxTop > maxBottom)
                                {
                                    makeAllSimilarIdeal(i, j);
                                }
                            }
                        }
                    }
                }
            }

            for (int i = 0; i < horLength; i++)
            {
                for (int j = 0; j < vertLength; j++)
                {
                    if (this.map[i, j] == MyTileType.Red && this.mapWithIdealPath[i, j] == MyRoadType.IdealPath)
                    {
                        makeAllSimilarIdeal(i, j);
                        this.mapWithIdealPath[i, j] = MyRoadType.Empty;
                    }
                }
            }

            for (int i = 0; i < horLength; i++)
            {
                for (int j = 0; j < vertLength; j++)
                {
                    if (whatBecameIdeal[i, j])
                    {
                        this.mapWithIdealPath[i, j] = MyRoadType.IdealPath;
                    }
                }
            }
        }
Exemplo n.º 3
0
        //generate data for iteration 3 scenario 1
        public void createIteration3Patients(
            int patientCount_in, 
            int safePatientsNeeded_in,
            int safenessThreshold_in,
            List<GenerationRule> generationRules_in)
        {
            int patientCount = patientCount_in;
            int safePatientsNeeded = safePatientsNeeded_in;
            int patientsInserted = 0;
            int safenessThreshold = safenessThreshold_in;
            Queue<GenerationRule> generationRules = new Queue<GenerationRule>(generationRules_in);
            GenerationRule currentRule = generationRules.Dequeue();
            Random r = new Random();
            List<Visit> safeVisitsBuffer = new List<Visit>();
            DbMethods db = DbMethods.getInstance();
            int counter = 0;

            db.clearTables();

            while (patientsInserted < patientCount)
            {
                if (currentRule.PatientsLeft == 0)
                {
                    currentRule = generationRules.Dequeue();
                }

                db.InsertPatient();
                int patientID = patientsInserted;//db.InsertPatient();
                int visitsLeft = currentRule.VisitsAllowed;

                //adding safe visits
                if (safePatientsNeeded >= 0)
                {
                    if (patientsInserted % (safenessThreshold+1) == 0)
                    {
                        safeVisitsBuffer = new List<Visit>();

                        while(visitsLeft > 0)
                        {
                            safeVisitsBuffer.Add(new Visit(-1, r.Next(1, 10), r.Next(1, 6), new DateTime(2012, r.Next(1, 13), r.Next(1, 29))));
                            visitsLeft--;
                        }
                    }

                    db.InsertVisits(new Dictionary<int, List<Visit>>() { { patientID, safeVisitsBuffer } });
                    safePatientsNeeded--;
                }
                else
                {
                    while (visitsLeft > 0)
                    {
                        db.InsertVisit(patientID, r.Next(1, 10), r.Next(1, 6), new DateTime(2012, r.Next(1, 13), r.Next(1, 29)));
                        visitsLeft--;
                    }
                }

                currentRule.PatientsLeft--;
                patientsInserted++;
            }
        }
Exemplo n.º 4
0
        public void Listas()
        {
            List<Produto> produtos = new List<Produto>();

            var produto = new Produto
            {
                Nome = "Xbox One"
            };

            produtos.Add(produto);
            produtos.Add(produto);

            Assert.AreEqual(2, produtos.Count);

            HashSet<Produto> produtosUnicos = new HashSet<Produto>();

            produtosUnicos.Add(produto);
            produtosUnicos.Add(produto);

            Assert.AreEqual(1, produtosUnicos.Count);

            Stack<Produto> pilha = new Stack<Produto>();

            var produto2 = new Produto
            {
                Nome = "PS4"
            };

            pilha.Push(produto);
            pilha.Push(produto2);

            var produtoPeek = pilha.Peek();
            var produtoPop = pilha.Pop();
            var produtoPop2 = pilha.Pop();

            if (pilha.Any())
            {
                produtoPeek = pilha.Peek();
            }

            Queue<Produto> fila = new Queue<Produto>();

            fila.Enqueue(produto);
            fila.Enqueue(produto2);

            var primeiroDaFila = fila.Peek();
            primeiroDaFila = fila.Dequeue();

            Console.WriteLine(primeiroDaFila);

            var dic = new Dictionary<int, Produto>();

            dic.Add(50, produto);
            dic.Add(90, produto2);

            Console.WriteLine(dic[50]);
            Console.WriteLine(dic[90]);

            if (dic.ContainsKey(5))
            {
                Console.WriteLine(dic[5]);
            }
            else
            {
                dic[5] = produto;

                Console.WriteLine(dic[5]);
            }
        }
Exemplo n.º 5
0
        private void RunRound(Queue<Character> currentQueue, Queue<Character> nextQueue, int turn)
        {
            Output("Round: " + turn);

            while (currentQueue.Count > 0)
            {
                Character currentCharacter = currentQueue.FirstOrDefault();
                if (currentCharacter.CharBase.Alive)
                    currentCharacter.CharAI.DoTurn(fullList);
                else
                {
                    msg.Add(currentCharacter.CharBase.Name, Color.Red);
                    msg.Add(" remains dead..", Color.Black);
                    cleverOutput(msg);
                }

                nextQueue.Enqueue(currentQueue.Dequeue());
            }
        }