예제 #1
0
 /// <summary>
 /// Removes a light
 /// </summary>
 /// <param name="Wall">Handle</param>
 public void RemoveWall(Wall wall)
 {
     if (Walls.Contains(wall))
     {
         Walls.Remove(wall);
     }
 }
예제 #2
0
        public override string ToString()
        {
            StringBuilder sb          = new StringBuilder();
            StringBuilder sbCreatures = new StringBuilder();

            for (int y = 0; y < BoundingRect.Height; y++)
            {
                for (int x = 0; x < BoundingRect.Width; x++)
                {
                    Point p = new Point(x, y);
                    if (Walls.Contains(p))
                    {
                        sb.Append("#");
                    }
                    else if (Creatures.ContainsKey(p))
                    {
                        sb.Append(Creatures[p].Symbol);
                        sbCreatures.Append($"{Creatures[p].Symbol}({Creatures[p].HitPoints}),");
                    }
                    else
                    {
                        sb.Append(".");
                    }
                }
                sb.Append("\t");
                sb.Append(sbCreatures);
                sbCreatures.Clear();
                sb.Append("\n");
            }
            //foreach (var kvp in Creatures) sb.Append($"{kvp.Key} ({kvp.Value.Symbol}): HP {kvp.Value.HitPoints}\n");

            return(sb.ToString());
        }
예제 #3
0
        public void BuildGraph()
        {
            AdjacencyList = new Dictionary <Point, List <Point> >();
            for (int y = 0; y < BoundingRect.Height; y++)
            {
                for (int x = 0; x < BoundingRect.Width; x++)
                {
                    Point p = new Point(x, y);
                    if (Walls.Contains(p) || Creatures.ContainsKey(p))
                    {
                        continue;
                    }

                    AdjacencyList.Add(p, new List <Point>());
                    List <Point> adjacentList = Fifteen.AdjacentPoints(p);
                    foreach (Point ap in adjacentList)
                    {
                        if (Walls.Contains(ap) || Creatures.ContainsKey(ap))
                        {
                            continue;
                        }
                        AdjacencyList[p].Add(ap);
                    }
                }
            }
        }
예제 #4
0
파일: Node.cs 프로젝트: s162995/CtC
    // Node expansion method
    public List <Node> GetExpandedNodes()
    {
        List <Node> expandedNodes = new List <Node>();
        Location    newLocation   = null;

        foreach (Action a in Enum.GetValues(typeof(Action)))
        {
            switch (a)
            {
            case Action.Up:
                newLocation = new Location(CurLocation.Row - 1, CurLocation.Col);

                break;

            case Action.Down:
                newLocation = new Location(CurLocation.Row + 1, CurLocation.Col);

                break;

            case Action.Left:
                newLocation = new Location(CurLocation.Row, CurLocation.Col - 1);

                break;

            case Action.Right:
                newLocation = new Location(CurLocation.Row, CurLocation.Col + 1);

                break;

            case Action.None:
                newLocation = new Location(CurLocation);

                break;
            }

            if (!Walls.Contains(newLocation) && !Obstacles.Contains(newLocation))
            {
                expandedNodes.Add(ChildNode(newLocation));
            }
        }

        //expandedNodes.OrderBy(item => rnd.Next());

        return(expandedNodes);
    }
예제 #5
0
        // Display the current map
        public override string ToString()
        {
            string s = "";

            for (int y = 0; y < Rows; y++)
            {
                for (int x = 0; x < Cols; x++)
                {
                    Coordinates pos = new Coordinates(x, y);
                    char        c   = '_';
                    if (MyAnts.ContainsKey(pos))
                    {
                        c = 'a';
                    }
                    else if (Walls.Contains(pos))
                    {
                        c = '#';
                    }
                    else if (Enemies.Contains(pos))
                    {
                        c = 'e';
                    }
                    else if (Food.Contains(pos))
                    {
                        c = '.';
                    }
                    else if (Hills.Contains(pos))
                    {
                        c = '*';
                    }
                    s += c;
                }
                s += "\n";
            }
            return(s);
        }
예제 #6
0
        public void ComputeMoves(GameActionsMessage msg)
        {
            // Next ants positions
            Dictionary <Coordinates, Ant> updated = new Dictionary <Coordinates, Ant>();

            Random rand = new Random();

            foreach (KeyValuePair <Coordinates, Ant> pair in MyAnts)
            {
                Ant          ant = pair.Value;
                Coordinates  pos = pair.Key;
                AntDirection move;
                Coordinates  next;
                if (ant.HasFood)
                {
                    if (ant.History.Count > 0 && !Hills.Contains(pos))
                    {
                        // Walk back
                        move = ant.History.Pop().Reverse();
                        next = pos.ApplyDirection(move).Normalize(this);
                        // If the path is blocked, don't move (and put the move back on the history for next round)
                        if (!Enemies.Contains(next) && !updated.ContainsKey(next))
                        {
                            msg.AddMove(pos.Col, pos.Row, move.ToString());
                            updated.Add(next, ant);
                        }
                        else
                        {
                            ant.History.Push(move.Reverse());
                        }
                        continue;
                    }
                    else
                    {
                        ant.HasFood = false;
                    }
                }
                move = GetNearbyFood(pos);
                // If there is accessible food, go there
                if (move == AntDirection.U)
                {
                    List <AntDirection> remaining = new List <AntDirection>()
                    {
                        AntDirection.N, AntDirection.S, AntDirection.E, AntDirection.W
                    };
                    int index;
                    // First move to try : continue the same way or not ? 50% chance
                    if (ant.History.Count > 0 && rand.NextDouble() < 0.5)
                    {
                        move = ant.History.Peek();
                        remaining.Remove(move);
                    }
                    else
                    {
                        index = rand.Next(4);
                        move  = remaining[index];
                        remaining.RemoveAt(index);
                    }
                    int size = remaining.Count;
                    do
                    {
                        Coordinates result = pos.ApplyDirection(move).Normalize(this);
                        if (!Walls.Contains(result) && !MyAnts.ContainsKey(result) && !updated.ContainsKey(result))
                        {
                            break;
                        }
                        if (size > 0)
                        {
                            index = rand.Next(size);
                            move  = remaining[index];
                            remaining.RemoveAt(index);
                        }
                        else
                        {
                            move = AntDirection.U;
                        }
                        size--;
                    } while (size >= 0);
                }
                else
                {
                    ant.HasFood = true;
                }
                next = pos.ApplyDirection(move).Normalize(this);
                if (move != AntDirection.U && !updated.ContainsKey(next))
                {
                    ant.History.Push(move);
                    msg.AddMove(pos.Col, pos.Row, move.ToString());
                    updated.Add(next, ant);
                }
            }

            MyAnts = updated;
        }
