From b704539e86fb3ffcb6eb834437eb142a5778bc63 Mon Sep 17 00:00:00 2001 From: Dion Aliu Date: Wed, 26 Jan 2022 10:32:33 +0100 Subject: [PATCH] added getCurrentState method for Board class with tests for detecting winner, draw or not finished game --- src/main/java/Game/TicTacToe/Board.java | 44 +++++++++++++++++++- src/test/java/Game/TicTacToe/BoardTest.java | 45 +++++++++++++++++++++ 2 files changed, 88 insertions(+), 1 deletion(-) diff --git a/src/main/java/Game/TicTacToe/Board.java b/src/main/java/Game/TicTacToe/Board.java index 66c00c1..c3a9e87 100644 --- a/src/main/java/Game/TicTacToe/Board.java +++ b/src/main/java/Game/TicTacToe/Board.java @@ -1,6 +1,7 @@ package Game.TicTacToe; import java.util.ArrayList; +import java.util.Arrays; public class Board { @@ -14,9 +15,10 @@ public class Board { DRAW, CIRCLEWIN, CROSSWIN, - NOTFINÌSHED + NOTFINISHED } + private ArrayList winPatterns; private State[] states; @@ -25,6 +27,15 @@ public class Board { for (int i = 0; i < states.length; i++) { states[i] = State.EMPTY; } + winPatterns = new ArrayList<>(); + winPatterns.add(new int[]{1,1,1,0,0,0,0,0,0}); + winPatterns.add(new int[]{0,0,0,1,1,1,0,0,0}); + winPatterns.add(new int[]{0,0,0,0,0,0,1,1,1}); + winPatterns.add(new int[]{1,0,0,1,0,0,1,0,0}); + winPatterns.add(new int[]{0,1,0,0,1,0,0,1,0}); + winPatterns.add(new int[]{0,0,1,0,0,1,0,0,1}); + winPatterns.add(new int[]{0,0,1,0,1,0,1,0,0}); + winPatterns.add(new int[]{1,0,0,0,1,0,0,0,1}); } @@ -106,7 +117,38 @@ public class Board { //Checkfull(draw), Checkwinner clear,neue Runde und Scoreboard + public CurrentState getCurrentState() { + + int counterCross = 0; + int counterCircle = 0; + + for (int[] pattern : winPatterns) { + counterCircle = 0; + counterCross = 0; + for (int i = 0; i < pattern.length; i++) { + if (pattern[i] == 1) { + if (getStates()[i] == State.CIRCLE) + counterCircle++; + if (getStates()[i] == State.CROSS) + counterCross++; + } + } + if (counterCircle >= 3) + return CurrentState.CIRCLEWIN; + if (counterCross >= 3) + return CurrentState.CROSSWIN; + } + + //Not finished + for (int i = 0; i < getStates().length; i++) { + if (getStates()[i] == State.EMPTY) { + return CurrentState.NOTFINISHED; + } + } + //Draw + return CurrentState.DRAW; + } public static State[] convertSimpleToState(int[] temp) { if (temp.length != 9) diff --git a/src/test/java/Game/TicTacToe/BoardTest.java b/src/test/java/Game/TicTacToe/BoardTest.java index 6a297fe..40218e4 100644 --- a/src/test/java/Game/TicTacToe/BoardTest.java +++ b/src/test/java/Game/TicTacToe/BoardTest.java @@ -80,4 +80,49 @@ class BoardTest { assertTrue(Arrays.deepEquals(expected4, Board.convertSimpleToState(test4))); assertNull(Board.convertSimpleToState(test5)); } + + @Test + void getCurrentState() { + Board.State[] testArray = Board.convertSimpleToState(new int[]{2, 2, 2, 1, 0, 1, 1, 0, 0}); + Board.State[] testArray2 = Board.convertSimpleToState(new int[]{1, 0, 2, 1, 2, 0, 1, 0, 2}); + Board.State[] testArray3 = Board.convertSimpleToState(new int[]{1, 0, 2, 0, 1, 2, 0, 2, 1}); + Board.State[] testArray4 = Board.convertSimpleToState(new int[]{0, 1, 2, 2, 1, 0, 0, 1, 2}); + Board.State[] testArray5 = Board.convertSimpleToState(new int[]{1, 0, 0, 2, 2, 2, 1, 0, 1}); + Board.State[] testArray6 = Board.convertSimpleToState(new int[]{2, 0, 1, 2, 1, 0, 1, 2, 0}); + Board.State[] testArray7 = Board.convertSimpleToState(new int[]{0, 0, 1, 0, 2, 1, 2, 2, 1}); + Board.State[] testArray8 = Board.convertSimpleToState(new int[]{1, 0, 0, 1, 1, 0, 2, 2, 2}); + + Board.State[] drawArray = Board.convertSimpleToState(new int[]{2, 1, 2, 1, 2, 1, 1, 2, 1}); + Board.State[] drawArray2 = Board.convertSimpleToState(new int[]{2, 2, 1, 1, 1, 2, 2, 2, 1}); + Board.State[] notFinishedArray = Board.convertSimpleToState(new int[]{2, 2, 1, 2, 0, 1, 0, 0, 0}); + + try { + FieldSetter.setField(board, board.getClass().getDeclaredField("states"), testArray); + assertEquals(board.getCurrentState(), Board.CurrentState.CROSSWIN); + FieldSetter.setField(board, board.getClass().getDeclaredField("states"), testArray2); + assertEquals(board.getCurrentState(), Board.CurrentState.CIRCLEWIN); + FieldSetter.setField(board, board.getClass().getDeclaredField("states"), testArray3); + assertEquals(board.getCurrentState(), Board.CurrentState.CIRCLEWIN); + FieldSetter.setField(board, board.getClass().getDeclaredField("states"), testArray4); + assertEquals(board.getCurrentState(), Board.CurrentState.CIRCLEWIN); + FieldSetter.setField(board, board.getClass().getDeclaredField("states"), testArray5); + assertEquals(board.getCurrentState(), Board.CurrentState.CROSSWIN); + FieldSetter.setField(board, board.getClass().getDeclaredField("states"), testArray6); + assertEquals(board.getCurrentState(), Board.CurrentState.CIRCLEWIN); + FieldSetter.setField(board, board.getClass().getDeclaredField("states"), testArray7); + assertEquals(board.getCurrentState(), Board.CurrentState.CIRCLEWIN); + FieldSetter.setField(board, board.getClass().getDeclaredField("states"), testArray8); + assertEquals(board.getCurrentState(), Board.CurrentState.CROSSWIN); + + FieldSetter.setField(board, board.getClass().getDeclaredField("states"), drawArray); + assertEquals(board.getCurrentState(), Board.CurrentState.DRAW); + FieldSetter.setField(board, board.getClass().getDeclaredField("states"), drawArray2); + assertEquals(board.getCurrentState(), Board.CurrentState.DRAW); + FieldSetter.setField(board, board.getClass().getDeclaredField("states"), notFinishedArray); + assertEquals(board.getCurrentState(), Board.CurrentState.NOTFINISHED); + + } catch (NoSuchFieldException e) { + e.printStackTrace(); + } + } } \ No newline at end of file