Browse Source

Created Chess class with convertInput method with test

feature-chess
Dion Aliu 2 years ago
parent
commit
84be917943
  1. 53
      src/main/java/Game/Chess.java
  2. 43
      src/test/java/Game/ChessTest.java

53
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;
}
}

43
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);
}
}
Loading…
Cancel
Save