From 57bc37a4d8e808887d6470c7513aaa693e9a9892 Mon Sep 17 00:00:00 2001 From: Steffen Helmke Date: Wed, 5 Jan 2022 15:14:30 +0100 Subject: [PATCH] 4gewinnt: testForWin win in a column --- src/main/java/de/tims/viergewinnt/ai/Logic.java | 14 ++++++++------ .../java/de/tims/viergewinnt/ai/LogicTest.java | 9 ++++++++- 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/src/main/java/de/tims/viergewinnt/ai/Logic.java b/src/main/java/de/tims/viergewinnt/ai/Logic.java index e6ff8e0..8c62c9f 100644 --- a/src/main/java/de/tims/viergewinnt/ai/Logic.java +++ b/src/main/java/de/tims/viergewinnt/ai/Logic.java @@ -3,13 +3,15 @@ package de.tims.viergewinnt.ai; public class Logic { public boolean testForWin(int[][] playfield, int player) { 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; } diff --git a/src/test/java/de/tims/viergewinnt/ai/LogicTest.java b/src/test/java/de/tims/viergewinnt/ai/LogicTest.java index f6f88d8..2a861ad 100644 --- a/src/test/java/de/tims/viergewinnt/ai/LogicTest.java +++ b/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}}; + 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( 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) ); }