Browse Source

Added makeMove method to Chess for moving a figure and getting the corresponding output with test

feature-chess
Dion Aliu 2 years ago
parent
commit
8dfe543e92
  1. 55
      src/main/java/Game/Chess.java
  2. 13
      src/test/java/Game/ChessTest.java

55
src/main/java/Game/Chess.java

@ -7,7 +7,8 @@ import java.util.ArrayList;
public class Chess extends Game {
ChessBoard chessBoard;
private ChessFigure.Team currentTeam;
private ChessBoard chessBoard;
public Chess() {
init();
@ -15,6 +16,7 @@ public class Chess extends Game {
private void init() {
chessBoard = new ChessBoard();
currentTeam = ChessFigure.Team.WHITE;
outputBuffer.addAll(chessBoard.getOutputBoard());
}
@ -54,20 +56,57 @@ public class Chess extends Game {
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]);
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);
} else {
result.add("Invalid input!");
switch (moveFeedback) {
case INVALID:
if (chessBoard.getBoard()[target[0]][target[1]].getTeam() == getCurrentTeam())
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;
}
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) {
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) {
if (counter >= maxPerLine) {
result.add(line);
line = "";
counter = 0;
@ -77,4 +116,12 @@ public class Chess extends Game {
return result;
}
public ChessBoard getChessBoard() {
return this.chessBoard;
}
public ChessFigure.Team getCurrentTeam() {
return this.currentTeam;
}
}

13
src/test/java/Game/ChessTest.java

@ -72,4 +72,17 @@ class ChessTest {
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));
}
}
Loading…
Cancel
Save