10 Commits

  1. 247
      src/main/java/Game/Chess.java
  2. 52
      src/main/java/Game/ChessObj/ChessBoard.java
  3. 2
      src/main/java/Game/ChessObj/ChessFigure.java
  4. 64
      src/test/java/Game/ChessObj/ChessBoardTest.java
  5. 2
      src/test/java/Game/ChessObj/ChessFigureTest.java
  6. 132
      src/test/java/Game/ChessTest.java

247
src/main/java/Game/Chess.java

@ -0,0 +1,247 @@
package Game;
import Game.ChessObj.ChessBoard;
import Game.ChessObj.ChessFigure;
import java.util.ArrayList;
public class Chess extends Game {
private int[] firstTurn;
private ChessFigure.Team currentTeam;
private ArrayList<ChessFigure> destroyedWhiteFigures;
private ArrayList<ChessFigure> destroyedBlackFigures;
private ChessBoard.MoveFeedback lastFeedback;
private ChessBoard chessBoard;
public Chess() {
init();
}
private void init() {
resetGame();
}
@Override
public void update(String input) {
outputBuffer.clear();
ArrayList<String> footer = new ArrayList<>();
if (isFinished()) {
resetGame();
return;
}
int[] coords = convertInput(input);
if (coords == null) {
footer.add("Invalid Input!");
firstTurn = null;
} else {
if (firstTurn == null) {
firstTurn = coords;
if (getChessBoard().getBoard()[firstTurn[1]][firstTurn[0]] != null) {
if (getChessBoard().getBoard()[firstTurn[1]][firstTurn[0]].getTeam() == currentTeam) {
String firstCoords = (char) (firstTurn[0] + 97) + "" + (firstTurn[1] + 1);
footer.add("Currently selected cell: " + firstCoords);
} else {
footer.add("It's " + currentTeam.name() + "'s turn ");
firstTurn = null;
}
} else {
footer.add("Can't select empty cell");
firstTurn = null;
}
} else {
footer = makeMove(firstTurn, coords);
if(lastFeedback != ChessBoard.MoveFeedback.INVALID && lastFeedback != ChessBoard.MoveFeedback.OUTSIDEOFBOARD)
switchCurrentTeam();
firstTurn = null;
}
}
if (chessBoard.scanForOccurringFigure(ChessFigure.Type.KING, ChessFigure.Team.WHITE) == 0) {
footer.add("Black has Won!");
footer.add("Press any key to restart game");
setFinished(true);
}
if (chessBoard.scanForOccurringFigure(ChessFigure.Type.KING, ChessFigure.Team.BLACK) == 0) {
footer.add("White has Won!");
footer.add("Press any key to restart game");
setFinished(true);
}
outputBuffer.addAll(getHeader());
outputBuffer.addAll(getUpdatedOutputBoard(destroyedWhiteFigures, destroyedBlackFigures));
outputBuffer.addAll(footer);
}
private ArrayList<String> getHeader() {
ArrayList<String> header = new ArrayList<>();
header.add("Welcome to the good old game Chess!");
header.add("First select one cell like: 'e7', than confirm it with an enter.");
header.add("After that, select a second cell and also confirm it with an enter.");
header.add("The Goal is to defeat the other teams King(K).");
header.add("");
header.add("It's your Turn " + currentTeam.name().toLowerCase() + ".");
return header;
}
//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;
}
public ArrayList<String> makeMove(int[] source, int[] target) {
ArrayList<String> result = new ArrayList<>();
ChessFigure sourceFigure = chessBoard.getBoard()[source[1]][source[0]];
ChessFigure targetFigure = chessBoard.getBoard()[target[1]][target[0]];
String sourceFigureName = "<NOT FOUND>";
String targetFigureName = "<NOT FOUND>";
String sourceCoords = (char) (source[0] + 97) + "" + (source[1] + 1);
String targetCoords = (char) (target[0] + 97) + "" + (target[1] + 1);
ChessBoard.MoveFeedback moveFeedback = chessBoard.moveFigure(source[0], source[1], target[0], target[1]);
lastFeedback = moveFeedback;
if (sourceFigure != null)
sourceFigureName = sourceFigure.getTeam().name().toCharArray()[0] + sourceFigure.getTeam().name().substring(1).toLowerCase() + " " + sourceFigure.getType().name().toLowerCase();
if (targetFigure != null)
targetFigureName = targetFigure.getTeam().name().toLowerCase() + " " + targetFigure.getType().name().toLowerCase();
if (moveFeedback == ChessBoard.MoveFeedback.MOVE) {
result.add("Successfully moved " + sourceFigureName.toLowerCase() + " from " + sourceCoords + " to " + targetCoords);
} else if (moveFeedback == ChessBoard.MoveFeedback.ENEMYBEATEN) {
result.add(sourceFigureName + " successfully beat " + targetFigureName.toLowerCase() + " at " + targetCoords);
if (targetFigure.getTeam() == ChessFigure.Team.WHITE)
getDestroyedWhiteFigures().add(targetFigure);
else
getDestroyedBlackFigures().add(targetFigure);
} else {
result.add("Invalid input!");
switch (moveFeedback) {
case INVALID:
if (targetFigure != null)
if (targetFigure.getTeam() == sourceFigure.getTeam())
result.add("You are on the same Team! [" + getCurrentTeam().name() + "]");
break;
case OUTSIDEOFBOARD:
result.add("Input is not inside the board!");
break;
default:
}
}
return result;
}
private ArrayList<String> getUpdatedOutputBoard(ArrayList<ChessFigure> whiteFigures, ArrayList<ChessFigure> blackFigures) {
ArrayList<String> updatedOutputBoard = chessBoard.getOutputBoard();
ArrayList<String> sidebarWhiteFigures = getSidebarFigures(whiteFigures, 5);
ArrayList<String> sidebarBlackFigures = getSidebarFigures(blackFigures, 5);
for (int i = 0; i < sidebarWhiteFigures.size(); i++) {
int index = i + 3;
updatedOutputBoard.set(index, updatedOutputBoard.get(index) + sidebarWhiteFigures.get(i));
}
for (int i = 0; i < sidebarBlackFigures.size(); i++) {
int index = updatedOutputBoard.size() - (i + 3);
updatedOutputBoard.set(index, updatedOutputBoard.get(index) + sidebarBlackFigures.get(i));
}
return updatedOutputBoard;
}
public ArrayList<String> getSidebarFigures(ArrayList<ChessFigure> chessFigureArrayList, int maxPerLine) {
ArrayList<String> result = new ArrayList<>();
String line = "";
int counter = 0;
for (int i = 0; i < chessFigureArrayList.size(); i++) {
if (i == chessFigureArrayList.size() - 1) {
line += chessFigureArrayList.get(i).getSymbol() + "";
result.add(line);
return result;
}
line += chessFigureArrayList.get(i).getSymbol() + ",";
counter++;
if (counter >= maxPerLine) {
result.add(line);
line = "";
counter = 0;
}
}
return result;
}
private void switchCurrentTeam() {
if (currentTeam == ChessFigure.Team.WHITE)
currentTeam = ChessFigure.Team.BLACK;
else
currentTeam = ChessFigure.Team.WHITE;
}
public void resetGame() {
chessBoard = new ChessBoard();
firstTurn = null;
currentTeam = ChessFigure.Team.WHITE;
destroyedWhiteFigures = new ArrayList<>();
destroyedBlackFigures = new ArrayList<>();
setFinished(false);
outputBuffer.clear();
outputBuffer.addAll(getHeader());
outputBuffer.addAll(chessBoard.getOutputBoard());
}
public ChessBoard getChessBoard() {
return this.chessBoard;
}
public ChessFigure.Team getCurrentTeam() {
return this.currentTeam;
}
public ArrayList<ChessFigure> getDestroyedWhiteFigures() {
return destroyedWhiteFigures;
}
public ArrayList<ChessFigure> getDestroyedBlackFigures() {
return destroyedBlackFigures;
}
}

