Skip to content

Commit 550e52a

Browse files
authored
Added stable files
1 parent bdb9682 commit 550e52a

File tree

7 files changed

+844
-0
lines changed

7 files changed

+844
-0
lines changed

CHE4C/AntichessCheckerboard.pde

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
import java.util.Map.Entry;
2+
3+
{
4+
checkerboardClasses.add(AntichessCheckerboard.class);
5+
}
6+
7+
/**
8+
* Implementierung eines Schachbretts für den Schachmodus "Räuberschach".
9+
*/
10+
class AntichessCheckerboard extends Checkerboard {
11+
12+
/**
13+
* Erstellt eine Instanz von AntichessCheckerboard.
14+
*/
15+
public AntichessCheckerboard() {
16+
super();
17+
}
18+
19+
/**
20+
* Erstellt eine Instanz von AntichessCheckerboard.
21+
*
22+
* @param initial Wenn true, wird die Startaufstellung auf das Schachbrett gelegt. Sonst ist das Schachbrett leer.
23+
*/
24+
public AntichessCheckerboard(boolean initial) {
25+
super(initial);
26+
}
27+
28+
/**
29+
* Erstellt eine Instanz von AntichessCheckerboard aus einer anderen Instanz.
30+
* Es wird also eine (tiefe) Kopie der anderen Instanz erstellt.
31+
*
32+
* @param board die andere Instanz, welche kopiert wird
33+
*/
34+
public AntichessCheckerboard(Checkerboard board) {
35+
super(board);
36+
}
37+
38+
@Override
39+
public int getScore(ChessFigureColor chessColor) {
40+
return -super.getScore(chessColor);
41+
}
42+
43+
@Override
44+
public int getFigureScore(ChessFigure figure) {
45+
return figure.type == ChessFigureType.KING ? (int) (ChessFigureType.PAWN.getRelativeValue() * 2.5) : super.getFigureScore(figure);
46+
}
47+
48+
@Override
49+
public boolean hasLost(ChessFigureColor chessColor) {
50+
return this.hasWon(chessColor.getOpposing());
51+
}
52+
53+
@Override
54+
public boolean hasWon(ChessFigureColor chessColor) {
55+
if (this.getSuccessionalBoards(chessColor).isEmpty())
56+
return true;
57+
for (ChessFigure figure : this.figures)
58+
if (figure.chessColor == chessColor)
59+
return false;
60+
return true;
61+
}
62+
63+
@Override
64+
public HashMap<Checkerboard, ChessMovement> getSuccessionalBoards() {
65+
return this.filterBoards(super.getSuccessionalBoards());
66+
}
67+
68+
@Override
69+
public HashMap<Checkerboard, ChessMovement> getSuccessionalBoards(ChessFigureColor chessColor) {
70+
return this.filterBoards(super.getSuccessionalBoards(chessColor));
71+
}
72+
73+
@Override
74+
public HashMap<Checkerboard, ChessMovement> getSuccessionalBoards(ChessFigure figure) {
75+
return this.filterBoards(super.getSuccessionalBoards(figure));
76+
}
77+
78+
@Override
79+
protected HashMap<Checkerboard, ChessMovement> getSuccessionalBoardsWithPawn(ChessFigure figure) {
80+
HashMap<Checkerboard, ChessMovement> boards = super.getSuccessionalBoardsWithPawn(figure);
81+
HashMap<Checkerboard, ChessMovement> kings = new HashMap<Checkerboard, ChessMovement>();
82+
83+
for (Entry<Checkerboard, ChessMovement> entry : boards.entrySet()) {
84+
if (entry.getValue().toType != null && entry.getValue().toType != ChessFigureType.PAWN) {
85+
Checkerboard board = new Checkerboard(this);
86+
ChessMovement movement = new ChessMovement(entry.getValue());
87+
movement.toType = ChessFigureType.KING;
88+
movement.apply(board);
89+
kings.put(board, movement);
90+
}
91+
}
92+
93+
boards.putAll(kings);
94+
return boards;
95+
}
96+
97+
/**
98+
* Entfernt alle invaliden Züge, die aufgrund von Sonderregeln wegfallen, aus einer Zuordnung möglicher Züge.
99+
*
100+
* @param boards die Zuordnung möglicher Züge
101+
* @return die Zuordnung möglicher Züge nach dem Entfernen invalider Züge
102+
*/
103+
protected HashMap<Checkerboard, ChessMovement> filterBoards(HashMap<Checkerboard, ChessMovement> boards) {
104+
HashMap<Checkerboard, ChessMovement> filtered = new HashMap<Checkerboard, ChessMovement>();
105+
int minFigures = this.figures.size();
106+
107+
for (Entry<Checkerboard, ChessMovement> entry : boards.entrySet()) {
108+
if (entry.getKey().figures.size() < minFigures) {
109+
minFigures = entry.getKey().figures.size();
110+
filtered.clear();
111+
}
112+
if (entry.getKey().figures.size() == minFigures)
113+
filtered.put(new AntichessCheckerboard(entry.getKey()), entry.getValue());
114+
}
115+
116+
return filtered;
117+
}
118+
119+
}

