uebung02 aufgabe 1
This commit is contained in:
parent
15893d7bb8
commit
bb54a8a684
@ -5,6 +5,8 @@ public class ChessApp {
|
||||
final Piece p2 = new Piece(Kind.QUEEN, Color.WHITE, board, 4, 8);
|
||||
final Piece p3 = new Piece(Kind.KNIGHT, Color.WHITE, board, 3, 3);
|
||||
final Piece p4 = new Piece(Kind.KNIGHT, Color.BLACK, board, 4, 6);
|
||||
final Piece p5 = new Piece(Kind.ROOK, Color.BLACK, board, 7, 6);
|
||||
final Piece p6 = new Piece(Kind.ROOK, Color.WHITE, board, 7, 2);
|
||||
board.printBoard(System.out);
|
||||
board.check(System.out);
|
||||
}
|
||||
|
@ -159,4 +159,10 @@ public class ChessTest {
|
||||
numPieces++;
|
||||
assertEquals(expectedNumPieces, numPieces);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRunWithErrors() {
|
||||
final Piece rook = new Piece(Kind.ROOK, Color.WHITE, board, 1, 1);
|
||||
assertEquals(Kind.ROOK, board.pieceAt(1, 1).kind);
|
||||
}
|
||||
}
|
@ -1,3 +1,3 @@
|
||||
public enum Kind {
|
||||
QUEEN, KNIGHT
|
||||
QUEEN, KNIGHT, ROOK
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user