labyrintherstellung

Status
Nicht offen für weitere Antworten.
moin, sitze hier und komm nicht vom fleck.
mein problem: will ein zufälliges labyrinth erstellen, bei dem es immer einen weg gibt.
ansatz (pseudocode):
depth-first-code

LiFoStack stack= null
int totalCells = labyrinth.höhe * labyrinth.breite
cell currentCell = labyrinth.random (x,y)
int visitedCells = 1
while (visitedCells < totalCells)
neighborCell = labyrinth.randomNeighborCellWithWallsUp(currentCell)
if neighborCell == 0
currentCell = stack.pop()
else
labyrinth.removeWall(currentCell, neighborCell)
stack.push(currentCell)
currentCell = neighborCell
visitedCells++
end if
end while

wie schreibe ich diesen code in java (swing, awt, beans)??
und wie sieht der code dann aus?
hoffe mir kann jemand helfen.
danke
 

André Uhres

Top Contributor
standortpfarrer hat gesagt.:
..und wie sieht der code dann aus?..
Code:
       stack = new Stack<Cell>();
        totalCells = höhe * breite;
        currentCell = random();
        visitedCells = 1;
        while(visitedCells < totalCells){
            neighborCell = randomNeighborCellWithWallsUp(currentCell);
            if(neighborCell == null){
                currentCell = stack.pop();
            }else{
                removeWall(currentCell, neighborCell);
                stack.push(currentCell);
                currentCell = neighborCell;
                visitedCells++;
            }
        }
Das array könnte man etwa so initialisieren:
Code:
       array = new Cell[höhe][breite];
        for (int y1 = 0; y1 < array.length; y1++) {
            for (int x1 = 0; x1 < array[y1].length; x1++) {
                wall = new Directions(true, true, true, true);
                border = new Directions(false, false, false, false);
                if(y1 == 0) border.setNorth(true);
                if(y1 == höhe-1) border.setSouth(true);
                if(x1 == 0) border.setWest(true);
                if(x1 == breite-1) border.setEast(true);
                coordinates = new Coordinates(y1, x1);
                array[y1][x1] = new Cell(border, wall, coordinates);
            }
        }
Und eine Zelle könnte man so malen:
Code:
   protected void paintComponent(final Graphics g) {
        super.paintComponent(g);
        if(cell.wall.isNorth()) g.drawLine(0, 0, cellWidth, 0);
        if(cell.wall.isEast()) {
            if(cell.border.isEast()){
                g.drawLine(cellWidth-1, 0, cellWidth-1, cellHeight);
            }else{
                g.drawLine(cellWidth, 0, cellWidth, cellHeight);
            }
        }
        if(cell.wall.isSouth()) {
            if(cell.border.isSouth()){
                g.drawLine(0, cellHeight-1, cellWidth, cellHeight-1);
            }else{
                g.drawLine(0, cellHeight, cellWidth, cellHeight);
            }
        }
        if(cell.wall.isWest()) g.drawLine(0, 0, 0, cellHeight);
    }
Ansonsten ist eigentlich nicht viel Besonderes dabei.
http://www.java-forum.org/de/userfiles/user3690/Labyrinth.jar
 
Könnte vielleicht jemand die Methode zum suchen der Nachbarzellen mit 4 intakten Wänden und daraus muss zufällig eine gewählt werden.
Und die Methode zum löschen der Wand zwischen der Zufälligen Zelle mit den intakten Wänden und der aktuellen Zelle.
Wäre echt nett, sind am verzweifeln
 

André Uhres

Top Contributor
Code:
   private  Cell randomNeighborCellWithWallsUp(final Cell currentCell) {
        int x = currentCell.coordinates.getX();
        int y = currentCell.coordinates.getY();
        List<Cell> neighborCells = new ArrayList<Cell>();
        if(y > 0 && array[y-1][x].allWallsUp())
            neighborCells.add(array[y-1][x]);
        if(x < breite-1 && array[y][x+1].allWallsUp())
            neighborCells.add(array[y][x+1]);
        if(y < höhe-1 && array[y+1][x].allWallsUp())
            neighborCells.add(array[y+1][x]);
        if(x > 0 && array[y][x-1].allWallsUp())
            neighborCells.add(array[y][x-1]);
        if(neighborCells.size() == 0) return null;
        int index = Math.abs(new Random().nextInt()) % neighborCells.size();
        return neighborCells.get(index);
    }
    private void removeWall(final Cell currentCell, final Cell neighborCell) {
        int x = currentCell.coordinates.getX();
        int y = currentCell.coordinates.getY();
        int xN = neighborCell.coordinates.getX();
        int yN = neighborCell.coordinates.getY();
        if( y-1 == yN ){
            currentCell.wall.setNorth(false);
            neighborCell.wall.setSouth(false);
        }else if( x+1 == xN){
            currentCell.wall.setEast(false);
            neighborCell.wall.setWest(false);
        }else if( y+1 == yN){
            currentCell.wall.setSouth(false);
            neighborCell.wall.setNorth(false);
        }else if( x-1 == xN){
            currentCell.wall.setWest(false);
            neighborCell.wall.setEast(false);
        }
    }
 
