From bb54a8a684fbbd91bf61a2821e859f2fdb0ef2f6 Mon Sep 17 00:00:00 2001 From: Peet Date: Mon, 28 Apr 2025 15:15:53 +0200 Subject: [PATCH] uebung02 aufgabe 1 --- uebung02/chess/ChessApp.java | 2 ++ uebung02/chess/ChessTest.java | 6 ++++++ uebung02/chess/Kind.java | 2 +- uebung02/chess/Piece.java | 36 +++++++++++++++++++++++++++++++++++ 4 files changed, 45 insertions(+), 1 deletion(-) diff --git a/uebung02/chess/ChessApp.java b/uebung02/chess/ChessApp.java index 6ab4d38..632ade0 100644 --- a/uebung02/chess/ChessApp.java +++ b/uebung02/chess/ChessApp.java @@ -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); } diff --git a/uebung02/chess/ChessTest.java b/uebung02/chess/ChessTest.java index 808176c..feda3b7 100644 --- a/uebung02/chess/ChessTest.java +++ b/uebung02/chess/ChessTest.java @@ -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); + } } \ No newline at end of file diff --git a/uebung02/chess/Kind.java b/uebung02/chess/Kind.java index 8bb73c3..bec56be 100644 --- a/uebung02/chess/Kind.java +++ b/uebung02/chess/Kind.java @@ -1,3 +1,3 @@ public enum Kind { - QUEEN, KNIGHT + QUEEN, KNIGHT, ROOK } diff --git a/uebung02/chess/Piece.java b/uebung02/chess/Piece.java index 98da724..3255217 100644 --- a/uebung02/chess/Piece.java +++ b/uebung02/chess/Piece.java @@ -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; + } + }