Hi, ich soll ein 4 Gewinnt Spiel programmieren, welches überprüft, welcher Spieler das Spiel gewinnt.
Ich steck momentan bei der checkDiagonal() Methode fest, bei der true zurückgegeben werden soll, wenn 4 gleiche Spielsteine diagonal auf einer Linie stehen.
Die checkRow() und checkColumn() Methoden funktionieren:
checkDiagonal() Methode:
Was muss geändert werden?
Ich steck momentan bei der checkDiagonal() Methode fest, bei der true zurückgegeben werden soll, wenn 4 gleiche Spielsteine diagonal auf einer Linie stehen.
Die checkRow() und checkColumn() Methoden funktionieren:
Java:
private boolean checkColumn() {
for (int j = 0; j < 7; ++j) {
int count = 0;
for (int i = 1; i < 7; ++i) {
if (this.board[j] != '-' &&
this.board[j] == this.board[i-1][j]) {
++count;
}
else {
count = 1;
}
if (count >= 4) {
return true;
}
}
}
return false;
}
Java:
private boolean checkRow() {
for (int i = 0; i < 7; ++i) {
int count = 0;
for (int j = 1; j < 7; ++j) {
if (this.board[j] != '-' &&
this.board[j] == this.board[j-1]) {
++count;
}
else {
count = 1;
}
if (count >= 4) {
return true;
}
}
}
return false;
}
checkDiagonal() Methode:
Java:
private boolean checkDiagonal() {
int count = 0;
for (int i = 0; i < 4; i++) {
if (count >= 4) {
break;
}
count = 0;
for (int j = 0; j < 6; j++) {
if (i + j >= 6) {
break;
}
if (this.board[i+j][j] == this.board[i+j+1][j+1]) {
count++;
if (count >= 4) {
break;
}
}
else {
count = 0;
}
}
}
if (count >= 4) {
return true;
}
else {
for (int i = 3; i < 7; i++) {
if (count >= 4) {
break;
}
count = 0;
for (int j = 0; j < 6; j++) {
if (i - j <= 0) {
break;
}
if (this.board[i-j][j] == this.board[i-j-1][j+1]) {
count++;
if (count >= 4) {
break;
}
}
else {
count = 0;
}
}
}
if (count >= 4) {
return true;
}
else {
return false;
}
}
}
Was muss geändert werden?