uebung02 aufgabe 1

This commit is contained in:
2025-04-28 15:15:53 +02:00
parent 15893d7bb8
commit bb54a8a684
4 changed files with 45 additions and 1 deletions

View File

@@ -25,6 +25,8 @@ class Piece {
return queenCharRep();
case KNIGHT:
return knightCharRep();
case ROOK:
return rookCharRep();
}
throw new IllegalArgumentException("Unknown piece " + kind);
}
@@ -35,6 +37,8 @@ class Piece {
return queenShowPiece();
case KNIGHT:
return knightShowPiece();
case ROOK:
return rookShowPiece();
}
throw new IllegalArgumentException("Unknown piece " + kind);
}
@@ -45,6 +49,8 @@ class Piece {
return queenCanCapture(other);
case KNIGHT:
return knightCanCapture(other);
case ROOK:
return rookCanCapture(other);
}
throw new IllegalArgumentException("Unknown piece " + kind);
}
@@ -63,6 +69,14 @@ class Piece {
return 'N';
}
char rookCharRep() {
if (color == Color.WHITE) {
return 'r';
} else {
return 'R';
}
}
String queenShowPiece() {
return "" + color.toString().toLowerCase() + " queen at (" + row + ", " + col + ")";
}
@@ -71,6 +85,10 @@ class Piece {
return "" + color.toString().toLowerCase() + " knight at (" + row + ", " + col + ")";
}
String rookShowPiece() {
return "" + color.toString().toLowerCase() + " rook at (" + row + ", " + col + ")";
}
boolean queenCanCapture(Piece other) {
if (board != other.board || color == other.color)
return false;
@@ -97,4 +115,22 @@ class Piece {
final int dc = abs(col - other.col);
return dr == 2 && dc == 1 || dr == 1 && dc == 2;
}
boolean rookCanCapture(Piece other) {
if (board != other.board || color == other.color)
return false;
if (other.row != row && other.col != col )
return false;
final int dr = signum(other.row - row);
final int dc = signum(other.col - col);
int r = row + dr;
int c = col + dc;
while (r != other.row || c != other.col) {
if (board.pieceAt(r, c) != null) return false;
r += dr;
c += dc;
}
return true;
}
}