Browse Source

4gewinnt: testForWin win in a row

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

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

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

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

@ -41,10 +41,17 @@ class LogicTest {
{0, 0, 0, 1, 0, 0}, {0, 0, 0, 1, 0, 0},
{0, 0, 0, 1, 0, 0}, {0, 0, 0, 1, 0, 0},
{0, 0, 0, 1, 0, 0}}; {0, 0, 0, 1, 0, 0}};
int[][] playfield3 = {{0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0},
{1, 1, 1, 1, 0, 0}};
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)
); );
} }

Loading…
Cancel
Save