Browse Source

added getCurrentState method for Board class with tests for detecting winner, draw or not finished game

feature-game
Dion Aliu 3 years ago
parent
commit
b704539e86
  1. 44
      src/main/java/Game/TicTacToe/Board.java
  2. 45
      src/test/java/Game/TicTacToe/BoardTest.java

44
src/main/java/Game/TicTacToe/Board.java

@ -1,6 +1,7 @@
package Game.TicTacToe; package Game.TicTacToe;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
public class Board { public class Board {
@ -14,9 +15,10 @@ public class Board {
DRAW, DRAW,
CIRCLEWIN, CIRCLEWIN,
CROSSWIN, CROSSWIN,
NOTFINÌSHED
NOTFINISHED
} }
private ArrayList<int[]> winPatterns;
private State[] states; private State[] states;
@ -25,6 +27,15 @@ public class Board {
for (int i = 0; i < states.length; i++) { for (int i = 0; i < states.length; i++) {
states[i] = State.EMPTY; 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 //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) { public static State[] convertSimpleToState(int[] temp) {
if (temp.length != 9) if (temp.length != 9)

45
src/test/java/Game/TicTacToe/BoardTest.java

@ -80,4 +80,49 @@ class BoardTest {
assertTrue(Arrays.deepEquals(expected4, Board.convertSimpleToState(test4))); assertTrue(Arrays.deepEquals(expected4, Board.convertSimpleToState(test4)));
assertNull(Board.convertSimpleToState(test5)); 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();
}
}
} }
Loading…
Cancel
Save