From c6f77554bb7d30288258a60e2869f73e086cbcc2 Mon Sep 17 00:00:00 2001 From: Dion Aliu Date: Wed, 26 Jan 2022 09:08:57 +0100 Subject: [PATCH] added static method convertSimpleToState to Board class with test --- src/main/java/Game/Game.java | 5 +-- src/main/java/Game/TicTacToe/Board.java | 41 ++++++++++++++++++++- src/main/java/Game/Tictactoe.java | 12 ++++++ src/test/java/Game/TicTacToe/BoardTest.java | 21 +++++++++++ 4 files changed, 73 insertions(+), 6 deletions(-) diff --git a/src/main/java/Game/Game.java b/src/main/java/Game/Game.java index fd0e0ff..f927c82 100644 --- a/src/main/java/Game/Game.java +++ b/src/main/java/Game/Game.java @@ -6,7 +6,7 @@ public abstract class Game { protected ArrayList outputBuffer = new ArrayList<>(); - public abstract void update(String string); + public abstract void update(String input); public void print() { for (String s : outputBuffer) { @@ -21,7 +21,4 @@ public abstract class Game { public ArrayList getOutputBuffer() { return this.outputBuffer; } - - - } diff --git a/src/main/java/Game/TicTacToe/Board.java b/src/main/java/Game/TicTacToe/Board.java index 0f82d3d..66c00c1 100644 --- a/src/main/java/Game/TicTacToe/Board.java +++ b/src/main/java/Game/TicTacToe/Board.java @@ -9,6 +9,14 @@ public class Board { CROSS, EMPTY } + + enum CurrentState { + DRAW, + CIRCLEWIN, + CROSSWIN, + NOTFINÌSHED + } + 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 (this.states[cell-1] != State.EMPTY) { - return; + return false; } if (cross) { this.states[cell-1] = State.CROSS; } else { this.states[cell-1] = State.CIRCLE; } + }else { + return false; } + return true; } @@ -91,4 +102,30 @@ public class Board { } 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; + } + } diff --git a/src/main/java/Game/Tictactoe.java b/src/main/java/Game/Tictactoe.java index b8c09eb..035b7b7 100644 --- a/src/main/java/Game/Tictactoe.java +++ b/src/main/java/Game/Tictactoe.java @@ -19,7 +19,19 @@ public class Tictactoe extends Game { @Override public void update(String input) { + outputBuffer.clear(); + boolean validTurn = currentBoard.setCellState(Integer.parseInt(input), crossTurn); 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; } } diff --git a/src/test/java/Game/TicTacToe/BoardTest.java b/src/test/java/Game/TicTacToe/BoardTest.java index bda9727..6a297fe 100644 --- a/src/test/java/Game/TicTacToe/BoardTest.java +++ b/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.BeforeEach; import org.junit.jupiter.api.Test; +import org.mockito.internal.util.reflection.FieldSetter; import java.util.Arrays; import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.Mockito.mock; class BoardTest { @@ -59,4 +61,23 @@ class BoardTest { board.getStates()[0] = Board.State.CROSS; 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)); + } } \ No newline at end of file