From 36b8cdfcacf918476b4bb41b02be0e2d758a48db Mon Sep 17 00:00:00 2001 From: Tobias Krause Date: Mon, 31 Jan 2022 14:15:01 +0100 Subject: [PATCH] tictactoe: countCharsInCol counts number of given char in specific col --- src/main/java/de/tims/tictactoe/ai/AIHard.java | 9 ++++++++- src/test/java/de/tims/tictactoe/ai/AIHardTest.java | 5 ++++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/src/main/java/de/tims/tictactoe/ai/AIHard.java b/src/main/java/de/tims/tictactoe/ai/AIHard.java index ac70681..56ff489 100644 --- a/src/main/java/de/tims/tictactoe/ai/AIHard.java +++ b/src/main/java/de/tims/tictactoe/ai/AIHard.java @@ -68,6 +68,13 @@ public class AIHard implements TicTacToeAI { } public int countCharsInCol(int index, char charToCount) { - return 0; + int count = 0; + char[][] board = gl.getBoard(); + + for (int i = 0; i < BOARD_SIZE; i++) { + count += (board[i][index] == charToCount) ? 1 : 0; + } + + return count; } } diff --git a/src/test/java/de/tims/tictactoe/ai/AIHardTest.java b/src/test/java/de/tims/tictactoe/ai/AIHardTest.java index 76a81f5..f764772 100644 --- a/src/test/java/de/tims/tictactoe/ai/AIHardTest.java +++ b/src/test/java/de/tims/tictactoe/ai/AIHardTest.java @@ -100,7 +100,10 @@ class AIHardTest { private static Stream testCasesForCountCharsInCol() { return Stream.of(Arguments.of("EmptyFieldReturns0", new char[][] { {'-', '-', '-'}, {'-', '-', '-'}, {'-', '-', '-'} }, - 0, 'o', 0)); + 0, 'o', 0), + Arguments.of("TwoCharsInRowReturnsTwo", + new char[][] { {'-', '-', '-'}, {'o', 'o', '-'}, {'-', '-', '-'} }, + 1, 'o', 1)); } }