10 Commits
e234eb2e50
...
4ef75e249b
6 changed files with 488 additions and 11 deletions
-
247src/main/java/Game/Chess.java
-
52src/main/java/Game/ChessObj/ChessBoard.java
-
2src/main/java/Game/ChessObj/ChessFigure.java
-
64src/test/java/Game/ChessObj/ChessBoardTest.java
-
2src/test/java/Game/ChessObj/ChessFigureTest.java
-
132src/test/java/Game/ChessTest.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; |
||||
|
} |
||||
|
|
||||
|
} |
@ -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()); |
||||
|
} |
||||
|
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue