From 1d8342e45adf8aafe48b9f6ea97223f742f9ef2b Mon Sep 17 00:00:00 2001 From: Nick Stolbov Date: Mon, 31 Jan 2022 12:32:36 +0100 Subject: [PATCH] Created Chess class with convertInput method with test --- src/main/java/Game/Chess.java | 53 +++++++++++++++++++++++++++++++ src/test/java/Game/ChessTest.java | 43 +++++++++++++++++++++++++ 2 files changed, 96 insertions(+) create mode 100644 src/main/java/Game/Chess.java create mode 100644 src/test/java/Game/ChessTest.java diff --git a/src/main/java/Game/Chess.java b/src/main/java/Game/Chess.java new file mode 100644 index 0000000..26a0c51 --- /dev/null +++ b/src/main/java/Game/Chess.java @@ -0,0 +1,53 @@ +package Game; + +import Game.ChessObj.ChessBoard; + +public class Chess extends Game { + + ChessBoard chessBoard; + + public Chess() { + init(); + } + + private void init() { + chessBoard = new ChessBoard(); + outputBuffer.addAll(chessBoard.getOutputBoard()); + } + + @Override + public void update(String input) { + + } + + //int[0] = x, int[1] = y; + public int[] convertInput(String input) { + int[] temp = new int[2]; + + //input is not 2 chars big + if (input.length() != 2) + return null; + char[] symbols = input.toCharArray(); + int counterDigit = 0; + int counterChar = 0; + //input contains one number and one char + for (char x : symbols) { + if (Character.isDigit(x)) { + counterDigit++; + temp[1] = Character.getNumericValue(x) - 1; + } + if (Character.isAlphabetic(x)) { + counterChar++; + temp[0] = x; + } + } + if (counterChar != 1 || counterDigit != 1) + return null; + + temp[0] = Character.toLowerCase(temp[0]) - 97; + if (!chessBoard.isCellInBoard(temp[0], temp[1])) + return null; + else + return temp; + } +} diff --git a/src/test/java/Game/ChessTest.java b/src/test/java/Game/ChessTest.java new file mode 100644 index 0000000..8dc691c --- /dev/null +++ b/src/test/java/Game/ChessTest.java @@ -0,0 +1,43 @@ +package Game; + +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; +import static org.junit.jupiter.api.Assertions.assertNull; + +class ChessTest { + + Chess chess; + + @BeforeEach + void setUp() { + chess = new Chess(); + } + + @AfterEach + void tearDown() { + } + + @Test + void convertInput() { + int[] test1 = {0, 0}; + int[] test2 = {7, 7}; + int[] test3 = {6, 2}; + + assertNull(chess.convertInput("0g2")); + assertNull(chess.convertInput("25")); + assertNull(chess.convertInput("bg")); + assertNull(chess.convertInput("9b")); + assertNull(chess.convertInput("2i")); + + assertArrayEquals(chess.convertInput("1a"), test1); + assertArrayEquals((chess.convertInput("a1")), test1); + assertArrayEquals((chess.convertInput("8h")), test2); + assertArrayEquals((chess.convertInput("h8")), test2); + + assertArrayEquals((chess.convertInput("3G")), test3); + assertArrayEquals((chess.convertInput("G3")), test3); + } +} \ No newline at end of file