CHE4C/ChessFigure.pde

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
/**
2+
* Implementierung einer Schachfigur.
3+
*/
4+
class ChessFigure {
5+
6+
public ChessFigureType type;
7+
public ChessFigureColor chessColor;
8+
9+
protected byte positionX;
10+
protected byte positionY;
11+
12+
protected boolean moved = false;
13+
14+
/**
15+
* Erstellt eine Instanz von ChessFigure.
16+
*
17+
* @param type Typ der Figur (Dame, König, Bauer, etc.)
18+
* @param chessColor Spielerfarbe der Figur (Schwarz / Weiß)
19+
* @param positionX die x-Position der Figur auf dem Brett (0 - 7)
20+
* @param positionY die y-Position der Figur auf dem Brett (0 - 7)
21+
*/
22+
public ChessFigure(ChessFigureType type, ChessFigureColor chessColor, byte positionX, byte positionY) {
23+
this.type = type;
24+
this.chessColor = chessColor;
25+
this.positionX = positionX;
26+
this.positionY = positionY;
27+
}
28+
29+
/**
30+
* Erstellt eine Instanz von ChessFigure aus einer anderen Instanz.
31+
* Es wird also eine (tiefe) Kopie der anderen Instanz erstellt.
32+
*
33+
* @param figure die andere Instanz, welche kopiert wird
34+
*/
35+
public ChessFigure(ChessFigure figure) {
36+
this.type = figure.type;
37+
this.chessColor = figure.chessColor;
38+
this.positionX = figure.positionX;
39+
this.positionY = figure.positionY;
40+
this.moved = figure.moved;
41+
}
42+
43+
/**
44+
* Gibt den Namen der Figur zurück. (SPIELERFARBE_TYP)
45+
*
46+
* @return der Figurenname
47+
*/
48+
public String getFigureName() {
49+
return this.chessColor.toString() + "_" + this.type.toString();
50+
}
51+
52+
/**
53+
* Gibt die x-Position der Figur zurück.
54+
*
55+
* @return die x-Position
56+
*/
57+
public byte getPositionX() {
58+
return this.positionX;
59+
}
60+
61+
/**
62+
* Gibt die y-Position der Figur zurück.
63+
*
64+
* @return die y-Position
65+
*/
66+
public byte getPositionY() {
67+
return this.positionY;
68+
}
69+
70+
/**
71+
* Setzt die x-Position der Figur, sodass die Figur bewegt wird.
72+
*
73+
* @param x die x-Position
74+
*/
75+
public void setPositionX(byte x) {
76+
this.positionX = x;
77+
this.moved = true;
78+
}
79+
80+
/**
81+
* Setzt die y-Position der Figur, sodass die Figur bewegt wird.
82+
*
83+
* @param y die y-Position
84+
*/
85+
public void setPositionY(byte y) {
86+
this.positionY = y;
87+
this.moved = true;
88+
}
89+
90+
/**
91+
* Setzt die Position der Figur, sodass die Figur bewegt wird.
92+
*
93+
* @param x die x-Position
94+
* @param y die y-Position
95+
*/
96+
public void setPosition(byte x, byte y) {
97+
this.setPositionX(x);
98+
this.setPositionY(y);
99+
}
100+
101+
/**
102+
* Gibt zurück, ob die Figur bewegt wurde.
103+
*
104+
* @return ob die Figur jemals bewegt wurde.
105+
*/
106+
public boolean hasMoved() {
107+
return this.moved;
108+
}
109+
110+
}

