Browse Source

tictactoe: added test case for trying to set a field again

tictactoe
Malte Schellhardt 3 years ago
committed by Lorenz Hohmann
parent
commit
56da63b0e6
  1. 9
      src/main/java/de/tims/tictactoe/GameLogic.java
  2. 6
      src/test/java/de/tims/tictactoe/GameLogicTest.java

9
src/main/java/de/tims/tictactoe/GameLogic.java

@ -5,7 +5,7 @@ import java.util.Arrays;
public class GameLogic { public class GameLogic {
private char[][] board; private char[][] board;
private final char emptyField = '-';
private final char[] occupiedFields = { 'x', 'o' };
public GameLogic(int size) { public GameLogic(int size) {
if (size < 3) { if (size < 3) {
@ -33,11 +33,14 @@ public class GameLogic {
} }
public void setField(int column, int row, char player) { public void setField(int column, int row, char player) {
this.board[column][row] = player;
if(fieldIsEmpty(column, row)) this.board[column][row] = player;
} }
public boolean fieldIsEmpty(int column, int row) { public boolean fieldIsEmpty(int column, int row) {
return (this.board[column][row] == emptyField) ? true : false;
for (char field : this.occupiedFields) {
if (this.board[column][row] == field) return false;
}
return true;
} }
} }

6
src/test/java/de/tims/tictactoe/GameLogicTest.java

@ -88,7 +88,11 @@ class GameLogicTest {
Arguments.of("set field [1][0] for player 2", 1, 0, 'o', new char[][] Arguments.of("set field [1][0] for player 2", 1, 0, 'o', new char[][]
{{'x', '-', '-'}, {{'x', '-', '-'},
{'o', '-', '-'}, {'o', '-', '-'},
{'-', '-', '-'}})
{'-', '-', '-'}}),
Arguments.of("try to set occupied field [1][0] for player 1", 1, 0, 'x', new char[][]
{{'x', '-', '-'},
{'o', '-', '-'},
{'-', '-', '-'}})
); );
} }

Loading…
Cancel
Save