001    package de.hska.info1.backtracking;
002    
003    import java.awt.event.ActionEvent;
004    import java.awt.event.ActionListener;
005    import java.awt.event.KeyEvent;
006    import java.awt.event.KeyListener;
007    
008    /**
009     * <p>Wegsuche im Labyrinth mit Backtracking.
010     * Diese Implementierung enthält noch viele Anweisungen
011     * zum Zeichnen des Labyrinths und zur Behandlung
012     * der Tastatureingaben.</p>
013     * Für eine übersichtlichere Lösunge bitte hier nachsehen
014     * {@link de.hska.info1.backtracking.labyrinth.Labyrinth}
015     * 
016     * @author Christian Pape
017     */
018    public class Labyrinth implements ActionListener, KeyListener {
019            
020            private static final int NORDEN = 0;
021    
022            private static final int WESTEN = 1;
023    
024            private static final int SUEDEN = 2;
025    
026            private static final int OSTEN = 3;
027    
028            private static final char BESUCHT = '-';
029    
030            private static final char GEHE_WEG = '.';
031    
032            private static final char FREIER_WEG = ' ';
033    
034            private static final char WAND = 'W';
035            
036            private char [] [] labyrinth;
037    
038            private int ausgangX;
039    
040            private int ausgangY;
041            
042            private Ariadne ariadne;
043    
044            private boolean weiter;
045            
046            public Labyrinth(char [] [] labyrinth, int ausgangX, int ausgangY) {
047                    this.labyrinth = labyrinth;
048                    this.ausgangX = ausgangX;
049                    this.ausgangY = ausgangY;
050            }
051            
052            public boolean sucheAusgang(int x, int y) {
053                    for (int i = 0; i < 4; i++) {
054                            int neuesX = x;
055                            int neuesY = y;
056                            switch (i) {
057                                    case NORDEN: neuesY--;
058                                                            break;
059                                    case WESTEN: neuesX--;
060                                                            break;
061                                    case SUEDEN: neuesY++;
062                                                            break;
063                                    case OSTEN: neuesX++;
064                                                            break;
065                            }
066                            ariadne.naechstenSchrittVersuchen(neuesX, neuesY);
067                            warte();
068                            if ( isGueltigerSchritt(neuesX, neuesY)) {
069                                    labyrinth[neuesY][neuesX] = GEHE_WEG;
070                                    ariadne.neuenSchrittGehen(neuesX, neuesY);
071                                    warte();
072                                    if (neuesX != ausgangX || neuesY != ausgangY ) {
073                                            if ( sucheAusgang(neuesX, neuesY)) {
074                                                    return true;
075                                            } else {
076                                                    labyrinth[neuesY][neuesX] = BESUCHT;
077                                                    ariadne.alsBesuchtMarkieren(neuesX, neuesY);
078                                                    warte();
079                                            }
080                                    } else {
081                                            return true;
082                                    }
083                            } else {
084                                    ariadne.altenZustandWiederherstellen(neuesX, neuesY);
085                                    warte();
086                            }
087                    }
088                    
089                    return false;
090            }
091    
092            private boolean isGueltigerSchritt(int neuesX, int neuesY) {
093                    return 0 <= neuesX && neuesX < getBreite()
094                            && 0 <= neuesY && neuesY < getHoehe()
095                            && labyrinth[neuesY][neuesX] == FREIER_WEG;
096            }
097            
098            public void setAriadne(Ariadne ariadne) {
099                    this.ariadne = ariadne;
100            }
101            
102            public int getHoehe() {
103                    return labyrinth.length;
104            }
105            
106            public int getBreite() {
107                    return labyrinth[0].length;
108            }
109            
110            public boolean isWand(int x, int y) {
111                    return labyrinth[y][x] == WAND;
112            }
113            
114            public boolean isFreierWeg(int x, int y) {
115                    return labyrinth[y][x] == FREIER_WEG;           
116            }
117            
118            public boolean isBereitsBesucht(int x, int y) {
119                    return labyrinth[y][x] == BESUCHT;
120            }
121            
122            public boolean isGegangerWeg(int x, int y) {
123                    return labyrinth[y][x] == GEHE_WEG;
124            }
125    
126            public void warte() {
127                    weiter = false;
128                    while (! weiter) {
129                            try {
130                                    Thread.sleep(50);
131                            } catch (InterruptedException e) {
132                                    weiter = true;
133                            }
134                    }
135            }
136            
137            public void actionPerformed(ActionEvent e) {
138                    weiter = true;
139            }
140    
141            public void keyPressed(KeyEvent e) {
142                    weiter = true;
143            }
144    
145            public void keyReleased(KeyEvent e) {
146                    
147            }
148    
149            public void keyTyped(KeyEvent e) {
150                    
151            }
152    
153    }