From caec8e73d7b57d56511e0acf9c33827ffe5c539a Mon Sep 17 00:00:00 2001 From: Steffen Helmke Date: Wed, 2 Feb 2022 14:14:24 +0100 Subject: [PATCH] 4gewinnt: finished getFreePlaceMethod --- src/main/java/de/tims/viergewinnt/ai/Logic.java | 8 ++++---- src/test/java/de/tims/viergewinnt/ai/LogicTest.java | 8 ++++++++ 2 files changed, 12 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 a20f7ce..fa0a461 100644 --- a/src/main/java/de/tims/viergewinnt/ai/Logic.java +++ b/src/main/java/de/tims/viergewinnt/ai/Logic.java @@ -59,11 +59,11 @@ public class Logic { } public int getFreePlace(int column) { - if(getField(getBoardSize() - 1, column) == 0) { - return getBoardSize() - 1; - } else { - return getBoardSize() - 2; + int size = getBoardSize() - 1; + while(getField(size, column) != 0) { + size--; } + return size; } public int playChip(int column) { diff --git a/src/test/java/de/tims/viergewinnt/ai/LogicTest.java b/src/test/java/de/tims/viergewinnt/ai/LogicTest.java index f77859e..6ddeefa 100644 --- a/src/test/java/de/tims/viergewinnt/ai/LogicTest.java +++ b/src/test/java/de/tims/viergewinnt/ai/LogicTest.java @@ -199,6 +199,14 @@ class LogicTest { assertEquals(testObj.getBoardSize() - 2, testObj.getFreePlace(0)); } + @Test + void getFreePlaceTestWithAnyField() { + testObj.setField(1, testObj.getBoardSize() - 1, 0); + testObj.setField(1, testObj.getBoardSize() - 2, 0); + + assertEquals(testObj.getBoardSize() - 3, testObj.getFreePlace(0)); + } + private static Stream playfieldResults() { int[][] playfield1 = {{0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0},