Ich verstehe nicht ganz warum ich, wenn ich versuche eine outOfBoundsException bekomme, da ich doch die Größe des Arrays aus einer Datei einlese und dann setze 🙁
Das ist die getestete Datei:
Und das ist meine Klasse:
Das ist die getestete Datei:
Code:
4
3
O#O
OO#
#OO
#O#
Und das ist meine Klasse:
Java:
import java.io.*;
import java.util.Arrays;
public class Labyrinth {
private int playerpositionx;
private int playerpositiony;
private int arrayWidth;
private int arrayLength;
private boolean [][] gameboard = new boolean[arrayLength][arrayWidth];
Labyrinth(String filename) throws IOException{
int counter = 0, counter2 = 0, checking = 0;
BufferedReader objReader = null;
//try {
String strCurrentLine;
objReader = new BufferedReader(new FileReader(filename));
while ((strCurrentLine = objReader.readLine()) != null) {
System.out.println("Checking: " + checking);
System.out.println("Counter Vertikal: " + counter);
System.out.println("Counter Horizontal: " + counter2);
//System.out.println(Arrays.toString(gameboard));
if(checking == 0) {
arrayLength = Integer.parseInt(strCurrentLine);
System.out.println("Length: " + arrayLength);
checking++;
}else if(checking == 1){
arrayWidth = Integer.parseInt(strCurrentLine);
System.out.println("Width: " + arrayWidth);
checking++;
}else {
for(int i = 0; i < strCurrentLine.length(); i++) {
if(strCurrentLine.charAt(i) == 'O') {
gameboard[counter][counter2] = true; //<-----
counter2++;
}
if(strCurrentLine.charAt(i) == '#') {
gameboard[counter][counter2] = false; //<-----
counter2++;
}
}
counter++;
counter2 = 0;
}
System.out.println(strCurrentLine);
}
arrayPrinter(gameboard);
for(int i = 0; i < gameboard[0].length; i++) {
if(gameboard[0][i] == true) {
this.playerpositionx = i;
this.playerpositiony = 0;
break;
}
}
System.out.println();
System.out.println("Die Spielerposition ist: (" + playerpositionx + "|" + playerpositiony + ")");
System.out.println("Das ist die Länge des Arrays: " + gameboard.length);
}
public boolean bewegeDich(char symbol){
switch(symbol) {
case 'o':
if(playerpositiony == 0) {return false;}
if(gameboard[playerpositiony-1][playerpositionx] == false){return false;}
else {return true;}
//break;
case 'u':
if((gameboard.length - playerpositiony) < 0) {return false; }
if(gameboard[playerpositiony+1][playerpositionx] == false){return false;}
else { return true;}
//break;
case 'l':
if((gameboard[0].length - playerpositionx) < 0) {return false;}
if(gameboard[playerpositiony][playerpositionx-1] == false){return false;}
else {return true;}
//break;
case 'r':
if((gameboard[0].length - playerpositionx) < 0) {return false;}
if(gameboard[playerpositiony][playerpositionx+1] == false){return false;}
else {return true;}
//break;
}
return false;
}
public void arrayPrinter(boolean [][] c) {
for(boolean[] l: c) {
for(boolean m: l) {
System.out.print(m + " | ");
}
System.out.println();
}
}
@Override
public String toString() {
String result = "";
for(int i = 0; i < gameboard.length; i++) {
for(int j = 0; j < gameboard[i].length; j++) {
if(gameboard[i][j] == true && i != playerpositiony && j != playerpositionx) {
result += "O";
}else if(gameboard[i][j] == true && i == playerpositiony && j == playerpositionx){
result += "x";
}else if(gameboard[i][j] == false) {
result += "#";
}
}
result += "\n";
}
return result;
}
}