Browse Source

4gewinnt: calcScore refactored

4gewinnt
Steffen Helmke 3 years ago
committed by Lorenz Hohmann
parent
commit
9f99538406
  1. 10
      src/main/java/de/tims/viergewinnt/ai/Logic.java
  2. 20
      src/test/java/de/tims/viergewinnt/ai/LogicTest.java

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

@ -147,7 +147,7 @@ public class Logic {
return false;
}
public int calcScore(int[][] playfield, int player) {
public int calcScore(int[][] playfield) {
int score = 1000;
for(int i = 0; i < playfield.length; i++) {
int oldScore = score;
@ -156,8 +156,8 @@ public class Logic {
score += 10;
}
}
if(score - 60 == oldScore) {
score += 60;
if(score - (10 * playfield[0].length) == oldScore) {
score += (10 * playfield[0].length);
}
}
for(int i = 0; i < playfield[0].length; i++) {
@ -166,11 +166,11 @@ public class Logic {
break;
}
if(j == playfield.length - 1) {
score += 60;
score += (10 * playfield.length);
}
}
}
if(score == 2080) {
if(score == (1000 + 3 * (10 * playfield.length * playfield[0].length))) {
score += 500;
}
return score;

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

@ -68,11 +68,11 @@ class LogicTest {
assertEquals(expectedResult, realResult);
}
@ParameterizedTest(name = "[{index}] {0} Win for Player {2} {1}")
@ParameterizedTest(name = "[{index}] {0} with {1}")
@MethodSource("playfieldForScore")
void calcScoreTest(String testName, int[][] playfield, int player, int expectedResult) {
void calcScoreTest(String testName, int[][] playfield, int expectedResult) {
int realResult = testObj.calcScore(playfield, player);
int realResult = testObj.calcScore(playfield);
assertEquals(expectedResult, realResult);
}
@ -156,13 +156,13 @@ class LogicTest {
{2, 1, 2, 2, 1, 2},
{2, 1, 2, 2, 1, 2}};
return Stream.of(
Arguments.of("emptyField", new int[6][6], 1, 1000),
Arguments.of("9 chips played", playfield1, 1, 1090),
Arguments.of("first row filled", playfield2, 1, 1120),
Arguments.of("2 rows filled", playfield3, 1, 1240),
Arguments.of("first column filled", playfield4, 1, 1120),
Arguments.of("2 column filled", playfield5, 1, 1240),
Arguments.of("fullyFilled", playfield6, 1, 2580)
Arguments.of("emptyField", new int[6][6], 1000),
Arguments.of("9 chips played", playfield1, 1090),
Arguments.of("first row filled", playfield2, 1120),
Arguments.of("2 rows filled", playfield3, 1240),
Arguments.of("first column filled", playfield4, 1120),
Arguments.of("2 column filled", playfield5, 1240),
Arguments.of("fullyFilled", playfield6, 2580)
);
}

Loading…
Cancel
Save