Browse Source

4gewinnt: testForWin diagonal right win

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

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

@ -24,6 +24,24 @@ public class Logic {
if(chain == 4) return true;
}
}
chain = 0;
for(int i = 0; i < playfield.length - 3; i++) {
for(int j = 0; j < playfield[i].length; j++) {
if(playfield[i][j] == player) {
chain++;
while(true) {
i++; j++;
if(playfield[i][j] == player) {
chain++;
if(chain == 4) return true;
} else {
chain = 0;
break;
}
}
}
}
}
return false;
}
}

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

@ -47,11 +47,18 @@ class LogicTest {
{0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0},
{1, 1, 1, 1, 0, 0}};
int[][] playfield4 = {{0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0},
{1, 0, 0, 0, 0, 0},
{1, 1, 0, 0, 0, 0},
{2, 2, 1, 0, 0, 0},
{2, 2, 2, 1, 0, 0}};
return Stream.of(
Arguments.of("emptyField", new int[6][6], 1, false),
Arguments.of("4 in column1 player1", playfield1, 1, true),
Arguments.of("4 in a column player1", playfield2, 1, true),
Arguments.of("4 in a row player1", playfield3, 1, true)
Arguments.of("4 in a row player1", playfield3, 1, true),
Arguments.of("4 diagonal right player1", playfield4, 1, true)
);
}

Loading…
Cancel
Save