Applied encapsulation principle to TurnBasedBame.java

This commit is contained in:
2025-10-29 17:37:21 +01:00
parent 6811890531
commit d7e370536e
4 changed files with 6 additions and 7 deletions

View File

@@ -109,7 +109,7 @@ public class Connect4 extends TurnBasedGame {
} }
private char getCurrentValue() { private char getCurrentValue() {
return currentTurn == 0 ? 'X' : 'O'; return this.getCurrentTurn() == 0 ? 'X' : 'O';
} }
} }

View File

@@ -1,9 +1,8 @@
package org.toop.game; package org.toop.game;
public abstract class TurnBasedGame extends Game { public abstract class TurnBasedGame extends Game {
public final int turns; private final int turns;
private int currentTurn;
protected int currentTurn;
protected TurnBasedGame(int rowSize, int columnSize, int turns) { protected TurnBasedGame(int rowSize, int columnSize, int turns) {
super(rowSize, columnSize); super(rowSize, columnSize);

View File

@@ -50,7 +50,7 @@ public final class Reversi extends TurnBasedGame {
public Move[] getLegalMoves() { public Move[] getLegalMoves() {
final ArrayList<Move> legalMoves = new ArrayList<>(); final ArrayList<Move> legalMoves = new ArrayList<>();
char[][] boardGrid = makeBoardAGrid(); char[][] boardGrid = makeBoardAGrid();
char currentPlayer = (currentTurn==0) ? 'B' : 'W'; char currentPlayer = (this.getCurrentTurn()==0) ? 'B' : 'W';
Set<Point> adjCell = getAdjacentCells(boardGrid); Set<Point> adjCell = getAdjacentCells(boardGrid);
for (Point point : adjCell){ for (Point point : adjCell){
Move[] moves = getFlipsForPotentialMove(point,boardGrid,currentPlayer); Move[] moves = getFlipsForPotentialMove(point,boardGrid,currentPlayer);
@@ -176,7 +176,7 @@ public final class Reversi extends TurnBasedGame {
} }
public char getCurrentPlayer() { public char getCurrentPlayer() {
if (currentTurn == 0){ if (this.getCurrentTurn() == 0){
return 'B'; return 'B';
} }
else { else {

View File

@@ -98,6 +98,6 @@ public final class TicTacToe extends TurnBasedGame {
} }
private char getCurrentValue() { private char getCurrentValue() {
return currentTurn == 0 ? 'X' : 'O'; return this.getCurrentTurn() == 0 ? 'X' : 'O';
} }
} }