From 2639a99f673ef5299583d76e91cd0620e69d5d57 Mon Sep 17 00:00:00 2001 From: Steffen Helmke Date: Wed, 5 Jan 2022 17:56:00 +0100 Subject: [PATCH] 4gewinnt: testForWin diagonal right win --- .../java/de/tims/viergewinnt/ai/Logic.java | 18 ++++++++++++++++++ .../java/de/tims/viergewinnt/ai/LogicTest.java | 9 ++++++++- 2 files changed, 26 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 9041a0b..eb5afa5 100644 --- a/src/main/java/de/tims/viergewinnt/ai/Logic.java +++ b/src/main/java/de/tims/viergewinnt/ai/Logic.java @@ -24,6 +24,24 @@ public class Logic { if(chain == 4) return true; } } + chain = 0; + for(int i = 0; i < playfield.length - 3; i++) { + for(int j = 0; j < playfield[i].length; 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; } } diff --git a/src/test/java/de/tims/viergewinnt/ai/LogicTest.java b/src/test/java/de/tims/viergewinnt/ai/LogicTest.java index 11a655f..091ff8c 100644 --- a/src/test/java/de/tims/viergewinnt/ai/LogicTest.java +++ b/src/test/java/de/tims/viergewinnt/ai/LogicTest.java @@ -47,11 +47,18 @@ class LogicTest { {0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0}, {1, 1, 1, 1, 0, 0}}; + int[][] playfield4 = {{0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0}, + {1, 0, 0, 0, 0, 0}, + {1, 1, 0, 0, 0, 0}, + {2, 2, 1, 0, 0, 0}, + {2, 2, 2, 1, 0, 0}}; return Stream.of( Arguments.of("emptyField", new int[6][6], 1, false), Arguments.of("4 in column1 player1", playfield1, 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) ); }