Browse Source

tictactoe: refactoring: formatted code and removed unused lines

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

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

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

Loading…
Cancel
Save