Browse Source

4gewinnt: testForWin diagonal left win

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

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

@ -26,7 +26,7 @@ public class Logic {
} }
chain = 0; chain = 0;
for(int i = 0; i < playfield.length - 3; i++) { for(int i = 0; i < playfield.length - 3; i++) {
for(int j = 0; j < playfield[i].length; j++) {
for(int j = 0; j < playfield[i].length - 3; j++) {
if(playfield[i][j] == player) { if(playfield[i][j] == player) {
chain++; chain++;
while(true) { while(true) {
@ -42,6 +42,24 @@ public class Logic {
} }
} }
} }
chain = 0;
for(int i = 0; i < playfield.length - 3; i++) {
for(int j = playfield[i].length - 1; j > 2; 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; return false;
} }
} }

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

@ -53,12 +53,19 @@ class LogicTest {
{1, 1, 0, 0, 0, 0}, {1, 1, 0, 0, 0, 0},
{2, 2, 1, 0, 0, 0}, {2, 2, 1, 0, 0, 0},
{2, 2, 2, 1, 0, 0}}; {2, 2, 2, 1, 0, 0}};
int[][] playfield5 = {{0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 1},
{0, 0, 0, 0, 1, 2},
{0, 0, 0, 1, 2, 2},
{0, 0, 1, 1, 2, 2}};
return Stream.of( return Stream.of(
Arguments.of("emptyField", new int[6][6], 1, false), Arguments.of("emptyField", new int[6][6], 1, false),
Arguments.of("4 in column1 player1", playfield1, 1, true), Arguments.of("4 in column1 player1", playfield1, 1, true),
Arguments.of("4 in a column player1", playfield2, 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)
Arguments.of("4 diagonal right player1", playfield4, 1, true),
Arguments.of("4 diagonal left player1", playfield5, 1, true)
); );
} }

Loading…
Cancel
Save