コード例 #1
0
        private MapItem bfsColl(MapItem[,] map, MapItem s, String[] whatToFind)
        {
            int playerX = Int32.Parse(s.Name[0].ToString());
            int playerY = Int32.Parse(s.Name[2].ToString());
            //Console.WriteLine("WEWEWE: " + whatToFind);
            for (int i = 0; i < map.GetLength(0); i++)
            {
                for (int j = 0; j < map.GetLength(1); j++)
                {
                    map[j, i].Colour = this.WHITE;
                    map[j, i].Pre = null;
                    map[j, i].Dis = Int32.MaxValue - 1;
                }
            }
            s.Colour = this.GRAY;
            s.Dis = 0;

            Queue<MapItem> Q = new Queue<MapItem>();
            Q.Enqueue(s);
            while (Q.Count > 0)
            {
                MapItem u = Q.Dequeue();
                int x = Int32.Parse(u.Name[0].ToString());
                int y = Int32.Parse(u.Name[2].ToString());

                try
                {
                    //if (map[y, x - 1].Contain.Equals(MapItem.BLANK))
                    if (blankList.Contains(map[y, x - 1].Contain, StringComparer.Ordinal))
                    {
                        if (map[y, x - 1].Colour.Equals(this.WHITE))
                        {
                            map[y, x - 1].Colour = this.GRAY;
                            map[y, x - 1].Dis = u.Dis + 1;
                            map[y, x - 1].Pre = u;
                            Q.Enqueue(map[y, x - 1]);
                        }

                    }
                    if (playerX != x - 1 && playerY!=y && whatToFind.Contains(map[y, x - 1].Contain, StringComparer.Ordinal))
                    {
                        //Console.WriteLine(y+" " +(x-1)+" "+ map[y, x - 1].Contain);
                        map[y, x - 1].Pre = u;
                        return map[y, x - 1];
                    }
                }
                catch (Exception e)
                {
                }
                try
                {
                    //if (map[y - 1, x].Contain.Equals(MapItem.BLANK))
                    if (blankList.Contains(map[y - 1, x].Contain, StringComparer.Ordinal))
                    {
                        if (map[y - 1, x].Colour.Equals(this.WHITE))
                        {
                            map[y - 1, x].Colour = this.GRAY;
                            map[y - 1, x].Dis = u.Dis + 1;
                            map[y - 1, x].Pre = u;
                            Q.Enqueue(map[y - 1, x]);
                        }

                    }

                    if (playerX != x && playerY != y - 1 && whatToFind.Contains(map[y - 1, x].Contain, StringComparer.Ordinal))
                    {
                        //Console.WriteLine((y-1) + " " + x + " " + map[y, x - 1].Contain);
                        map[y - 1, x].Pre = u;
                        return map[y - 1, x];
                    }

                }
                catch (Exception e)
                {
                }
                try
                {
                    //if (map[y, x + 1].Contain.Equals(MapItem.BLANK))
                    if (blankList.Contains(map[y, x + 1].Contain, StringComparer.Ordinal))
                    {
                        if (map[y, x + 1].Colour.Equals(this.WHITE))
                        {
                            map[y, x + 1].Colour = this.GRAY;
                            map[y, x + 1].Dis = u.Dis + 1;
                            map[y, x + 1].Pre = u;
                            Q.Enqueue(map[y, x + 1]);
                        }

                    }
                    if (playerX != x + 1 && playerY != y && whatToFind.Contains(map[y, x + 1].Contain, StringComparer.Ordinal))
                    {
                        //Console.WriteLine(y + " " + (x+1) + " " + map[y, x - 1].Contain);
                        map[y, x + 1].Pre = u;
                        return map[y, x + 1];
                    }

                }
                catch (Exception e)
                {
                }
                try
                {
                    //if (map[y + 1, x].Contain.Equals(MapItem.BLANK))
                    if (blankList.Contains(map[y + 1, x].Contain, StringComparer.Ordinal))
                    {
                        if (map[y + 1, x].Colour.Equals(this.WHITE))
                        {
                            map[y + 1, x].Colour = this.GRAY;
                            map[y + 1, x].Dis = u.Dis + 1;
                            map[y + 1, x].Pre = u;
                            Q.Enqueue(map[y + 1, x]);
                        }

                    }
                    if (playerX != x && playerY != y + 1 && whatToFind.Contains(map[y + 1, x].Contain, StringComparer.Ordinal))
                    {
                        //Console.WriteLine((y+1) + " " + x + " " + map[y, x - 1].Contain);
                        map[y + 1, x].Pre = u;
                        return map[y + 1, x];
                    }

                }
                catch (Exception e)
                {
                }
                u.Colour = this.BLACK;
            }
            return null;
        }
