Wie befülle ich abwechselnd ein 2D - char Array?
Ich würde gerne ein Schachbrett in Java nachbilden (mithilfe 2D arrays), dabei scheitere ich aber beim abwechselnden befüllen des char arrays (schwarz, weiß,...).
Endergebnis:
s... (schwarz)
w... (weiß)
w s w s w s w s
s w s w s w s w
w s w s w s w s
s w s w s w s w
1 Antwort
char[][] brett = new char[8][8];
public void fuellen() {
var b = false;
for(int i = 0; i < 8; i++){
for(int j = 0; j < 8; j++){
brett[j][i] = (b = !b) ? 'w' : 's';
//System.out.println(i + " " + j + " " + brett[j][i]); zum Test
}
b = !b;
}
}
Woher ich das weiß:Hobby