Browse Source

tictactoe: refactored productive code

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

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

@ -2,8 +2,10 @@ package de.tims.tictactoe;
public class GameLogic { public class GameLogic {
private static final char PLAYER_1 = 'x';
private static final char PLAYER_2 = 'o';
private char[][] board; private char[][] board;
private final char[] occupiedFields = { 'x', 'o' };
private final char[] occupiedFields = { PLAYER_1, PLAYER_2 };
public GameLogic(int size) { public GameLogic(int size) {
if (size < 3) { if (size < 3) {
@ -44,7 +46,7 @@ public class GameLogic {
} }
public boolean checkForWin(char player) { public boolean checkForWin(char player) {
boolean finished = false;
boolean won = false;
int countFields = 0; int countFields = 0;
// check columns // check columns
@ -54,7 +56,7 @@ public class GameLogic {
countFields++; countFields++;
} }
if (countFields == this.board.length) if (countFields == this.board.length)
return finished = true;
return won = true;
countFields = 0; countFields = 0;
} }
// check rows // check rows
@ -64,7 +66,7 @@ public class GameLogic {
countFields++; countFields++;
} }
if (countFields == this.board.length) if (countFields == this.board.length)
return finished = true;
return won = true;
countFields = 0; countFields = 0;
} }
@ -74,7 +76,7 @@ public class GameLogic {
countFields++; countFields++;
} }
if (countFields == this.board.length) if (countFields == this.board.length)
return finished = true;
return won = true;
countFields = 0; countFields = 0;
// check diagonal right // check diagonal right
@ -83,15 +85,13 @@ public class GameLogic {
countFields++; countFields++;
} }
if (countFields == this.board.length) if (countFields == this.board.length)
return finished = true;
return won = true;
return finished;
return won;
} }
public boolean checkEndOfGame() { public boolean checkEndOfGame() {
if (checkForWin('x')) return true;
if (checkForWin('o')) return true;
return false;
return checkForWin(PLAYER_1) || checkForWin(PLAYER_2);
} }
} }
Loading…
Cancel
Save