Compare commits
2 Commits
15893d7bb8
...
7f113c4526
Author | SHA1 | Date | |
---|---|---|---|
7f113c4526 | |||
bb54a8a684 |
@ -5,6 +5,8 @@ public class ChessApp {
|
|||||||
final Piece p2 = new Piece(Kind.QUEEN, Color.WHITE, board, 4, 8);
|
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 p3 = new Piece(Kind.KNIGHT, Color.WHITE, board, 3, 3);
|
||||||
final Piece p4 = new Piece(Kind.KNIGHT, Color.BLACK, board, 4, 6);
|
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.printBoard(System.out);
|
||||||
board.check(System.out);
|
board.check(System.out);
|
||||||
}
|
}
|
||||||
|
@ -159,4 +159,10 @@ public class ChessTest {
|
|||||||
numPieces++;
|
numPieces++;
|
||||||
assertEquals(expectedNumPieces, 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 {
|
public enum Kind {
|
||||||
QUEEN, KNIGHT
|
QUEEN, KNIGHT, ROOK
|
||||||
}
|
}
|
||||||
|
@ -25,6 +25,8 @@ class Piece {
|
|||||||
return queenCharRep();
|
return queenCharRep();
|
||||||
case KNIGHT:
|
case KNIGHT:
|
||||||
return knightCharRep();
|
return knightCharRep();
|
||||||
|
case ROOK:
|
||||||
|
return rookCharRep();
|
||||||
}
|
}
|
||||||
throw new IllegalArgumentException("Unknown piece " + kind);
|
throw new IllegalArgumentException("Unknown piece " + kind);
|
||||||
}
|
}
|
||||||
@ -35,6 +37,8 @@ class Piece {
|
|||||||
return queenShowPiece();
|
return queenShowPiece();
|
||||||
case KNIGHT:
|
case KNIGHT:
|
||||||
return knightShowPiece();
|
return knightShowPiece();
|
||||||
|
case ROOK:
|
||||||
|
return rookShowPiece();
|
||||||
}
|
}
|
||||||
throw new IllegalArgumentException("Unknown piece " + kind);
|
throw new IllegalArgumentException("Unknown piece " + kind);
|
||||||
}
|
}
|
||||||
@ -45,6 +49,8 @@ class Piece {
|
|||||||
return queenCanCapture(other);
|
return queenCanCapture(other);
|
||||||
case KNIGHT:
|
case KNIGHT:
|
||||||
return knightCanCapture(other);
|
return knightCanCapture(other);
|
||||||
|
case ROOK:
|
||||||
|
return rookCanCapture(other);
|
||||||
}
|
}
|
||||||
throw new IllegalArgumentException("Unknown piece " + kind);
|
throw new IllegalArgumentException("Unknown piece " + kind);
|
||||||
}
|
}
|
||||||
@ -63,6 +69,14 @@ class Piece {
|
|||||||
return 'N';
|
return 'N';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
char rookCharRep() {
|
||||||
|
if (color == Color.WHITE) {
|
||||||
|
return 'r';
|
||||||
|
} else {
|
||||||
|
return 'R';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
String queenShowPiece() {
|
String queenShowPiece() {
|
||||||
return "" + color.toString().toLowerCase() + " queen at (" + row + ", " + col + ")";
|
return "" + color.toString().toLowerCase() + " queen at (" + row + ", " + col + ")";
|
||||||
}
|
}
|
||||||
@ -71,6 +85,10 @@ class Piece {
|
|||||||
return "" + color.toString().toLowerCase() + " knight at (" + row + ", " + col + ")";
|
return "" + color.toString().toLowerCase() + " knight at (" + row + ", " + col + ")";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
String rookShowPiece() {
|
||||||
|
return "" + color.toString().toLowerCase() + " rook at (" + row + ", " + col + ")";
|
||||||
|
}
|
||||||
|
|
||||||
boolean queenCanCapture(Piece other) {
|
boolean queenCanCapture(Piece other) {
|
||||||
if (board != other.board || color == other.color)
|
if (board != other.board || color == other.color)
|
||||||
return false;
|
return false;
|
||||||
@ -97,4 +115,22 @@ class Piece {
|
|||||||
final int dc = abs(col - other.col);
|
final int dc = abs(col - other.col);
|
||||||
return dr == 2 && dc == 1 || dr == 1 && dc == 2;
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
122
uebung02/rationals/Rational.java
Normal file
122
uebung02/rationals/Rational.java
Normal 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));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
23
uebung02/rationals/RationalDemo.java
Normal file
23
uebung02/rationals/RationalDemo.java
Normal 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)));
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user