You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
164 lines
5.7 KiB
164 lines
5.7 KiB
package Game;
|
|
|
|
import Game.ChessObj.ChessBoard;
|
|
import Game.ChessObj.ChessFigure;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
public class Chess extends Game {
|
|
|
|
private ChessFigure.Team currentTeam;
|
|
|
|
private ArrayList<ChessFigure> destroyedWhiteFigures;
|
|
private ArrayList<ChessFigure> destroyedBlackFigures;
|
|
|
|
private ChessBoard chessBoard;
|
|
|
|
public Chess() {
|
|
init();
|
|
}
|
|
|
|
private void init() {
|
|
chessBoard = new ChessBoard();
|
|
currentTeam = ChessFigure.Team.WHITE;
|
|
destroyedWhiteFigures = new ArrayList<>();
|
|
destroyedBlackFigures = new ArrayList<>();
|
|
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;
|
|
}
|
|
|
|
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);
|
|
if (targetFigure.getTeam() == ChessFigure.Team.WHITE)
|
|
getDestroyedWhiteFigures().add(targetFigure);
|
|
else
|
|
getDestroyedBlackFigures().add(targetFigure);
|
|
} 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;
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
}
|