コード例 #2
0
        /*
          DIJKSTRA( G, w, s)
            1. INITIALIZE-SINGLE-SOURCE(G, s)
            2. S = 
            3. Q = G.V
            4. while Q 
            5. u = EXTRACT-MIN(Q)
            6.S = S {u}
            7.for each v G.Adj[u]
            8.RELAX (u, v, w)

         */
        private void dijkstra(MapItem[,] map, MapItem s, MapItem d)
        {
            for (int i = 0; i < map.GetLength(0); i++)
            {
                for (int j = 0; j < map.GetLength(1); j++)
                {
                    map[j, i].Pre = null;
                    map[j, i].Dis = Int32.MaxValue - 1;
                }
            }
            s.Dis = 0;

            List<MapItem> S = new List<MapItem>();
            List<MapItem> Q = new List<MapItem>();

            for (int k = 0; k < map.GetLength(0); k++)
                for (int l = 0; l < map.GetLength(1); l++)
                    Q.Add(map[k, l]);

            while (Q.Count > 0)
            {
                MapItem u = Q.Min();
                Q.Remove(u);
                S.Add(u);
                int x = Int32.Parse(u.Name[0].ToString());
                int y = Int32.Parse(u.Name[2].ToString());

                try
                {
                    //if (map[y, x - 1].Contain.Equals(MapItem.BLANK))
                    if (blankList.Contains(map[y, x - 1].Contain, StringComparer.Ordinal))
                    {
                        relax(u, map[y, x - 1]);
                        if (map[y, x - 1].Name.Equals(d.Name))
                            return;
                    }
                }
                catch (Exception e)
                {
                }
                try
                {
                    //if (map[y - 1, x].Contain.Equals(MapItem.BLANK))
                    if (blankList.Contains(map[y - 1, x].Contain, StringComparer.Ordinal))
                    {
                        relax(u, map[y - 1, x]);
                        if (map[y - 1, x].Name.Equals(d.Name))
                            return;
                    }
                }
                catch (Exception e)
                {
                }
                try
                {
                    //if (map[y, x + 1].Contain.Equals(MapItem.BLANK))
                    if (blankList.Contains(map[y, x + 1].Contain, StringComparer.Ordinal))
                    {
                        relax(u, map[y, x + 1]);
                        if (map[y, x + 1].Name.Equals(d.Name))
                            return;

                    }
                }
                catch (Exception e)
                {
                }
                try
                {
                    //if (map[y + 1, x].Contain.Equals(MapItem.BLANK))
                    if (blankList.Contains(map[y + 1, x].Contain, StringComparer.Ordinal))
                    {
                        relax(u, map[y + 1, x]);
                        if (map[y + 1, x ].Name.Equals(d.Name))
                            return;
                    }
                }
                catch (Exception e)
                {
                }
            }
        }
コード例 #3
0
        public bool shouldShoot(MapItem[,] map,MapItem player)
        {
            int x = Int32.Parse(player.Name[0].ToString());
            int y = Int32.Parse(player.Name[2].ToString());
            String[] notShoot = { DecodeOperations.stoneSimbol};
            int dir = player.Dir;

            //Console.WriteLine(x+" :::: "+y);
            switch(dir)
            {
                case 0:
                    while(y>0)
                    {
                        y--;
                        if (notShoot.Contains(map[y, x].Contain, StringComparer.Ordinal))
                            return false;
                        if (DecodeOperations.playerDir.Contains(map[y, x].Contain, StringComparer.Ordinal))
                            return true;
                    }
                    break;
                case 1:
                    while (x < map.GetLength(0)-1)
                    {
                        x++;
                        if (notShoot.Contains(map[y, x].Contain, StringComparer.Ordinal))
                            return false;
                        if (DecodeOperations.playerDir.Contains(map[y, x].Contain, StringComparer.Ordinal))
                            return true;
                    }
                    break;
                case 2:
                    while (y < map.GetLength(0)-1)
                    {
                        y++;
                        if (notShoot.Contains(map[y, x].Contain, StringComparer.Ordinal))
                            return false;
                        if (DecodeOperations.playerDir.Contains(map[y, x].Contain, StringComparer.Ordinal))
                            return true;
                    }
                    break;
                case 3:
                    while (x > 0)
                    {
                        x--;
                        if (notShoot.Contains(map[y, x].Contain, StringComparer.Ordinal))
                            return false;
                        if (DecodeOperations.playerDir.Contains(map[y, x].Contain, StringComparer.Ordinal))
                            return true;
                    }
                    break;
            }
            return false;
        }