From 56da63b0e6947edfd2b6d84a7edca09feac9799d Mon Sep 17 00:00:00 2001 From: Malte Schellhardt Date: Wed, 9 Feb 2022 16:27:48 +0100 Subject: [PATCH] tictactoe: added test case for trying to set a field again --- src/main/java/de/tims/tictactoe/GameLogic.java | 9 ++++++--- src/test/java/de/tims/tictactoe/GameLogicTest.java | 6 +++++- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/src/main/java/de/tims/tictactoe/GameLogic.java b/src/main/java/de/tims/tictactoe/GameLogic.java index 7beb042..0790cb3 100644 --- a/src/main/java/de/tims/tictactoe/GameLogic.java +++ b/src/main/java/de/tims/tictactoe/GameLogic.java @@ -5,7 +5,7 @@ import java.util.Arrays; public class GameLogic { private char[][] board; - private final char emptyField = '-'; + private final char[] occupiedFields = { 'x', 'o' }; public GameLogic(int size) { if (size < 3) { @@ -33,11 +33,14 @@ public class GameLogic { } 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) { - return (this.board[column][row] == emptyField) ? true : false; + for (char field : this.occupiedFields) { + if (this.board[column][row] == field) return false; + } + return true; } } diff --git a/src/test/java/de/tims/tictactoe/GameLogicTest.java b/src/test/java/de/tims/tictactoe/GameLogicTest.java index bc324bb..9e08172 100644 --- a/src/test/java/de/tims/tictactoe/GameLogicTest.java +++ b/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[][] {{'x', '-', '-'}, {'o', '-', '-'}, - {'-', '-', '-'}}) + {'-', '-', '-'}}), + Arguments.of("try to set occupied field [1][0] for player 1", 1, 0, 'x', new char[][] + {{'x', '-', '-'}, + {'o', '-', '-'}, + {'-', '-', '-'}}) ); }