Browse Source

4gewinnt: testForWin win in a column

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

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

@ -3,13 +3,15 @@ package de.tims.viergewinnt.ai;
public class Logic { public class Logic {
public boolean testForWin(int[][] playfield, int player) { public boolean testForWin(int[][] playfield, int player) {
int chain = 0; int chain = 0;
for(int i = 0; i < playfield.length; i++) {
if(playfield[i][0] == player) {
chain++;
} else {
chain = 0;
for(int i = 0; i < playfield[0].length; i++) {
for(int j = 0; j < playfield.length; j++) {
if(playfield[j][i] == player) {
chain++;
} else {
chain = 0;
}
if(chain == 4) return true;
} }
if(chain == 4) return true;
} }
return false; return false;
} }

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

@ -35,9 +35,16 @@ class LogicTest {
{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, 0, 0, 0}};
int[][] playfield2 = {{0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 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}};
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 1st column player1", playfield1, 1, true)
Arguments.of("4 in column1 player1", playfield1, 1, true),
Arguments.of("4 in a column player1", playfield2, 1, true)
); );
} }

Loading…
Cancel
Save