Hallo, ich möchte in einem Array Fußgänger simulieren die sich sich bei jedem Takt bewegen. Meine Console gibt aber ziemlich seltsame Sachen aus bzw es verschiebt die fußgänger(als 1 modelliert) nicht nur sonder es füllt praktisch mein ganzes array mit fußgängern. hier mein code:
Java:
public class Hauptklasse {
public static char[][] array;
public static char[][] temp;
public static Random random = new Random();
public static int y; // für den random
/**
* array wird aus array.tvt vom desktop geladen
*
* [USER=49078]@Return[/USER]
*/
public static char[][] load() {
String temp;
try {
Scanner sc = new Scanner(new BufferedReader(new FileReader("C:\\Users\\Lukas\\Desktop\\breite.txt")));
int a = sc.nextInt();
array = new char[a][a];
sc = new Scanner(new BufferedReader(new FileReader("C:\\Users\\Lukas\\Desktop\\array.txt")));
while (sc.hasNextLine()) {
for (int i = 0; i < array.length; i++) {
String[] line = sc.nextLine().trim().split(" ");
for (int j = 0; j < line.length; j++) {
temp = line[j];
array[i][j] = temp.charAt(0);
}
}
}
sc.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return array;
}
/**
* Array wird ausgegeben
*/
public static void ausgabe() {
for (int j = 0; j < array[5].length; j++) {
System.out.print(array[5][j]);
}
System.out.println();
}
public static void neuerTakt() {
temp = array;
y = random.nextInt(2);
for (int i = 1; i < array.length - 1; i++) {
if (array[5][i] == '1') {
if (array[5][i + 1] == '.') {
temp[5][i + 1] = '1';
}
if (array[5][i + 1] == '+') {
temp[5][i + 2] = '1';
}
if (array[5][i - 1] == '.') {
temp[5][i] = '.';
}
}
if (array[5][0] == '1') {
temp[5][1] = '1';
temp[5][0] = '.';
}
if (y == 0) {
temp[5][0] = '1';
}
if (array[5][array.length - 1] == '1') {
temp[5][array.length - 1] = '.';
}
array = temp;
ausgabe();
}
}
public static void main(String[] args) {
load();
for (int i = 0; i < 50; i++) {
neuerTakt();
ausgabe();
System.out.println();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
Zuletzt bearbeitet von einem Moderator: