Browse Source

4gewinnt: refactored code with setter/getter for currentPlayer

4gewinnt
Steffen Helmke 3 years ago
committed by Lorenz Hohmann
parent
commit
92f33426d8
  1. 11
      src/main/java/de/tims/viergewinnt/ai/Logic.java
  2. 17
      src/test/java/de/tims/viergewinnt/ai/LogicTest.java

11
src/main/java/de/tims/viergewinnt/ai/Logic.java

@ -32,15 +32,15 @@ public class Logic {
return currentPlayer; return currentPlayer;
} }
public int playChip(int column, int player) {
public int playChip(int column) {
//add board update //add board update
updateGui(); updateGui();
if(testForWin(player)) {
if(testForWin()) {
calcScore(); calcScore();
} }
currentPlayer = (player % 2) + 1;
setCurrentPlayer((getCurrentPlayer() % 2) + 1);
return 0; return 0;
} }
@ -95,7 +95,7 @@ public class Logic {
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
for(int i = 0; i < buttons.length; i++) { for(int i = 0; i < buttons.length; i++) {
if(e.getSource() == buttons[i]) { if(e.getSource() == buttons[i]) {
playChip(i, currentPlayer);
playChip(i);
break; break;
} }
} }
@ -103,7 +103,8 @@ public class Logic {
} }
public boolean testForWin(int player) {
public boolean testForWin() {
int player = getCurrentPlayer();
int chain = 0; int chain = 0;
for(int i = 0; i < board[0].length; i++) { for(int i = 0; i < board[0].length; i++) {
for(int j = 0; j < board.length; j++) { for(int j = 0; j < board.length; j++) {

17
src/test/java/de/tims/viergewinnt/ai/LogicTest.java

@ -27,8 +27,9 @@ class LogicTest {
testObj.setField(playfield[i][j], i, j); testObj.setField(playfield[i][j], i, j);
} }
} }
testObj.setCurrentPlayer(player);
boolean realResult = testObj.testForWin(player);
boolean realResult = testObj.testForWin();
assertEquals(expectedResult, realResult); assertEquals(expectedResult, realResult);
} }
@ -39,7 +40,9 @@ class LogicTest {
int player = 1; int player = 1;
int expectedResult = 0; int expectedResult = 0;
testObj.create4gewinntGui(); testObj.create4gewinntGui();
int realResult = testObj.playChip(column, player);
testObj.setCurrentPlayer(player);
int realResult = testObj.playChip(column);
assertEquals(expectedResult, realResult); assertEquals(expectedResult, realResult);
} }
@ -49,9 +52,10 @@ class LogicTest {
int column = 0; int column = 0;
int player = 1; int player = 1;
int expectedResult = 2; int expectedResult = 2;
testObj.create4gewinntGui(); testObj.create4gewinntGui();
testObj.playChip(column, player);
testObj.setCurrentPlayer(player);
testObj.playChip(column);
int realResult = testObj.currentPlayer; int realResult = testObj.currentPlayer;
assertEquals(expectedResult, realResult); assertEquals(expectedResult, realResult);
@ -62,9 +66,10 @@ class LogicTest {
int column = 0; int column = 0;
int player = 2; int player = 2;
int expectedResult = 1; int expectedResult = 1;
testObj.create4gewinntGui(); testObj.create4gewinntGui();
testObj.playChip(column, player);
testObj.setCurrentPlayer(player);
testObj.playChip(column);
int realResult = testObj.currentPlayer; int realResult = testObj.currentPlayer;
assertEquals(expectedResult, realResult); assertEquals(expectedResult, realResult);

Loading…
Cancel
Save