From c85bc7918dabef1194d6262cef52e3ca13f7d580 Mon Sep 17 00:00:00 2001 From: Malte Schellhardt Date: Mon, 14 Feb 2022 18:29:05 +0100 Subject: [PATCH] tictactoe: refactored productive code --- .../java/de/tims/tictactoe/GameLogic.java | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/main/java/de/tims/tictactoe/GameLogic.java b/src/main/java/de/tims/tictactoe/GameLogic.java index 95aa451..4a7c6d8 100644 --- a/src/main/java/de/tims/tictactoe/GameLogic.java +++ b/src/main/java/de/tims/tictactoe/GameLogic.java @@ -2,8 +2,10 @@ package de.tims.tictactoe; public class GameLogic { + private static final char PLAYER_1 = 'x'; + private static final char PLAYER_2 = 'o'; private char[][] board; - private final char[] occupiedFields = { 'x', 'o' }; + private final char[] occupiedFields = { PLAYER_1, PLAYER_2 }; public GameLogic(int size) { if (size < 3) { @@ -44,7 +46,7 @@ public class GameLogic { } public boolean checkForWin(char player) { - boolean finished = false; + boolean won = false; int countFields = 0; // check columns @@ -54,7 +56,7 @@ public class GameLogic { countFields++; } if (countFields == this.board.length) - return finished = true; + return won = true; countFields = 0; } // check rows @@ -64,7 +66,7 @@ public class GameLogic { countFields++; } if (countFields == this.board.length) - return finished = true; + return won = true; countFields = 0; } @@ -74,7 +76,7 @@ public class GameLogic { countFields++; } if (countFields == this.board.length) - return finished = true; + return won = true; countFields = 0; // check diagonal right @@ -83,15 +85,13 @@ public class GameLogic { countFields++; } if (countFields == this.board.length) - return finished = true; + return won = true; - return finished; + return won; } public boolean checkEndOfGame() { - if (checkForWin('x')) return true; - if (checkForWin('o')) return true; - return false; + return checkForWin(PLAYER_1) || checkForWin(PLAYER_2); } }