Hi, bist nur über das Forum zu erreichen, oder auch über ICQ?
Aller Besten Dank dafür!!!!!!!!
Müssen eine Umfangreiche Aufgabe erledigen, wo die Bearbeitungszeit für uns ziemlich knapp ist.
Es müssen noch Links-Rechts und Zufallsroboter durch das Labyrinth laufen.
Haben auch da noch nicht so die Idee.
Mal sehen wie weit wir kommen.
 
Problem: Beginnt immer bei (0,0). Es müsste über die Koordinaten der Zelle laufen.
Bekomme es nicht ganz hin. So wird nur ein Quadrat gezeichnet.???

Code:
// protected void paintComponent(final Graphics g) {
//        super.paintComponent(g);
//        if(LabyrinthZellen.wall.isNorth()) g.drawLine(0, 0, cellWidth, 0);
//        if(LabyrinthZellen.wall.isEast()) {
//            if(LabyrinthZellen.border.isEast()){
//                g.drawLine(cellWidth-1, 0, cellWidth-1, cellHeight);
//            }else{
//                g.drawLine(cellWidth, 0, cellWidth, cellHeight);
//            }
//        }
//        if(LabyrinthZellen.wall.isSouth()) {
//            if(LabyrinthZellen.border.isSouth()){
//                g.drawLine(0, cellHeight-1, cellWidth, cellHeight-1);
//            }else{
//                g.drawLine(0, cellHeight, cellWidth, cellHeight);
//            }
//        }
//        if(LabyrinthZellen.wall.isWest()) g.drawLine(0, 0, 0, cellHeight);
//    }
------------------------------------------------------------------------------------------
Werte der Scrolbar vom Dialog werden nicht berücksichtigt.
Habe es mit Action Listener und AdjustmentListener versucht.
------------------------------------------------------------------------------------------
Wie bekomme ich die Methode LabyrinthZeichnen hin.
Was muss übergeben werden?
------------------------------------------------------------------------------------------
 

André Uhres

Top Contributor
onkel hermann hat gesagt.:
Problem: Beginnt immer bei (0,0).
Klar beginnt alles bei null, wo sonst?

onkel hermann hat gesagt.:
Es müsste über die Koordinaten der Zelle laufen.
Schön, läuft ja auch so.

onkel hermann hat gesagt.:
Bekomme es nicht ganz hin.
Aha.

onkel hermann hat gesagt.:
So wird nur ein Quadrat gezeichnet.???
Nein, wenn du alles auskommentierst, dann wird überhaupt nichts gezeichnet.

onkel hermann hat gesagt.:
Werte der Scrolbar vom Dialog werden nicht berücksichtigt.
Wovon sprichst du jetzt?

onkel hermann hat gesagt.:
Habe es mit Action Listener und AdjustmentListener versucht.
Und jetzt?

onkel hermann hat gesagt.:
Wie bekomme ich die Methode LabyrinthZeichnen hin.
Durchlauf das Labyrintharray und bring die Zellkomponenten zur Anzeigefläche.

onkel hermann hat gesagt.:
Was muss übergeben werden?
Das Labyrinth und das Anzeigepanel.
 
Habe am Anfang ein Dialog Fenster, wo ich Höhe, Breite, Wandwahrscheinlichkeit und eine Verzögerung per Scrollbar eingeben kann.
Nach den Einstellungen drücke ich ein Start Button, doch die Einstellungen werden nicht berücksichtigt bei der Labyrintherstellung.
 
Status
Nicht offen für weitere Antworten.

Neue Themen


Oben