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