Hallo liebe Foren Mitglieder,
ich habe ein Problem mit der Initialisierung eines Arrays: Konkret es soll hier ein TickTacToe (Zeile 11) initialisert werden. Die Main Methode schreibt erststellt dann ein neues TicTacToe (Zeile 109) und gibt es dann in Zeile 110 aus. Dabei verwendet es die toString Methode aus Zeile 96 - die auf den initalisierten ( Zeile 12) leeren Array zugreifen sollte. Tut Sie auch nur bekomme ich eine NullPointerException.
Bevor ich hier vollkommen verzweifele würde mir jemand bitte mit einen Hinweis helfen.
Vielen Dank,
Dennis
ich habe ein Problem mit der Initialisierung eines Arrays: Konkret es soll hier ein TickTacToe (Zeile 11) initialisert werden. Die Main Methode schreibt erststellt dann ein neues TicTacToe (Zeile 109) und gibt es dann in Zeile 110 aus. Dabei verwendet es die toString Methode aus Zeile 96 - die auf den initalisierten ( Zeile 12) leeren Array zugreifen sollte. Tut Sie auch nur bekomme ich eine NullPointerException.
Bevor ich hier vollkommen verzweifele würde mir jemand bitte mit einen Hinweis helfen.
Vielen Dank,
Dennis
Java:
package u06;
public class TicTacToe {
public static char[] SPIELER = new char[] { 'X', 'O' };
public static char FREI = ' ';
public static char UNENTSCHIEDEN = '?';
public char[][] brett;
private int istAnDerReihe;
public TicTacToe() {
char[][] brett = new char[2][2];
istAnDerReihe = 0;
}
public boolean istFrei(int zeile, int spalte) {
if (brett[zeile][spalte] > 0) {
return false;
} else
return true;
}
public char getGewinner() {
int summeh1 = brett[0][0] + brett[0][1] + brett[0][2];
int summeh2 = brett[1][0] + brett[1][1] + brett[1][2];
int summeh3 = brett[2][0] + brett[2][1] + brett[2][2];
int summev1 = brett[0][0] + brett[1][0] + brett[2][0];
int summev2 = brett[0][1] + brett[1][1] + brett[2][1];
int summev3 = brett[0][2] + brett[1][2] + brett[2][2];
int summeq1 = brett[0][0] + brett[1][1] + brett[2][2];
int summeq2 = brett[0][2] + brett[1][1] + brett[2][0];
if ((summeh1 == 237) || (summeh2 == 237) || (summeh3 == 237)
|| (summev1 == 237) || (summev2 == 237) || (summev3 == 237)
|| (summeq1 == 237) || (summeq2 == 237)) {
return SPIELER[1];
}
if ((summeh1 == 264) || (summeh2 == 264) || (summeh3 == 264)
|| (summev1 == 264) || (summev2 == 264) || (summev3 == 264)
|| (summeq1 == 264) || (summeq2 == 264)) {
return SPIELER[0];
} else
return UNENTSCHIEDEN;
}
public boolean allesBelegt() {
int counter = 0;
for (int zeile = 0; zeile < brett.length; zeile++) {
for (int spalte = 0; spalte < brett.length; spalte++) {
if (brett[zeile][spalte] > 0) {
counter++;
}
}
}
if (counter == 9) {
return true;
} else
return false;
}
public String ziehen(char spieler, int zeile, int spalte) {
char gewinner = getGewinner();
if (gewinner != UNENTSCHIEDEN || allesBelegt()) {
String meldung = "Das ist Spiel ist beendet, keine Zuege mehr moeglich. ";
if (gewinner == UNENTSCHIEDEN)
meldung += "Niemand hat gewonnen!";
else
meldung += "Spieler " + gewinner + " hat gewonnen!";
return meldung;
} else {
if (SPIELER[istAnDerReihe] == spieler) {
if (istFrei(zeile, spalte)) {
// hier Teil der Spiellogik ergaenzen
} else {
return "Feld (" + zeile + "," + spalte
+ ") ist bereits von Spieler "
+ brett[zeile][spalte] + " belegt!";
}
} else {
return "Spieler " + spieler + " ist nicht an der Reihe!";
}
}
return null;// DEFALUT!
}
@Override
public String toString() {
String ln = System.getProperty("line.separator");
String result = brett[0][0] + " | " + brett[0][1] + " | " + brett[0][2]
+ ln + "-" + "---" + "-" + "---" + "-" + ln + brett[1][0]
+ " | " + brett[1][1] + " | " + brett[1][2] + ln + "-" + "---"
+ "-" + "---" + "-" + ln + brett[2][0] + " | " + brett[2][1]
+ " | " + brett[2][2];
return result;
}
public static void main(String[] args) {
// Spielverlauf 1
System.out.println("Neues Spiel gestartet");
TicTacToe tictactoe = new TicTacToe();
System.out.println(tictactoe);
System.out.println(tictactoe.ziehen('X', 0, 2));
System.out.println(tictactoe);
System.out.println(tictactoe.ziehen('O', 0, 0));
System.out.println(tictactoe);
// Spieler O ist hier nicht an der Reihe
System.out.println(tictactoe.ziehen('O', 0, 0));
System.out.println(tictactoe);
System.out.println(tictactoe.ziehen('X', 2, 0));
System.out.println(tictactoe);
System.out.println(tictactoe.ziehen('O', 1, 1));
System.out.println(tictactoe);
System.out.println(tictactoe.ziehen('X', 2, 2));
System.out.println(tictactoe);
// Spieler O versucht hier zu schummeln, denn Feld (2,2) ist bereits
// belegt
System.out.println(tictactoe.ziehen('O', 2, 2));
System.out.println(tictactoe);
System.out.println(tictactoe.ziehen('O', 1, 2));
System.out.println(tictactoe);
System.out.println(tictactoe.ziehen('X', 2, 1));
System.out.println(tictactoe);
// Zu diesem Zeitpunkt hat Spieler X bereits gewonnen
System.out.println(tictactoe.ziehen('O', 1, 0));
System.out.println(tictactoe);
// Spielverlauf 2
System.out.println();
System.out.println("Neues Spiel gestartet");
tictactoe = new TicTacToe();
System.out.println(tictactoe);
System.out.println(tictactoe.ziehen('X', 0, 1));
System.out.println(tictactoe);
System.out.println(tictactoe.ziehen('O', 2, 0));
System.out.println(tictactoe);
System.out.println(tictactoe.ziehen('X', 2, 2));
System.out.println(tictactoe);
System.out.println(tictactoe.ziehen('O', 0, 2));
System.out.println(tictactoe);
System.out.println(tictactoe.ziehen('X', 1, 1));
System.out.println(tictactoe);
System.out.println(tictactoe.ziehen('O', 2, 1));
System.out.println(tictactoe);
System.out.println(tictactoe.ziehen('X', 1, 2));
System.out.println(tictactoe);
System.out.println(tictactoe.ziehen('O', 1, 0));
System.out.println(tictactoe);
System.out.println(tictactoe.ziehen('X', 0, 0));
System.out.println(tictactoe);
// Spielverlauf 3
System.out.println();
System.out.println("Neues Spiel gestartet");
tictactoe = new TicTacToe();
System.out.println(tictactoe);
// Spieler O ist hier nicht an der Reihe
System.out.println(tictactoe.ziehen('O', 0, 0));
System.out.println(tictactoe);
System.out.println(tictactoe.ziehen('X', 0, 0));
System.out.println(tictactoe);
System.out.println(tictactoe.ziehen('O', 1, 1));
System.out.println(tictactoe);
System.out.println(tictactoe.ziehen('X', 2, 0));
System.out.println(tictactoe);
System.out.println(tictactoe.ziehen('O', 1, 0));
System.out.println(tictactoe);
System.out.println(tictactoe.ziehen('X', 1, 2));
System.out.println(tictactoe);
System.out.println(tictactoe.ziehen('O', 0, 1));
System.out.println(tictactoe);
System.out.println(tictactoe.ziehen('X', 2, 1));
System.out.println(tictactoe);
System.out.println(tictactoe.ziehen('O', 2, 2));
System.out.println(tictactoe);
System.out.println(tictactoe.ziehen('X', 0, 2));
System.out.println(tictactoe);
}
}