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;
}
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;
char[][] board = gl.getBoard();

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

@ -125,5 +125,17 @@ class AIHardTest {
new char[][] { {'-', '-', 'o'}, {'o', 'o', '-'}, {'-', '-', '-'} },
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