Skip to content

Commit cf139d5

Browse files
committed
- Lecture on Thursday, 5th, 2026.
1 parent 4adb670 commit cf139d5

11 files changed

Lines changed: 238 additions & 161 deletions

File tree

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package de.hilling.chess;
2+
3+
public class ChessException extends RuntimeException {
4+
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package de.hilling.chess;
2+
3+
import java.util.LinkedList;
4+
import java.util.List;
5+
6+
/**
7+
* A (not so) small example for a chessboard maybe understanding Long algebraic notation (@see <a
8+
* href="https://en.wikipedia.org/wiki/Algebraic_notation_(chess)#Long_algebraic_notation">Wikipedia</a>).
9+
*/
10+
public class ChessGame {
11+
private ChessPiece[][] board = new ChessPiece[8][8];
12+
private List<String> moves = new LinkedList<>();
13+
14+
public ChessGame() {
15+
for(int x=0; x<8; x++) {
16+
setPieceForWhiteAndBlack(PieceType.PAWN, x, 1);
17+
}
18+
setPieceForWhiteAndBlack(PieceType.KING, 4, 0);
19+
setPieceForWhiteAndBlack(PieceType.ROOK, 0, 0);
20+
setPieceForWhiteAndBlack(PieceType.ROOK, 7, 0);
21+
setPieceForWhiteAndBlack(PieceType.KNIGHT, 1, 0);
22+
setPieceForWhiteAndBlack(PieceType.KNIGHT, 6, 0);
23+
setPieceForWhiteAndBlack(PieceType.BISHOP, 2, 0);
24+
setPieceForWhiteAndBlack(PieceType.BISHOP, 5, 0);
25+
setPieceForWhiteAndBlack(PieceType.QUEEN, 3, 0);
26+
}
27+
28+
private void setPiece(PieceType pieceType, Color color, int x, int y) {
29+
board[x][y] = new ChessPiece(pieceType, color);
30+
}
31+
32+
private void setPieceForWhiteAndBlack(PieceType pieceType, int x, int y) {
33+
setPiece(pieceType, Color.WHITE, x, y);
34+
setPiece(pieceType, Color.BLACK, x, 7-y);
35+
}
36+
37+
public ChessPiece get(Position position) {
38+
return board[position.x][position.y];
39+
}
40+
41+
public String toString() {
42+
StringBuilder sb = new StringBuilder();
43+
sb.append("-----------------\n");
44+
for(int y=7; y>=0; y--) {
45+
sb.append('|');
46+
for(int x=0; x<8; x++) {
47+
ChessPiece piece = board[x][y];
48+
if(piece != null) {
49+
sb.append(piece.toString());
50+
} else {
51+
sb.append(' ');
52+
}
53+
if(x<7) {
54+
sb.append(' ');
55+
}
56+
}
57+
sb.append("|\n");
58+
}
59+
sb.append("-----------------");
60+
return sb.toString();
61+
}
62+
63+
public void move(String move) {
64+
if(move.length() != 4) {
65+
throw new IllegalArgumentException("Move must be 4 characters long");
66+
}
67+
Position from = Position.of(move.substring(0, 2));
68+
Position to = Position.of(move.substring(2, 4));
69+
ChessPiece fromPiece = board[from.x][from.y];
70+
if(fromPiece != null) {
71+
board[to.x][to.y] = fromPiece;
72+
board[from.x][from.y] = null;
73+
moves.add(move);
74+
} else {
75+
throw new IllegalArgumentException("no piece on " + from);
76+
}
77+
}
78+
79+
public String getMoves() {
80+
String result = "";
81+
for(String move : moves) {
82+
result = result + move + '\n';
83+
}
84+
return result;
85+
}
86+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package de.hilling.chess;
2+
3+
public class ChessPiece {
4+
public final PieceType type;
5+
public final Color color;
6+
7+
public ChessPiece(PieceType type, Color color) {
8+
this.type = type;
9+
this.color = color;
10+
}
11+
12+
@Override
13+
public String toString() {
14+
return switch (color) {
15+
case WHITE -> type.shortcut().toUpperCase();
16+
case BLACK -> type.shortcut();
17+
};
18+
}
19+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package de.hilling.chess;
2+
3+
public enum Color {
4+
WHITE,
5+
BLACK
6+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package de.hilling.chess;
2+
3+
/**
4+
* Describe the type and include the shortcut for the piece.
5+
*/
6+
public enum PieceType {
7+
PAWN,
8+
KNIGHT,
9+
ROOK,
10+
QUEEN,
11+
KING,
12+
BISHOP;
13+
14+
public String shortcut() {
15+
if(this == KNIGHT) {
16+
return "n";
17+
} else {
18+
return name().substring(0, 1).toLowerCase();
19+
}
20+
}
21+
}

2026-02-05-Retro_Copilot/src/main/java/de/hilling/chess/Position.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import java.util.HashMap;
44
import java.util.Map;
5-
import java.util.regex.Pattern;
65

76
import org.jspecify.annotations.NonNull;
87

2026-02-05-Retro_Copilot/src/main/java/de/hilling/chess/Sort.java

Lines changed: 0 additions & 83 deletions
This file was deleted.
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
package de.hilling.chess;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.fail;
5+
6+
import org.junit.jupiter.api.BeforeEach;
7+
import org.junit.jupiter.api.Test;
8+
9+
class ChessGameTest {
10+
private static final String STARTPOSITION = """
11+
-----------------
12+
|r n b q k b n r|
13+
|p p p p p p p p|
14+
| |
15+
| |
16+
| |
17+
| |
18+
|P P P P P P P P|
19+
|R N B Q K B N R|
20+
-----------------""";
21+
private static final String E2E4_POSITION = """
22+
-----------------
23+
|r n b q k b n r|
24+
|p p p p p p p p|
25+
| |
26+
| |
27+
| P |
28+
| |
29+
|P P P P P P P|
30+
|R N B Q K B N R|
31+
-----------------""";
32+
private static final String E2E4E7E5_POSITION = """
33+
-----------------
34+
|r n b q k b n r|
35+
|p p p p p p p|
36+
| |
37+
| p |
38+
| P |
39+
| |
40+
|P P P P P P P|
41+
|R N B Q K B N R|
42+
-----------------""";
43+
44+
private ChessGame chessGame;
45+
46+
@BeforeEach
47+
void setUp() {
48+
chessGame = new ChessGame();
49+
}
50+
51+
// TODO: test more pieces, refactoring
52+
@Test
53+
void testSetup() {
54+
ChessPiece whitePawn = chessGame.get(Position.of(1, 1));
55+
assertEquals(PieceType.PAWN, whitePawn.type);
56+
assertEquals(Color.WHITE, whitePawn.color);
57+
ChessPiece blackPawn = chessGame.get(Position.of(7, 6));
58+
assertEquals(PieceType.PAWN, blackPawn.type);
59+
assertEquals(Color.BLACK, blackPawn.color);
60+
}
61+
62+
@Test
63+
void testSetupToString() {
64+
assertEquals(STARTPOSITION, chessGame.toString());
65+
}
66+
67+
@Test
68+
void illegalMoveLength() {
69+
try {
70+
chessGame.move("a2a4bb");
71+
fail("Illegal move exception");
72+
} catch (IllegalArgumentException iae) {
73+
// nothing to do
74+
}
75+
}
76+
77+
@Test
78+
void illegalMoveCharacters() {
79+
try {
80+
chessGame.move("a2z4");
81+
fail("Illegal move exception");
82+
} catch (IllegalArgumentException iae) {
83+
assertEquals("Illegal position: z4", iae.getMessage());
84+
}
85+
}
86+
87+
@Test
88+
void movePawn() {
89+
chessGame.move("e2e4");
90+
assertEquals(E2E4_POSITION, chessGame.toString());
91+
}
92+
@Test
93+
94+
void movePawns() {
95+
chessGame.move("e2e4");
96+
chessGame.move("e7e5");
97+
assertEquals(E2E4E7E5_POSITION, chessGame.toString());
98+
assertEquals("e2e4\ne7e5\n", chessGame.getMoves());
99+
}
100+
}

2026-02-05-Retro_Copilot/src/test/java/de/hilling/chess/PositionGenericsTest.java

Lines changed: 0 additions & 48 deletions
This file was deleted.

2026-02-05-Retro_Copilot/src/test/java/de/hilling/chess/PositionTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package de.hilling.chess;
22

3-
import org.junit.jupiter.api.Test;
4-
53
import static org.junit.jupiter.api.Assertions.*;
64

5+
import org.junit.jupiter.api.Test;
6+
77
class PositionTest {
88

99
@Test

0 commit comments

Comments
 (0)