myGully.com Boerse.SH - BOERSE.AM - BOERSE.IO - BOERSE.IM Boerse.BZ .TO Nachfolger
Zurück   myGully.com > Computer & Technik > Programmierung
Seite neu laden

A* Java problem

Willkommen

myGully

Links

Forum

 
 
 
Themen-Optionen Ansicht
Prev Vorheriger Beitrag   Nächster Beitrag Next
Ungelesen 18.12.11, 00:24   #1
PunchyDEADBEEF
Anfänger
 
Registriert seit: Sep 2011
Beiträge: 4
Bedankt: 0
PunchyDEADBEEF ist noch neu hier! | 0 Respekt Punkte
Standard A* Java problem

Hi, hat irgendwer eine Ahnung wieso dieser A* nie closest.equals(end) validiert?
PHP-Code:
  private ArrayList<Pointopen;
    private 
ArrayList<Pointclosed;
    private 
HashMap<PointPointparents;
    private 
HashMap<Point,IntegertotalCost;
    
    public 
PathFinder() {
        
this.open = new ArrayList<Point>();
        
this.closed = new ArrayList<Point>();
        
this.parents = new HashMap<PointPoint>();
        
this.totalCost = new HashMap<PointInteger>();
    }
    
    private 
int heuristicCost(Point fromPoint to) {
        return 
Math.max(Math.abs(from.to.x), Math.abs(from.to.y));
    }

    private 
int costToGetTo(Point from) {
        return 
parents.get(from) == null : (costToGetTo(parents.get(from)));
    }
    
    private 
int totalCost(Point fromPoint to) {
        if (
totalCost.containsKey(from))
            return 
totalCost.get(from);
        
        
int cost costToGetTo(from) + heuristicCost(fromto);
        
totalCost.put(fromcost);
        return 
cost;
    }

    private 
void reParent(Point childPoint parent){
        
parents.put(childparent);
        
totalCost.remove(child);
    }

    public 
ArrayList<PointfindPath(Creature creaturePoint startPoint endint maxTries) {
        
open.clear();
        
closed.clear();
        
parents.clear();
        
totalCost.clear();
        
        
open.add(start);
        
        for (
int tries 0tries maxTries && open.size() > 0tries++){
            
Point closest getClosestPoint(end);
            
            
open.remove(closest);
            
closed.add(closest);

            if (
closest.equals(end))
                return 
createPath(startclosest);
            else
                
checkNeighbors(creatureendclosest);
        }
        return 
null;
    }

    private 
Point getClosestPoint(Point end) {
        
Point closest open.get(0);
        for (
Point other open){
            if (
totalCost(otherend) < totalCost(closestend))
                
closest other;
        }
        return 
closest;
    }

    private 
void checkNeighbors(Creature creaturePoint endPoint closest) {
        for (
Point neighbor closest.neighbors8()) {
            if (
closed.contains(neighbor)
                    
//|| !creature.canEnter(neighbor.x, neighbor.y)
                    
&& !neighbor.equals(end))
                continue;
            
            if (
open.contains(neighbor))
                
reParentNeighborIfNecessary(closestneighbor);
            else
                
reParentNeighbor(closestneighbor);
        }
    }

    private 
void reParentNeighbor(Point closestPoint neighbor) {
        
reParent(neighborclosest);
        
open.add(neighbor);
    }

    private 
void reParentNeighborIfNecessary(Point closestPoint neighbor) {
        
Point originalParent parents.get(neighbor);
        
double currentCost costToGetTo(neighbor);
        
reParent(neighborclosest);
        
double reparentCost costToGetTo(neighbor);
        
        if (
reparentCost currentCost)
            
open.remove(neighbor);
        else
            
reParent(neighbororiginalParent);
    }

    private 
ArrayList<PointcreatePath(Point startPoint end) {
        
ArrayList<Pointpath = new ArrayList<Point>();

        while (!
end.equals(start)) {
            
path.add(end);
            
end parents.get(end);
        }

        
Collections.reverse(path);
        return 
path;
    } 
PunchyDEADBEEF ist offline   Mit Zitat antworten
 


Forumregeln
Du kannst keine neue Themen eröffnen
Du kannst keine Antworten verfassen
Du kannst keine Anhänge posten
Du kannst nicht deine Beiträge editieren

BB code is An
Smileys sind An.
[IMG] Code ist An.
HTML-Code ist Aus.

Gehe zu


Alle Zeitangaben in WEZ +1. Es ist jetzt 07:18 Uhr.


Sitemap

().