CHE4C/ChessFigureColor.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/**
2+
* Aufzählung für die Spielerfarbe von Schachfiguren.
3+
*/
4+
enum ChessFigureColor {
5+
WHITE,
6+
BLACK;
7+
8+
/**
9+
* Gibt die gegnerische Spielerfarbe zurück.
10+
*
11+
* @return die gegnerische Spielerfarbe
12+
*/
13+
ChessFigureColor getOpposing() {
14+
return this == WHITE ? BLACK : WHITE;
15+
}
16+
}

CHE4C/ChessFigureType.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* Aufzählung für die Schachfigur-Art.
3+
*/
4+
enum ChessFigureType {
5+
KING(10000),
6+
QUEEN(900),
7+
ROOK(500),
8+
BISHOP(300),
9+
KNIGHT(300),
10+
PAWN(100);
11+
12+
protected int relativeValue;
13+
14+
ChessFigureType(int relativeValue) {
15+
this.relativeValue = relativeValue;
16+
}
17+
18+
/**
19+
* Gibt den relativen Figurenwert zurück.
20+
*
21+
* @return der Figurenwert (in [vielfachen] Bauerneinheiten)
22+
*/
23+
int getRelativeValue() {
24+
return this.relativeValue;
25+
}
26+
27+
}

CHE4C/ChessMovement.pde

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
/**
2+
* Implementierung eines Schachzuges, welcher mit dem SChFiM-Protokoll arbeiten kann.
3+
*/
4+
class ChessMovement {
5+
6+
protected final static String REMOVE_SYNTAX = "/r %d %d \n";
7+
protected final static String MOVE_SYNTAX = "/m %d %d %d %d \n";
8+
9+
protected byte fromX;
10+
protected byte fromY;
11+
protected byte toX;
12+
protected byte toY;
13+
protected boolean castling;
14+
protected boolean removes;
15+
protected ChessFigureType toType;
16+
protected int score = Integer.MIN_VALUE + 1;
17+
18+
/**
19+
* Erstellt eine Instanz von ChessMovement.
20+
*
21+
* @param fromX die x-Position der Figur auf dem Brett (0 - 7)
22+
* @param fromY die y-Position der Figur auf dem Brett (0 - 7)
23+
* @param toX die neue x-Position auf dem Brett (0 - 7)
24+
* @param toY die neue y-Position auf dem Brett (0 - 7)
25+
* @param castling ob der Zug eine Rochade darstellt, d.h. ob die Figur auf dem Zielfeld auch bewegt werden soll
26+
* @param removes ob der Schachzug eine Figur entfernt
27+
* @param toType Typ der Figur, falls sich dieser verändert (Dame, König, Bauer, etc.)
28+
*/
29+
public ChessMovement(byte fromX, byte fromY, byte toX, byte toY, boolean castling, boolean removes, ChessFigureType toType) {
30+
this.fromX = fromX;
31+
this.fromY = fromY;
32+
this.toX = toX;
33+
this.toY = toY;
34+
this.castling = castling;
35+
this.removes = removes;
36+
this.toType = toType;
37+
}
38+
39+
/**
40+
* Erstellt eine Instanz von ChessMovement.
41+
*
42+
* @param fromX die x-Position der Figur auf dem Brett (0 - 7)
43+
* @param fromY die y-Position der Figur auf dem Brett (0 - 7)
44+
* @param toX die neue x-Position auf dem Brett (0 - 7)
45+
* @param toY die neue y-Position auf dem Brett (0 - 7)
46+
* @param castling ob der Zug eine Rochade darstellt, d.h. ob die Figur auf dem Zielfeld auch bewegt werden soll
47+
* @param removes ob der Schachzug eine Figur entfernt
48+
*/
49+
public ChessMovement(byte fromX, byte fromY, byte toX, byte toY, boolean castling, boolean removes) {
50+
this(fromX, fromY, toX, toY, castling, removes, null);
51+
}
52+
53+
/**
54+
* Erstellt eine Instanz von ChessMovement aus einer anderen Instanz.
55+
* Es wird also eine (tiefe) Kopie der anderen Instanz erstellt.
56+
*
57+
* @param movement die andere Instanz, welche kopiert wird
58+
*/
59+
public ChessMovement(ChessMovement movement) {
60+
this.fromX = movement.fromX;
61+
this.fromY = movement.fromY;
62+
this.toX = movement.toX;
63+
this.toY = movement.toY;
64+
this.castling = movement.castling;
65+
this.removes = movement.removes;
66+
this.toType = movement.toType;
67+
}
68+
69+
/**
70+
* Führt den Bewegungsablauf auf einem Schachbrett aus.
71+
*
72+
* @param board Schachbrett, auf dem der Bewegungsablauf ausgeführt wird
73+
*/
74+
public void apply(Checkerboard board) {
75+
if (this.removes)
76+
board.figures.remove(board.getFigureOn(this.toX, this.toY));
77+
if (this.castling) {
78+
if (board.getFigureOn(this.fromX, this.fromY).chessColor == ChessFigureColor.WHITE)
79+
board.whiteUsedCastling = true;
80+
else
81+
board.blackUsedCastling = true;
82+
// (n >> 7) | 1 gibt für ein byte n dessen Vorzeichen an
83+
board.getFigureOn(this.fromX, this.fromY).setPosition((byte) ((((this.toX - this.fromX) >> 7) | 1) * 2 + this.fromX), this.toY);
84+
board.getFigureOn(this.toX, this.toY).setPosition((byte) ((((this.toX - this.fromX) >> 7) | 1) + this.fromX), this.fromY);
85+
} else
86+
board.getFigureOn(this.fromX, this.fromY).setPosition(this.toX, this.toY);
87+
if (this.toType != null)
88+
board.getFigureOn(this.toX, this.toY).type = this.toType;
89+
board.boardScore = null;
90+
}
91+
92+
/**
93+
* Gibt den Schachzug als SChFiM-Befehl zurück.
94+
*
95+
* @return ein String, welcher den Befehl (bzw. meherere Befehle) enthält
96+
*/
97+
public String toSChFiMCommandSequence() {
98+
String command = "";
99+
if (this.removes)
100+
command += String.format(REMOVE_SYNTAX, this.toX, this.toY);
101+
if (this.castling) {
102+
// (n >> 7) | 1 gibt für ein byte n dessen Vorzeichen an
103+
command += String.format(MOVE_SYNTAX, this.fromX, this.fromY, (((this.toX - this.fromX) >> 7) | 1) * 2 + this.fromX, this.toY);
104+
command += String.format(MOVE_SYNTAX, this.toX, this.toY, (((this.toX - this.fromX) >> 7) | 1) + this.fromX, this.fromY);
105+
} else
106+
command += String.format(MOVE_SYNTAX, this.fromX, this.fromY, this.toX, this.toY);
107+
return command;
108+
}
109+
110+
}

0 commit comments

Comments
 (0)