From 358938422603efd9d3170687caf7ae0cb51b65fa Mon Sep 17 00:00:00 2001 From: Steffen Helmke Date: Fri, 14 Jan 2022 18:02:15 +0100 Subject: [PATCH] 4gewinnt: calcScore pointsForAnyColumn --- src/main/java/de/tims/viergewinnt/ai/Logic.java | 12 +++++++++--- src/test/java/de/tims/viergewinnt/ai/LogicTest.java | 9 ++++++++- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/src/main/java/de/tims/viergewinnt/ai/Logic.java b/src/main/java/de/tims/viergewinnt/ai/Logic.java index 63ecf74..6e082ba 100644 --- a/src/main/java/de/tims/viergewinnt/ai/Logic.java +++ b/src/main/java/de/tims/viergewinnt/ai/Logic.java @@ -160,9 +160,15 @@ public class Logic { score += 60; } } - if(playfield[0][0] != 0 && playfield[1][0] != 0 && playfield[2][0] != 0 && - playfield[3][0] != 0 && playfield[4][0] != 0 && playfield[5][0] != 0) { - score += 60; + for(int i = 0; i < playfield[0].length; i++) { + for(int j = 0; j < playfield.length; j++) { + if(playfield[j][i] == 0) { + break; + } + if(j == playfield.length - 1) { + score += 60; + } + } } return score; } diff --git a/src/test/java/de/tims/viergewinnt/ai/LogicTest.java b/src/test/java/de/tims/viergewinnt/ai/LogicTest.java index 49a5794..c6efc6d 100644 --- a/src/test/java/de/tims/viergewinnt/ai/LogicTest.java +++ b/src/test/java/de/tims/viergewinnt/ai/LogicTest.java @@ -143,12 +143,19 @@ class LogicTest { {2, 0, 0, 0, 0, 0}, {1, 0, 0, 0, 0, 0}, {1, 0, 0, 0, 0, 0}}; + int[][] playfield5 = {{2, 0, 1, 0, 0, 0}, + {2, 0, 1, 0, 0, 0}, + {1, 0, 2, 0, 0, 0}, + {2, 0, 2, 0, 0, 0}, + {1, 0, 2, 0, 0, 0}, + {1, 0, 1, 0, 0, 0}}; return Stream.of( Arguments.of("emptyField", new int[6][6], 1, 1000), Arguments.of("9 chips played", playfield1, 1, 1090), Arguments.of("first row filled", playfield2, 1, 1120), Arguments.of("2 rows filled", playfield3, 1, 1240), - Arguments.of("first column filled", playfield4, 1, 1120) + Arguments.of("first column filled", playfield4, 1, 1120), + Arguments.of("2 column filled", playfield5, 1, 1240) ); }