52
src/main/java/Game/ChessObj/ChessBoard.java

@ -4,6 +4,13 @@ import java.util.ArrayList;
public class ChessBoard {
public enum MoveFeedback {
ENEMYBEATEN,
MOVE,
OUTSIDEOFBOARD,
INVALID
}
private ChessFigure[][] board;
public ChessBoard() {
@ -88,14 +95,14 @@ public class ChessBoard {
return false;
ChessFigure figure = board[sourceY][sourceX];
//Destination has same Team
if(board[destY][destX] != null)
if(board[destY][destX].getTeam() == figure.getTeam())
if (board[destY][destX] != null)
if (board[destY][destX].getTeam() == figure.getTeam())
return false;
int deltaX = destX - sourceX;
int deltaY = sourceY - destY;
//Pawn special case
if(figure.getType() == ChessFigure.Type.PAWN){
if(board[destY][destX] != null) {
if (figure.getType() == ChessFigure.Type.PAWN) {
if (board[destY][destX] != null) {
if (board[destY][destX].getTeam() == figure.getTeam())
return false;
if (figure.getTeam() == ChessFigure.Team.WHITE && board[destY][destX].getTeam() != figure.getTeam() && (deltaX == -1 || deltaX == 1) && deltaY == 1)
@ -103,13 +110,13 @@ public class ChessBoard {
if (figure.getTeam() == ChessFigure.Team.BLACK && board[destY][destX].getTeam() != figure.getTeam() && (deltaX == -1 || deltaX == 1) && deltaY == -1)
return true;
}
if(figure.isRelativeMoveValid(deltaX, deltaY))
if (figure.isRelativeMoveValid(deltaX, deltaY))
return true;
}
if(figure.isRelativeMoveValid(deltaX, deltaY)){
if(figure.getType() == ChessFigure.Type.KNIGHT)
if (figure.isRelativeMoveValid(deltaX, deltaY)) {
if (figure.getType() == ChessFigure.Type.KNIGHT)
return true;
if(validateCleanPath(sourceX, sourceY, destX, destY))
if (validateCleanPath(sourceX, sourceY, destX, destY))
return true;
}
@ -126,7 +133,7 @@ public class ChessBoard {
sourceY += stepY;
if (!isCellInBoard(sourceX, sourceY) || !isCellInBoard(destX, destY))
return false;
if(Math.abs(deltaX) <= 1 && Math.abs(deltaY) <= 1)
if (Math.abs(deltaX) <= 1 && Math.abs(deltaY) <= 1)
return true;
while (!(sourceX == destX && sourceY == destY)) {
if (board[sourceY][sourceX] != null)
@ -142,6 +149,22 @@ public class ChessBoard {
return false;
}
public MoveFeedback moveFigure(int sourceX, int sourceY, final int destX, final int destY) {
if (!isCellInBoard(sourceX, sourceY) || !isCellInBoard(destX, destY))
return MoveFeedback.OUTSIDEOFBOARD;
if (validateMove(sourceX, sourceY, destX, destY)) {
MoveFeedback feedback = MoveFeedback.INVALID;
if (board[destY][destX] == null)
feedback = MoveFeedback.MOVE;
else
feedback = MoveFeedback.ENEMYBEATEN;
board[destY][destX] = board[sourceY][sourceX];
board[sourceY][sourceX] = null;
return feedback;
}
return MoveFeedback.INVALID;
}
private int getStepWidth(int delta) {
if (delta == 0)
return 0;
@ -151,6 +174,17 @@ public class ChessBoard {
return -1;
}
public int scanForOccurringFigure(ChessFigure.Type type, ChessFigure.Team team){
int count = 0;
ChessFigure template = new ChessFigure(type, team);
for(int y = 0; y < board.length; y++)
for(int x = 0; x < board[0].length; x++)
count += (template.equals(board[y][x]))?1:0;
return count;
}
public ChessFigure[][] getBoard() {
return this.board;
}

2
src/main/java/Game/ChessObj/ChessFigure.java

@ -70,7 +70,7 @@ public class ChessFigure {
return true;
break;
case QUEEN:
if ((dx == dy) || (dx == 0 ^ dy == 0))
if ((Math.abs(dx) == Math.abs(dy)) || (dx == 0 ^ dy == 0))
return true;
break;
case CASTLE:

64
src/test/java/Game/ChessObj/ChessBoardTest.java

@ -161,4 +161,68 @@ class ChessBoardTest {
assertTrue(chessBoard.validateMove(4, 7, 6, 5));
assertFalse(chessBoard.validateMove(4, 7, 7, 4));
}
/*
0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7
0 0
1 T K 1 K
2 2
3 Z 3 Z
-->
4 4 B
5 |Q| 5 T
6 B 6
7 7
*/
@Test
void moveFigure() {
ChessFigure[][] tempBoard = new ChessFigure[8][8];
ChessFigure[][] resultBoard = new ChessFigure[8][8];
tempBoard[1][3] = new ChessFigure(ChessFigure.Type.CASTLE, ChessFigure.Team.WHITE);
tempBoard[1][7] = new ChessFigure(ChessFigure.Type.KNIGHT, ChessFigure.Team.WHITE);
tempBoard[6][6] = new ChessFigure(ChessFigure.Type.BISHOP, ChessFigure.Team.WHITE);
tempBoard[3][5] = new ChessFigure(ChessFigure.Type.KNIGHT, ChessFigure.Team.WHITE);
tempBoard[5][2] = new ChessFigure(ChessFigure.Type.QUEEN, ChessFigure.Team.BLACK);
resultBoard[5][2] = new ChessFigure(ChessFigure.Type.CASTLE, ChessFigure.Team.WHITE);
resultBoard[1][7] = new ChessFigure(ChessFigure.Type.KNIGHT, ChessFigure.Team.WHITE);
resultBoard[4][4] = new ChessFigure(ChessFigure.Type.BISHOP, ChessFigure.Team.WHITE);
resultBoard[3][5] = new ChessFigure(ChessFigure.Type.KNIGHT, ChessFigure.Team.WHITE);
chessBoard.setChessBoard(tempBoard);
assertEquals(chessBoard.moveFigure(3, 1, 2, 1), ChessBoard.MoveFeedback.MOVE);
assertEquals(chessBoard.moveFigure(2, 1, 2, 5), ChessBoard.MoveFeedback.ENEMYBEATEN);
assertEquals(chessBoard.moveFigure(6, 6, 4, 4), ChessBoard.MoveFeedback.MOVE);
assertEquals(chessBoard.moveFigure(7, 1, 8, 1), ChessBoard.MoveFeedback.OUTSIDEOFBOARD);
assertEquals(chessBoard.moveFigure(5, 3, 4, 2), ChessBoard.MoveFeedback.INVALID);
assertArrayEquals(resultBoard, chessBoard.getBoard());
}
@Test
void scanForOccurringFigure() {
assertEquals(8, chessBoard.scanForOccurringFigure(ChessFigure.Type.PAWN, ChessFigure.Team.WHITE));
assertEquals(2, chessBoard.scanForOccurringFigure(ChessFigure.Type.CASTLE, ChessFigure.Team.WHITE));
assertEquals(2, chessBoard.scanForOccurringFigure(ChessFigure.Type.BISHOP, ChessFigure.Team.WHITE));
assertEquals(2, chessBoard.scanForOccurringFigure(ChessFigure.Type.KNIGHT, ChessFigure.Team.WHITE));
assertEquals(1, chessBoard.scanForOccurringFigure(ChessFigure.Type.QUEEN, ChessFigure.Team.WHITE));
assertEquals(1, chessBoard.scanForOccurringFigure(ChessFigure.Type.KING, ChessFigure.Team.WHITE));
assertEquals(8, chessBoard.scanForOccurringFigure(ChessFigure.Type.PAWN, ChessFigure.Team.BLACK));
assertEquals(2, chessBoard.scanForOccurringFigure(ChessFigure.Type.CASTLE, ChessFigure.Team.BLACK));
assertEquals(2, chessBoard.scanForOccurringFigure(ChessFigure.Type.BISHOP, ChessFigure.Team.BLACK));
assertEquals(2, chessBoard.scanForOccurringFigure(ChessFigure.Type.KNIGHT, ChessFigure.Team.BLACK));
assertEquals(1, chessBoard.scanForOccurringFigure(ChessFigure.Type.QUEEN, ChessFigure.Team.BLACK));
assertEquals(1, chessBoard.scanForOccurringFigure(ChessFigure.Type.KING, ChessFigure.Team.BLACK));
}
}

2
src/test/java/Game/ChessObj/ChessFigureTest.java

@ -68,7 +68,7 @@ class ChessFigureTest {
}
break;
case QUEEN:
if ((x == y) || (x == 0 ^ y == 0)) {
if (Math.abs(x) == Math.abs(y) || (x == 0 ^ y == 0)) {
assertTrue(figure.isRelativeMoveValid(x, y));
continue;
}

132
src/test/java/Game/ChessTest.java

@ -0,0 +1,132 @@
package Game;
import Game.ChessObj.ChessFigure;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.util.ArrayList;
import static org.junit.jupiter.api.Assertions.*;
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);
}
@Test
void getSidebarFigures() {
ArrayList<ChessFigure> array1 = new ArrayList<>();
ArrayList<ChessFigure> array2 = new ArrayList<>();
ArrayList<ChessFigure> array3 = new ArrayList<>();
ArrayList<String> expectedArray2 = new ArrayList<>();
ArrayList<String> expectedArray3 = new ArrayList<>();
array2.add(new ChessFigure(ChessFigure.Type.PAWN, ChessFigure.Team.WHITE));
array2.add(new ChessFigure(ChessFigure.Type.BISHOP, ChessFigure.Team.WHITE));
array2.add(new ChessFigure(ChessFigure.Type.PAWN, ChessFigure.Team.WHITE));
array2.add(new ChessFigure(ChessFigure.Type.PAWN, ChessFigure.Team.WHITE));
array2.add(new ChessFigure(ChessFigure.Type.CASTLE, ChessFigure.Team.WHITE));
array2.add(new ChessFigure(ChessFigure.Type.PAWN, ChessFigure.Team.WHITE));
expectedArray2.add(" o , I , o , o , T ,");
expectedArray2.add(" o ");
array3.add(new ChessFigure(ChessFigure.Type.QUEEN, ChessFigure.Team.BLACK));
array3.add(new ChessFigure(ChessFigure.Type.PAWN, ChessFigure.Team.BLACK));
array3.add(new ChessFigure(ChessFigure.Type.KING, ChessFigure.Team.BLACK));
array3.add(new ChessFigure(ChessFigure.Type.PAWN, ChessFigure.Team.BLACK));
expectedArray3.add("|Q|,|o|,|K|,|o|");
assertEquals(0, chess.getSidebarFigures(array1, 5).size());
assertEquals(expectedArray2, chess.getSidebarFigures(array2, 5));
assertEquals(expectedArray3, chess.getSidebarFigures(array3, 5));
}
@Test
void makeMove() {
int countBlackPawns = chess.getChessBoard().scanForOccurringFigure(ChessFigure.Type.PAWN, ChessFigure.Team.BLACK);
int countWhiteBishops = chess.getChessBoard().scanForOccurringFigure(ChessFigure.Type.BISHOP, ChessFigure.Team.WHITE);
chess.makeMove(chess.convertInput("a2"), chess.convertInput("a3"));
chess.makeMove(chess.convertInput("e7"), chess.convertInput("e6"));
chess.makeMove(chess.convertInput("f8"), chess.convertInput("a3"));
chess.makeMove(chess.convertInput("b2"), chess.convertInput("a3"));
assertEquals(countBlackPawns - 1, chess.getChessBoard().scanForOccurringFigure(ChessFigure.Type.PAWN, ChessFigure.Team.BLACK));
assertEquals(countWhiteBishops - 1, chess.getChessBoard().scanForOccurringFigure(ChessFigure.Type.BISHOP, ChessFigure.Team.WHITE));
}
@Test
void update() {
//starting team = White
assertEquals(chess.getCurrentTeam(), ChessFigure.Team.WHITE);
//Emtpy cell
chess.update("d7");
chess.update("g5");
assertEquals(chess.getCurrentTeam(), ChessFigure.Team.WHITE);
chess.update("7b");
chess.update("6b");
assertEquals(chess.getCurrentTeam(), ChessFigure.Team.BLACK);
chess.update("e2");
chess.update("e3");
assertEquals(chess.getCurrentTeam(), ChessFigure.Team.WHITE);
chess.update("6b");
chess.update("5b");
assertEquals(chess.getCurrentTeam(), ChessFigure.Team.BLACK);
//Defeat enemy
chess.update("f1");
chess.update("b5");
assertEquals(7, chess.getChessBoard().scanForOccurringFigure(ChessFigure.Type.PAWN, ChessFigure.Team.WHITE));
assertEquals(chess.getCurrentTeam(), ChessFigure.Team.WHITE);
chess.update("d7");
chess.update("d6");
assertEquals(chess.getCurrentTeam(), ChessFigure.Team.BLACK);
//Invalid Move
chess.update("5b");
chess.update("a5");
assertEquals(chess.getCurrentTeam(), ChessFigure.Team.BLACK);
//King defeated
chess.update("5b");
chess.update("e8");
assertTrue(chess.isFinished());
}
}
Loading…
Cancel
Save