Browse Source

tictactoe: countCharsInDiag throws IndexOutOfBoundsException for invalid index

tictactoe
Tobias Krause 3 years ago
committed by Lorenz Hohmann
parent
commit
01f94e3198
  1. 6
      src/main/java/de/tims/tictactoe/ai/AIHard.java
  2. 12
      src/test/java/de/tims/tictactoe/ai/AIHardTest.java

6
src/main/java/de/tims/tictactoe/ai/AIHard.java

@ -78,7 +78,11 @@ public class AIHard implements TicTacToeAI {
return count; return count;
} }
public int countCharsInDiag(int index, char charToCount) {
public int countCharsInDiag(int index, char charToCount) throws IndexOutOfBoundsException {
if (index < 0 || index > 1) {
throw new IndexOutOfBoundsException("Only 0 and 1 are allowed values for index!");
}
int count = 0; int count = 0;
char[][] board = gl.getBoard(); char[][] board = gl.getBoard();

12
src/test/java/de/tims/tictactoe/ai/AIHardTest.java

@ -126,4 +126,16 @@ class AIHardTest {
1, 'o', 2)); 1, 'o', 2));
} }
@Test
void invalidIndexCausesIndexOutOfBoundsException() {
int index = 2;
char charToCount = 'o';
String msg = "Only 0 and 1 are allowed values for index!";
doReturn(new char[][] { {'-', '-', '-'}, {'-', '-', '-'}, {'-', '-', '-'} }).when(gl).getBoard();
AIHard ai = new AIHard(gl);
assertThatThrownBy(() -> {ai.countCharsInDiag(index, charToCount);}).isInstanceOf(IndexOutOfBoundsException.class).hasMessage(msg);
}
} }
Loading…
Cancel
Save