From bf73c5dc96b5eb7e12ecda2bb896e21cb3155f70 Mon Sep 17 00:00:00 2001 From: Steffen Helmke Date: Wed, 5 Jan 2022 15:01:25 +0100 Subject: [PATCH] 4gewinnt: testForWin column1 player1 check --- src/main/java/de/tims/viergewinnt/ai/Logic.java | 9 +++++++++ src/test/java/de/tims/viergewinnt/ai/LogicTest.java | 9 ++++++++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/main/java/de/tims/viergewinnt/ai/Logic.java b/src/main/java/de/tims/viergewinnt/ai/Logic.java index dbb4924..e6ff8e0 100644 --- a/src/main/java/de/tims/viergewinnt/ai/Logic.java +++ b/src/main/java/de/tims/viergewinnt/ai/Logic.java @@ -2,6 +2,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; + } + 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 010fcd6..f6f88d8 100644 --- a/src/test/java/de/tims/viergewinnt/ai/LogicTest.java +++ b/src/test/java/de/tims/viergewinnt/ai/LogicTest.java @@ -29,8 +29,15 @@ class LogicTest { } private static Stream playfieldResults() { + int[][] playfield1 = {{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, 0, 0, 0}}; 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) ); }