color changes in console + promotion

This commit is contained in:
Janet-Doe
2025-04-13 00:56:44 +02:00
parent 39c289cc47
commit 9f44548843
3 changed files with 38 additions and 34 deletions

View File

@@ -38,4 +38,8 @@ public class Coordinate {
} }
return false; return false;
} }
public String toString() {
return "(" + this.x + ", " + this.y + ")";
}
} }

View File

@@ -7,15 +7,17 @@ public class Colors {
// Regular Colors // Regular Colors
public static final String BLACK = "\033[38;2;0;0;0m"; public static final String BLACK = "\033[38;2;0;0;0m";
public static final String WHITE = "\033[38;2;255;255;255m"; public static final String WHITE = "\033[38;2;255;255;255m";
public static final String RED = "\u001B[31m"; public static final String RED = "\033[38;2;255;0;0m";
public static final String GREEN = "\u001B[32m"; public static final String GREEN = "\033[38;2;0;255;0m";
public static final String YELLOW = "\u001B[33m"; public static final String YELLOW = "\033[38;2;255;255;0m";
public static final String BLUE = "\u001B[34m"; public static final String BLUE = "\033[38;2;0;0;255m";
public static final String PURPLE = "\u001B[35m"; public static final String PURPLE = "\033[38;2;255;0;255m";
public static final String CYAN = "\u001B[36m"; public static final String CYAN = "\033[38;2;0;255;255m";
// Background // Background
public static final String BLACK_BACKGROUND = "\033[40m"; // BLACK public static final String BLACK_BACKGROUND = "\033[40m"; // BLACK
public static final String LIGHT_GRAY_BACKGROUND = "\033[48;2;180;180;180m";
public static final String DARK_GRAY_BACKGROUND = "\033[48;2;120;120;120m";
public static final String RED_BACKGROUND = "\033[41m"; // RED public static final String RED_BACKGROUND = "\033[41m"; // RED
public static final String GREEN_BACKGROUND = "\033[42m"; // GREEN public static final String GREEN_BACKGROUND = "\033[42m"; // GREEN
public static final String YELLOW_BACKGROUND = "\033[43m"; // YELLOW public static final String YELLOW_BACKGROUND = "\033[43m"; // YELLOW

View File

@@ -56,7 +56,6 @@ public class Console implements GameListener {
@Override @Override
public void playerTurn(Color color) { public void playerTurn(Color color) {
updateDisplay();
System.out.println(Colors.RED + "Player turn: " + color + Colors.RESET); System.out.println(Colors.RED + "Player turn: " + color + Colors.RESET);
boolean endTurn; boolean endTurn;
do { do {
@@ -74,6 +73,7 @@ public class Console implements GameListener {
}; };
} while (!endTurn); } while (!endTurn);
System.out.println(Colors.RED + "Turn ended." + Colors.RESET); System.out.println(Colors.RED + "Turn ended." + Colors.RESET);
updateDisplay();
} }
private boolean playerPickedSurrender(Color color) { private boolean playerPickedSurrender(Color color) {
@@ -88,12 +88,14 @@ public class Console implements GameListener {
System.out.println("New position: "); System.out.println("New position: ");
Coordinate end = stringToCoordinate(scanner.nextLine()); Coordinate end = stringToCoordinate(scanner.nextLine());
Command.CommandResult result = sendCommand(new MoveCommand(new Move(start, end))); Command.CommandResult result = sendCommand(new MoveCommand(new Move(start, end)));
if (Objects.requireNonNull(result) == Command.CommandResult.Moved) { switch (Objects.requireNonNull(result)) {
updateDisplay(); case Command.CommandResult.Moved: return true;
return true; case Command.CommandResult.ActionNeeded: updateDisplay(); promotePawn(end); return true;
} default: System.out.println(result);
System.out.println("Move not allowed."); System.out.println("Move not allowed.");
return false; return false;
}
} catch (Exception e) { } catch (Exception e) {
System.out.println(e.getMessage()); System.out.println(e.getMessage());
return false; return false;
@@ -152,7 +154,7 @@ public class Console implements GameListener {
@Override @Override
public void gameStarted() { public void gameStarted() {
System.out.println("Game start:"); System.out.println("Game start!");
updateDisplay(); updateDisplay();
} }
@@ -195,10 +197,10 @@ public class Console implements GameListener {
for (int j = 0; j < Coordinate.VALUE_MAX; j++) { for (int j = 0; j < Coordinate.VALUE_MAX; j++) {
Piece p = pieceAt(j, i); Piece p = pieceAt(j, i);
if ((i+j)%2==0) { if ((i+j)%2==0) {
string.append(Colors.WHITE_BACKGROUND); string.append(Colors.LIGHT_GRAY_BACKGROUND);
} }
else { else {
string.append(Colors.BLACK_BACKGROUND); string.append(Colors.DARK_GRAY_BACKGROUND);
} }
if (p == null) { if (p == null) {
string.append(" " + Colors.RESET); string.append(" " + Colors.RESET);
@@ -221,24 +223,20 @@ public class Console implements GameListener {
Coordinate currentCell = new Coordinate(j, i); Coordinate currentCell = new Coordinate(j, i);
Piece p = pieceAt(j, i); Piece p = pieceAt(j, i);
if (moves.contains(currentCell)){ if (moves.contains(currentCell)){
if ((i + j) % 2 == 0) {
string.append(Colors.YELLOW_BACKGROUND); string.append(Colors.YELLOW_BACKGROUND);
} else {
string.append(Colors.RED_BACKGROUND);
}
} else { } else {
if ((i + j) % 2 == 0) { if ((i + j) % 2 == 0) {
string.append(Colors.WHITE_BACKGROUND); string.append(Colors.LIGHT_GRAY_BACKGROUND);
} else { } else {
string.append(Colors.BLACK_BACKGROUND); string.append(Colors.DARK_GRAY_BACKGROUND);
} }
} }
if (p == null) { if (p == null) {
string.append(" " + Colors.RESET); string.append(" " + Colors.RESET);
} }
else { else {
if (currentCell == piece) { if (currentCell.equals(piece)) {
string.append(Colors.RED).append(" ").append(consolePieceName.getString(p)).append(" ").append(Colors.RESET); string.append(Colors.RED_BACKGROUND).append(" ").append(consolePieceName.getString(p)).append(" ").append(Colors.RESET);
} else { } else {
string.append(" ").append(consolePieceName.getString(p)).append(" ").append(Colors.RESET); string.append(" ").append(consolePieceName.getString(p)).append(" ").append(Colors.RESET);
} }