Browse Source

tictactoe: refactoring: formatted code and removed unused lines

tictactoe
Malte Schellhardt 2 years ago
committed by Lorenz Hohmann
parent
commit
864906fb98
  1. 36
      src/main/java/de/tims/tictactoe/GameLogic.java

36
src/main/java/de/tims/tictactoe/GameLogic.java

@ -31,12 +31,14 @@ public class GameLogic {
}
public void setField(int column, int row, char player) {
if(fieldIsEmpty(column, row)) this.board[column][row] = player;
if (fieldIsEmpty(column, row))
this.board[column][row] = player;
}
public boolean fieldIsEmpty(int column, int row) {
for (char field : this.occupiedFields) {
if (this.board[column][row] == field) return false;
if (this.board[column][row] == field)
return false;
}
return true;
}
@ -48,34 +50,40 @@ public class GameLogic {
// check columns
for (int i = 0; i < this.board.length; i++) {
for (int j = 0; j < this.board[0].length; j++) {
if(board[j][i] == player) countFields++;
if (this.board[j][i] == player)
countFields++;
}
if(countFields == this.board.length) return finished = true;
if (countFields == this.board.length)
return finished = true;
countFields = 0;
}
countFields = 0;
// check rows
for (int i = 0; i < this.board[0].length; i++) {
for (int j = 0; j < this.board.length; j++) {
if(board[i][j] == player) countFields++;
if (this.board[i][j] == player)
countFields++;
}
if(countFields == this.board.length) return finished = true;
if (countFields == this.board.length)
return finished = true;
countFields = 0;
}
// check diagonal left
for (int i = this.board.length - 1, j = this.board.length - 1; i >= 0 ; i--, j--) {
if (board[i][j] == player) countFields++;
for (int i = this.board.length - 1, j = this.board.length - 1; i >= 0; i--, j--) {
if (this.board[i][j] == player)
countFields++;
}
if(countFields == this.board.length) return finished = true;
if (countFields == this.board.length)
return finished = true;
countFields = 0;
// check diagonal right
for (int i = this.board.length - 1, j = 0; i >= 0 ; i--, j++) {
if (board[i][j] == player) countFields++;
for (int i = this.board.length - 1, j = 0; i >= 0; i--, j++) {
if (this.board[i][j] == player)
countFields++;
}
if(countFields == this.board.length) return finished = true;
if (countFields == this.board.length)
return finished = true;
return finished;
}

Loading…
Cancel
Save