Ich habe eine Übungsaufgabe (https://wiki.freitagsrunde.org/Javakurs/Übungsaufgaben/Doom) im Internet gefunden und wollte die Lösen.
Auch alles super, ist ja wirklich eine sehr einfache Aufgabe. ABER ich kriege an einer Stelle große Probleme.
Explizit geht es dabei um die Aufgabe " Das GameField"
Wenn ich in der convert Method mir jedes einzelne Zeichen ausgebe kommt die richtige Ausgabe herraus, aber in der toString methode sind immer leerzeichen etc. dazwischen die dort nicht sein sollten. ( Ich denke das liegt daran das das array “falsch herrum” durchgangen wird)
EDIT: Zwischen den Rauten sind mehrfache Leerzeichen, in der Konsole aus eclipse sichtbar; Hier aber nicht.
convert Ausgabe: ######### ## #### ## #########
falsche Ausgabe: ####### # ## # ## ## ## ## #######
import visual.GameField;
public class GameMain {
public static void main(String[] args) {
String s = "########" +
"# #" +
"# #" +
"### #" +
"# #" +
"########";
new GameField(s,8);
}
}
import data.GameTile;
public class GameField {
private GameTile[][] field;
private int width;
public GameField(String ascii, int width) {
this.width = width;
this.convertStringInTiles(ascii);
this.toString();
}
@Override
public String toString() {
String ascii_field = "";
for (int i = 0; i < field.length; i++) {
for (int a = 0; a < field**.length; a++) {
System.out.print(field**[a]);
}
System.out.println("");
}
return ascii_field;
}
private void convertStringInTiles(String string_field) {
field = new GameTile[width][string_field.length() / width];
int counterX = 0;
int counterY = 0;
for (char c : string_field.toCharArray()) {
if (counterX == width) {
counterY++;
counterX = 0;
}
field[counterX][counterY] = GameTile.getTile(c);
counterX++;
}
}
}
package data;
public class GameTile {
private Position position;
public String toString() {
return " ";
}
public static GameTile getTile(char tileChar) {
if (tileChar == '~')
return new WaterTile();
if (tileChar == '#')
return new WallTile();
if (tileChar == ' ')
return new EmptyTile();
return new EmptyTile();
}
}