From cce28aacd088b4020a0967816ef1bd593b646680 Mon Sep 17 00:00:00 2001 From: Tobias Krause Date: Mon, 31 Jan 2022 15:21:02 +0100 Subject: [PATCH] tictactoe: countCharsInDiag counts number of given char in specific diagonal --- 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 df3c16a..6ea1d35 100644 --- a/src/main/java/de/tims/tictactoe/ai/AIHard.java +++ b/src/main/java/de/tims/tictactoe/ai/AIHard.java @@ -79,6 +79,13 @@ public class AIHard implements TicTacToeAI { } public int countCharsInDiag(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 == 0) ? i : BOARD_SIZE - 1 - 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 5fdd8d6..d04e720 100644 --- a/src/test/java/de/tims/tictactoe/ai/AIHardTest.java +++ b/src/test/java/de/tims/tictactoe/ai/AIHardTest.java @@ -120,7 +120,10 @@ class AIHardTest { private static Stream testCasesForCountCharsInDiag() { return Stream.of(Arguments.of("EmptyFieldReturns0", new char[][] { {'-', '-', '-'}, {'-', '-', '-'}, {'-', '-', '-'} }, - 0, 'o', 0)); + 0, 'o', 0), + Arguments.of("TwoCharsInRowReturnsTwo", + new char[][] { {'-', '-', 'o'}, {'o', 'o', '-'}, {'-', '-', '-'} }, + 1, 'o', 2)); } }