Compare commits

..

2 Commits

Author SHA1 Message Date
7f113c4526 uebung02 aufgabe 2 2025-04-28 15:16:11 +02:00
bb54a8a684 uebung02 aufgabe 1 2025-04-28 15:15:53 +02:00
6 changed files with 190 additions and 1 deletions

View File

@ -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);
}

View File

@ -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);
}
}

View File

@ -1,3 +1,3 @@
public enum Kind {
QUEEN, KNIGHT
QUEEN, KNIGHT, ROOK
}

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;
}
}

View File

@ -0,0 +1,122 @@
package uebung02.rationals;
public class Rational {
private final int num, denom;
Rational(int num, int denom) {
if (denom == 0) {
throw new IllegalArgumentException("Zahl darf nicht 0 sein!");
} else {
int gcd = gcDivider(num, denom);
if (denom < 0) {
this.num = -num / gcd;
this.denom = -denom / gcd;
} else {
this.num = num / gcd;
this.denom = denom / gcd;
}
}
}
Rational (int num) {
this(num, 1);
}
private int gcDivider(int x, int y) {
//return x == 0 ? y : gcDivider(y % x, x);
if (x == 0) {
return y;
} else {
return gcDivider(y % x, x);
}
}
public int getNum() {
return num;
}
public int getDenom() {
return denom;
}
public Rational add(Rational other) {
return new Rational(num * other.getDenom() + denom * other.getNum(),
denom * other.getDenom());
}
public Rational add(int i) {
return add(new Rational(i));
}
public Rational sub(Rational r) {
return new Rational(num * r.denom - denom * r.num, denom * r.denom);
}
public Rational sub(int i) {
return this.sub(new Rational(i));
}
public Rational mult(Rational r) {
return new Rational(num * r.num, denom * r.denom);
}
public Rational mult(int i) {
return mult(new Rational(i));
}
public Rational div(Rational r) {
return new Rational(num * r.denom, denom * r.num);
}
public Rational div(int i) {
return div(new Rational(i));
}
@Override
public String toString() {
if (denom == 1) {
return "" + num;
} else {
return num + "/" + denom;
}
}
public boolean lessThan(Rational r) {
return this.sub(r).num < 0;
}
public boolean lessThan(int i) {
return this.sub(i).num < 0;
}
public boolean greaterThan(Rational r) {
return this.sub(r).num > 0;
}
public boolean greaterThan(int i) {
return this.sub(i).num > 0;
}
public boolean lessThanOrEqual(Rational r) {
return this.sub(r).num <= 0;
}
public boolean lessThanOrEqual(int i) {
return this.sub(i).num <= 0;
}
public boolean greaterThanOrEqual(Rational r) {
return this.sub(r).num >= 0;
}
public boolean isEqual(Rational r) {
return this.num == r.num && this.denom == r.denom;
}
public boolean isEqual(int i) {
return this.isEqual(new Rational(i));
}
}

View File

@ -0,0 +1,23 @@
package uebung02.rationals;
public class RationalDemo {
public static void main(String[] args) {
Rational r1 = new Rational(1, 2);
Rational r2 = new Rational(2, 4);
Rational r3 = new Rational(6, 18);
Rational r4 = new Rational(3, 12);
Rational r5 = new Rational(1);
Rational r6 = new Rational(2);
Rational r7 = new Rational(4, 3);
System.out.println("1/2 = " + r1);
System.out.println("2/4 = " + r2);
System.out.println("6/18 = " + r3);
System.out.println("1/2 + 2/4 = " + r1.add(r2));
System.out.println("1/2 - 6/18 = " + r1.sub(r3));
System.out.println("6/18 * 2/4 = " + r3.mult(r2));
System.out.println("1/2 == 2/4 ? " + r1.isEqual(r2));
System.out.println("2/4 == 3/12 ? " + r2.isEqual(r4));
System.out.println("1 + 2 / (4/3) = " + r5.add(r6.div(r7)));
}
}