diff --git a/src/model/Board.java b/src/model/Board.java index f4a753d..05374fc 100644 --- a/src/model/Board.java +++ b/src/model/Board.java @@ -1,10 +1,19 @@ package model; public class Board { + Player[] boxes = new Player[9]; public void setBox(int column, int row, Player value) { + int index = getIndex(column, row); + boxes[index] = value; } public Player getBox(int column, int row) { - return null; + int index = getIndex(column, row); + return boxes[index]; + } + + private int getIndex(int column, int row) { + if (row < 1 || row > 3) throw new IndexOutOfBoundsException(); + return (column - 1) * 3 + row - 1; } } diff --git a/src/model/InputChecker.java b/src/model/InputChecker.java index f289010..5745960 100644 --- a/src/model/InputChecker.java +++ b/src/model/InputChecker.java @@ -2,6 +2,10 @@ package model; public class InputChecker { public boolean isInputValid(Board board, int box) { - return false; + if (box < 1 || box > 9) return false; + int col = (box - 1) / 3 + 1; + int row = box % 3; + if (row == 0) row = 3; + return board.getBox(col, row) == null; } } diff --git a/src/model/WinChecker.java b/src/model/WinChecker.java index 0d536dd..aaa8326 100644 --- a/src/model/WinChecker.java +++ b/src/model/WinChecker.java @@ -2,10 +2,87 @@ package model; public class WinChecker { public boolean isGameOver(Board b) { - return false; + if (b.getBox(1, 1) != null) { + if (b.getBox(1, 1) == b.getBox(1, 2) + && b.getBox(1, 2) == b.getBox(1, 3)) + return true; + if (b.getBox(1, 1) == b.getBox(2, 1) + && b.getBox(1, 1) == b.getBox(3, 1)) + return true; + if (b.getBox(1, 1) == b.getBox(2, 2) + && b.getBox(1, 1) == b.getBox(3, 3)) + return true; + } + if (b.getBox(3, 1) != null) { + if (b.getBox(3, 1) == b.getBox(3, 2) + && b.getBox(3, 1) == b.getBox(3, 3)) + return true; + if (b.getBox(3, 1) == b.getBox(2, 2) + && b.getBox(3, 1) == b.getBox(1, 3)) + return true; + } + if (b.getBox(2, 1) != null + && b.getBox(2, 1) == b.getBox(2, 2) + && b.getBox(2, 1) == b.getBox(2, 3)) + return true; + if (b.getBox(1, 2) != null + && b.getBox(1, 2) == b.getBox(2, 2) + && b.getBox(1, 2) == b.getBox(3, 2)) + return true; + if (b.getBox(1, 3) != null + && b.getBox(1, 3) == b.getBox(2, 3) + && b.getBox(1, 3) == b.getBox(3, 3)) + return true; + for (int i = 1; i <= 3; ++i) + for (int j = 1; j <= 3; ++j) + if (b.getBox(i, j) == null) + return false; + return true; } - public Player getWinner(Board b) { - return null; + public Player getWinner(Board b) throws IllegalGameStateException { + Player currentBox, winner = null; + currentBox = b.getBox(1, 1); + if (currentBox != null) { + if (currentBox == b.getBox(1, 2) + && currentBox == b.getBox(1, 3)) + winner = currentBox; + if (currentBox == b.getBox(2, 1) + && currentBox == b.getBox(3, 1)) + winner = currentBox; + if (currentBox == b.getBox(2, 2) + && currentBox == b.getBox(3, 3)) + winner = currentBox; + } + currentBox = b.getBox(3, 1); + if (currentBox != null) { + if (currentBox == b.getBox(3, 2) + && currentBox == b.getBox(3, 3)) { + if (winner != null && winner != currentBox) throw new IllegalGameStateException(); + winner = currentBox; + } if (currentBox == b.getBox(2, 2) + && currentBox == b.getBox(1, 3)) { + if (winner != null && winner != currentBox) throw new IllegalGameStateException(); + winner = currentBox; + } } + currentBox = b.getBox(2, 1); + if (currentBox != null + && currentBox == b.getBox(2, 2) + && currentBox == b.getBox(2, 3)) { + if (winner != null && winner != currentBox) throw new IllegalGameStateException(); + winner = currentBox; + } currentBox = b.getBox(1, 2); + if (currentBox != null + && currentBox == b.getBox(2, 2) + && currentBox == b.getBox(3, 2)) { + if (winner != null && winner != currentBox) throw new IllegalGameStateException(); + winner = currentBox; + } currentBox = b.getBox(1, 3); + if (currentBox != null + && currentBox == b.getBox(2, 3) + && currentBox == b.getBox(3, 3)) { + if (winner != null && winner != currentBox) throw new IllegalGameStateException(); + winner = currentBox; + } return winner; } } diff --git a/test/BoardTests.java b/test/BoardTests.java index d6fdcf8..432399c 100644 --- a/test/BoardTests.java +++ b/test/BoardTests.java @@ -14,7 +14,7 @@ public class BoardTests { Random rng = RandomSingleton.get; int col = rng.nextInt(); int row = rng.nextInt(); - boolean shouldThrow = col >= 1 && col <= 3 && row >= 1 && row <= 3; + boolean shouldThrow = col < 1 || col > 3 || row < 1 || row > 3; return Stream.of( Arguments.of(1, 1, false), Arguments.of(1, 3, false), diff --git a/test/InputCheckerTests.java b/test/InputCheckerTests.java index abaa9df..1e524d0 100644 --- a/test/InputCheckerTests.java +++ b/test/InputCheckerTests.java @@ -6,7 +6,6 @@ import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.Arguments; import org.junit.jupiter.params.provider.MethodSource; -import java.util.Random; import java.util.stream.Stream; public class InputCheckerTests { @@ -15,7 +14,7 @@ public class InputCheckerTests { boolean isValid = val >= 1 && val <= 9; return Stream.of( Arguments.of(1, true), - Arguments.of(1, true), + Arguments.of(9, true), Arguments.of(-3, false), Arguments.of(15, false), Arguments.of(val, isValid) diff --git a/test/WinCheckerTests.java b/test/WinCheckerTests.java index 2a9bb0c..b6c5eb9 100644 --- a/test/WinCheckerTests.java +++ b/test/WinCheckerTests.java @@ -176,7 +176,7 @@ public class WinCheckerTests { @ParameterizedTest @MethodSource("getWinnerTestNormalData") - public void getWinnerTestNormal(Board b, Player expected){ + public void getWinnerTestNormal(Board b, Player expected) throws IllegalGameStateException { Player actual = new WinChecker().getWinner(b); Assertions.assertEquals(expected, actual); } @@ -194,7 +194,7 @@ public class WinCheckerTests { } @Test - public void getWinnerTestRandom(){ + public void getWinnerTestRandom() throws IllegalGameStateException { Player currentBox; Board b = new Board(); Player expected = null;