From 9029de6c126ac7f07138666d583bdd9b227d5cd8 Mon Sep 17 00:00:00 2001 From: Tobias Krause Date: Mon, 31 Jan 2022 14:08:11 +0100 Subject: [PATCH] tictactoe: countCharsInRow counts number of given char in specific row --- src/main/java/de/tims/tictactoe/ai/AIHard.java | 8 +++++++- src/test/java/de/tims/tictactoe/ai/AIHardTest.java | 7 +++++-- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/src/main/java/de/tims/tictactoe/ai/AIHard.java b/src/main/java/de/tims/tictactoe/ai/AIHard.java index 6891e94..48e7d1b 100644 --- a/src/main/java/de/tims/tictactoe/ai/AIHard.java +++ b/src/main/java/de/tims/tictactoe/ai/AIHard.java @@ -57,6 +57,12 @@ public class AIHard implements TicTacToeAI { } public int countCharsInRow(int index, char charToCount) { - return 0; + int count = 0; + char[][] board = gl.getBoard(); + for (int i = 0; i < BOARD_SIZE; i++) { + count += (board[index][i] == 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 049e093..a35614b 100644 --- a/src/test/java/de/tims/tictactoe/ai/AIHardTest.java +++ b/src/test/java/de/tims/tictactoe/ai/AIHardTest.java @@ -79,8 +79,11 @@ class AIHardTest { private static Stream testCasesForCountCharsInRow() { return Stream.of(Arguments.of("EmptyFieldReturns0", - new char[][] { {'-', '-', '-'}, {'-', '-', '-'}, {'-', '-', '-'} }, - 0, 'o', 0)); + new char[][] { {'-', '-', '-'}, {'-', '-', '-'}, {'-', '-', '-'} }, + 0, 'o', 0), + Arguments.of("TwoCharsInRowReturnsTwo", + new char[][] { {'-', '-', '-'}, {'o', 'o', '-'}, {'-', '-', '-'} }, + 1, 'o', 2)); } }