Browse Source

added static method convertSimpleToState to Board class with test

feature-game
Dion Aliu 3 years ago
parent
commit
c6f77554bb
  1. 5
      src/main/java/Game/Game.java
  2. 41
      src/main/java/Game/TicTacToe/Board.java
  3. 12
      src/main/java/Game/Tictactoe.java
  4. 21
      src/test/java/Game/TicTacToe/BoardTest.java

5
src/main/java/Game/Game.java

@ -6,7 +6,7 @@ public abstract class Game {
protected ArrayList<String> outputBuffer = new ArrayList<>(); protected ArrayList<String> outputBuffer = new ArrayList<>();
public abstract void update(String string);
public abstract void update(String input);
public void print() { public void print() {
for (String s : outputBuffer) { for (String s : outputBuffer) {
@ -21,7 +21,4 @@ public abstract class Game {
public ArrayList<String> getOutputBuffer() { public ArrayList<String> getOutputBuffer() {
return this.outputBuffer; return this.outputBuffer;
} }
} }

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

@ -9,6 +9,14 @@ public class Board {
CROSS, CROSS,
EMPTY EMPTY
} }
enum CurrentState {
DRAW,
CIRCLEWIN,
CROSSWIN,
NOTFINÌSHED
}
private State[] states; private State[] states;
@ -34,17 +42,20 @@ public class Board {
} }
} }
public void setCellState(int cell, boolean cross) {
public boolean setCellState(int cell, boolean cross) {
if (cell <= 9 && cell >= 1) { if (cell <= 9 && cell >= 1) {
if (this.states[cell-1] != State.EMPTY) { if (this.states[cell-1] != State.EMPTY) {
return;
return false;
} }
if (cross) { if (cross) {
this.states[cell-1] = State.CROSS; this.states[cell-1] = State.CROSS;
} else { } else {
this.states[cell-1] = State.CIRCLE; this.states[cell-1] = State.CIRCLE;
} }
}else {
return false;
} }
return true;
} }
@ -91,4 +102,30 @@ public class Board {
} }
return true; return true;
} }
//Checkfull(draw), Checkwinner clear,neue Runde und Scoreboard
public static State[] convertSimpleToState(int[] temp) {
if (temp.length != 9)
return null;
State[] stateArray = new State[9];
for (int i = 0; i < temp.length; i++) {
switch(temp[i]) {
case 1:
stateArray[i] = State.CIRCLE;
break;
case 2:
stateArray[i] = State.CROSS;
break;
default:
stateArray[i] = State.EMPTY;
}
}
return stateArray;
}
} }

12
src/main/java/Game/Tictactoe.java

@ -19,7 +19,19 @@ public class Tictactoe extends Game {
@Override @Override
public void update(String input) { public void update(String input) {
outputBuffer.clear();
boolean validTurn = currentBoard.setCellState(Integer.parseInt(input), crossTurn);
outputBuffer.addAll(currentBoard.getOutputBoard()); outputBuffer.addAll(currentBoard.getOutputBoard());
if(validTurn) {
switchTurn();
} else {
outputBuffer.add("Invalid Turn!");
}
outputBuffer.add((crossTurn?"Cross":"Circle") + " it´s your Turn, please choose a Cell:");
}
public void switchTurn() {
crossTurn = !crossTurn;
} }
} }

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

@ -4,10 +4,12 @@ import Game.Tictactoe;
import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.mockito.internal.util.reflection.FieldSetter;
import java.util.Arrays; import java.util.Arrays;
import static org.junit.jupiter.api.Assertions.*; import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.mock;
class BoardTest { class BoardTest {
@ -59,4 +61,23 @@ class BoardTest {
board.getStates()[0] = Board.State.CROSS; board.getStates()[0] = Board.State.CROSS;
assertEquals(board, secondBoard); assertEquals(board, secondBoard);
} }
@Test
void convertSimpleToState() {
int[] test = {1, 1, 1, 2, 0, 2, 2, 0, 0};
Board.State[] expected = {Board.State.CIRCLE, Board.State.CIRCLE, Board.State.CIRCLE, Board.State.CROSS, Board.State.EMPTY, Board.State.CROSS, Board.State.CROSS, Board.State.EMPTY, Board.State.EMPTY};
int[] test2 = {1, 0, 0, 1, 0, 0, 1, 0, 0};
Board.State[] expected2 = {Board.State.CIRCLE, Board.State.EMPTY, Board.State.EMPTY, Board.State.CIRCLE, Board.State.EMPTY, Board.State.EMPTY, Board.State.CIRCLE, Board.State.EMPTY, Board.State.EMPTY};
int[] test3 = {69, 0, 0, 0, 1, 0, 0, 0, 1};
Board.State[] expected3 = {Board.State.CIRCLE, Board.State.EMPTY, Board.State.EMPTY, Board.State.EMPTY, Board.State.CIRCLE, Board.State.EMPTY, Board.State.EMPTY, Board.State.EMPTY, Board.State.CIRCLE};
int[] test4 = {3, 0, 0, 0, 1, 0, 0, 0, 1};
Board.State[] expected4 = {Board.State.EMPTY, Board.State.EMPTY, Board.State.EMPTY, Board.State.EMPTY, Board.State.CIRCLE, Board.State.EMPTY, Board.State.EMPTY, Board.State.EMPTY, Board.State.CIRCLE};
int[] test5 = {3, 0, 0, 0, 1, 0, 0, 0};
assertTrue(Arrays.deepEquals(expected, Board.convertSimpleToState(test)));
assertTrue(Arrays.deepEquals(expected2, Board.convertSimpleToState(test2)));
assertFalse(Arrays.deepEquals(expected3, Board.convertSimpleToState(test3)));
assertTrue(Arrays.deepEquals(expected4, Board.convertSimpleToState(test4)));
assertNull(Board.convertSimpleToState(test5));
}
} }
Loading…
Cancel
Save