예제 #7
0
        public (Point, int) GetPathDistPoint_AStar(Point from, Point to)
        {
            List <Point> closedSet = new List <Point>();
            List <Point> openSet   = new List <Point>()
            {
                from
            };
            Dictionary <Point, Point> cameFrom = new Dictionary <Point, Point>();
            Dictionary <Point, int>   gScore   = new Dictionary <Point, int>()
            {
                { from, 0 }
            };
            Dictionary <Point, int> fScore = new Dictionary <Point, int>()
            {
                { from, Fifteen.ManhattanDist(from, to) }
            };

            bool found = false;

            while (openSet.Any())
            {
                Point current = openSet.OrderBy(p => fScore[p]).First();
                if (current.Equals(to))
                {
                    found = true; break;
                }

                openSet.Remove(current);
                closedSet.Add(current);

                foreach (Point p in Fifteen.AdjacentPoints(current).OrderBy(p => p))
                {
                    if (Walls.Contains(p) || Creatures.ContainsKey(p))
                    {
                        continue;
                    }
                    if (closedSet.Contains(p))
                    {
                        continue;
                    }
                    int tentativeGScore = gScore[current] + 1;
                    if (!openSet.Contains(p))
                    {
                        openSet.Add(p);
                    }
                    else if (tentativeGScore >= gScore[p])
                    {
                        continue;
                    }

                    cameFrom[p] = current;
                    gScore[p]   = tentativeGScore;
                    fScore[p]   = Fifteen.ManhattanDist(p, to);
                    //fScore[p] = tentativeGScore + Fifteen.ManhattanDist(p, to) + p.X + p.Y;
                }
            }

            if (found)
            {
                int          dist      = 0;
                Point        current   = to;
                List <Point> totalPath = new List <Point>()
                {
                    current
                };
                while (cameFrom.ContainsKey(current))
                {
                    dist++;
                    current = cameFrom[current];
                    totalPath.Add(current);
                }
                return(totalPath[totalPath.Count - 2], dist);
            }
            else
            {
                return(null, int.MaxValue);
            }
        }
예제 #8
0
        public (Point, int) GetPathDistPoint_BFS(Point from, HashSet <Point> spots)
        {
            Dictionary <int, List <Point> > distancePoints = new Dictionary <int, List <Point> >()
            {
                { 0, new List <Point> {
                      from
                  } }
            };
            Queue <Point>             openSet   = new Queue <Point>();
            HashSet <Point>           closedSet = new HashSet <Point>();
            Dictionary <Point, Point> cameFrom  = new Dictionary <Point, Point>();

            cameFrom[from] = null;
            openSet.Enqueue(from);

            bool  found       = false;
            Point closestSpot = null;

            while (openSet.Any())
            {
                Point currentRoot = openSet.Dequeue();
                if (spots.Contains(currentRoot))
                {
                    found = true;
                }

                foreach (Point p in Fifteen.AdjacentPoints(currentRoot).OrderBy(p => p))
                {
                    if (Walls.Contains(p) || Creatures.ContainsKey(p))
                    {
                        continue;
                    }
                    if (closedSet.Contains(p))
                    {
                        continue;
                    }

                    if (!openSet.Contains(p))
                    {
                        cameFrom[p] = currentRoot;
                        openSet.Enqueue(p);
                    }
                }
                closedSet.Add(currentRoot);
            }

            if (found)
            {
                spots.RemoveWhere(p => !cameFrom.ContainsKey(p));

                Dictionary <int, List <Point> > results = new Dictionary <int, List <Point> >();
                foreach (Point spot in spots)
                {
                    int          dist      = 0;
                    Point        current   = spot;
                    List <Point> totalPath = new List <Point>()
                    {
                        current
                    };
                    while (current != null && cameFrom.ContainsKey(current))
                    {
                        dist++;
                        current = cameFrom[current];
                        totalPath.Add(current);
                    }
                    if (!results.ContainsKey(dist))
                    {
                        results.Add(dist, new List <Point>());
                    }
                    results[dist].Add(totalPath[totalPath.Count - 3]);
                }

                int   minDist  = results.Keys.Min();
                Point minPoint = results[minDist].OrderBy(p => p).First();
                return(minPoint, minDist);
            }
            else
            {
                return(null, int.MaxValue);
            }
        }