G
Gelöschtes Mitglied 2635
Gast
Hallo zusammen.
Ein "Labyrinth" ist in einer Textdatei gegeben, der Ausgang soll mittels rekursivem Algorithmus gefunden werden, doch das ist ertsmal nicht das Problem. Ich möchte die Textdatei ("X" steht für Wand, " " steht für Weg) in ein Array, so breit wie das "Labyrinth" (10x10) einlesen, doch bisher bin ich kläglich gescheitert. Wer weiß Rat?
Danke für Hilfe, Der Tino.
lab1.txt hat z.B. diese Form:
Ein "Labyrinth" ist in einer Textdatei gegeben, der Ausgang soll mittels rekursivem Algorithmus gefunden werden, doch das ist ertsmal nicht das Problem. Ich möchte die Textdatei ("X" steht für Wand, " " steht für Weg) in ein Array, so breit wie das "Labyrinth" (10x10) einlesen, doch bisher bin ich kläglich gescheitert. Wer weiß Rat?
Danke für Hilfe, Der Tino.
Code:
import java.io.*;
public class Labyrinth {
final static char[][] lab = new char[10][10];
static void loadLab(String file)
throws IOException, ArrayIndexOutOfBoundsException {
/**
* Labyrinth aus Datei laden und in ein Array speichern
*/
int x=0;
int y=0;
FileReader fr = new FileReader(file);
for ( int c; ( c = fr.read() ) != -1; ) {
if (x<10 && y<10) {
lab[x][y]=(char)c;
y++;
if (y==10) {
y=0;
x++;
}
}
}
fr.close();
}
static void showLab() {
/**
* Ausgabe des Labyrinths aus dem Array
*/
for (int x=0; x<10; x++) {
for (int y=0; y<10; y++) {
System.out.print(lab[x][y]);
}
System.out.println();
}
}
public static void main(String[] args) {
/**
* Test implementierter Methoden
*/
try {
loadLab("lab1.txt");
showLab();
} catch(IOException e) {
System.out.println(e);
} catch(ArrayIndexOutOfBoundsException e2) {
System.out.println(e2);
}
}
}
lab1.txt hat z.B. diese Form:
Code:
XXXXXXXXXX
XXXXXXX
XX XXXXXXX
XX XXXXXXX
XX XXXXXXX
XX XXXXX
XXXXXXXXXX
XXXXXXXXXX
XXXXXXXXXX
XXXXXXXXXX