Skip to content

Commit 36df7c6

Browse files
committed
Track half-move and full-move clocks in MinChess. Update related classes and add tests for clock functionality.
1 parent 07a3885 commit 36df7c6

6 files changed

Lines changed: 71 additions & 2 deletions

File tree

src/main/java/net/chesstango/gardel/minchess/MinChess.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ public MinChess(boolean whiteTurn,
3333
boolean castlingWhiteKingAllowed,
3434
boolean castlingWhiteQueenAllowed,
3535
long enPassantSquare,
36+
int halfMoveClock,
37+
int fullMoveClock,
3638
long whitePositions,
3739
long blackPositions,
3840
long kingPositions,
@@ -48,6 +50,8 @@ public MinChess(boolean whiteTurn,
4850
castlingWhiteKingAllowed,
4951
castlingWhiteQueenAllowed,
5052
enPassantSquare,
53+
halfMoveClock,
54+
fullMoveClock,
5155
whitePositions,
5256
blackPositions,
5357
kingPositions,
@@ -184,6 +188,8 @@ public MinChess clone() {
184188
workspace.castlingWhiteKingAllowed,
185189
workspace.castlingWhiteQueenAllowed,
186190
workspace.enPassantSquare,
191+
workspace.halfMoveClock,
192+
workspace.fullMoveClock,
187193
workspace.whitePositions,
188194
workspace.blackPositions,
189195
workspace.kingPositions,
@@ -224,6 +230,14 @@ public boolean getTurn() {
224230
return workspace.whiteTurn;
225231
}
226232

233+
public int getHalfMoveClock() {
234+
return workspace.halfMoveClock;
235+
}
236+
237+
public int getFullMoveClock() {
238+
return workspace.fullMoveClock;
239+
}
240+
227241
public static MinChess from(FEN fen) {
228242
MinChessBuilder builder = new MinChessBuilder();
229243
fen.export(builder);

src/main/java/net/chesstango/gardel/minchess/MinChessBuilder.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ public MinChess getPositionRepresentation() {
1616
castlingWhiteKingAllowed,
1717
castlingWhiteQueenAllowed,
1818
enPassantSquare,
19+
halfMoveClock,
20+
fullMoveClock,
1921
whitePositions,
2022
blackPositions,
2123
kingPositions,

src/main/java/net/chesstango/gardel/minchess/MinChessExporter.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ public void export(MinChess minChess) {
2323
this.positionBuilder.withCastlingBlackQueenAllowed(workspace.castlingBlackQueenAllowed);
2424
this.positionBuilder.withCastlingWhiteKingAllowed(workspace.castlingWhiteKingAllowed);
2525
this.positionBuilder.withCastlingWhiteQueenAllowed(workspace.castlingWhiteQueenAllowed);
26+
this.positionBuilder.withHalfMoveClock(workspace.halfMoveClock);
27+
this.positionBuilder.withFullMoveClock(workspace.fullMoveClock);
2628

2729
if (workspace.enPassantSquare != 0) {
2830
final int enPassantSquareIdx = Long.numberOfTrailingZeros(workspace.enPassantSquare);

src/main/java/net/chesstango/gardel/minchess/MinChessWorkspace.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ class MinChessWorkspace {
1111
boolean castlingBlackQueenAllowed;
1212
boolean castlingWhiteKingAllowed;
1313
boolean castlingWhiteQueenAllowed;
14+
int halfMoveClock;
15+
int fullMoveClock;
1416

1517
long enPassantSquare = 0;
1618
long whitePositions = 0;
@@ -29,6 +31,8 @@ class MinChessWorkspace {
2931
boolean castlingWhiteKingAllowed,
3032
boolean castlingWhiteQueenAllowed,
3133
long enPassantSquare,
34+
int halfMoveClock,
35+
int fullMoveClock,
3236
long whitePositions,
3337
long blackPositions,
3438
long kingPositions,
@@ -44,6 +48,9 @@ class MinChessWorkspace {
4448
this.castlingWhiteKingAllowed = castlingWhiteKingAllowed;
4549
this.castlingWhiteQueenAllowed = castlingWhiteQueenAllowed;
4650
this.enPassantSquare = enPassantSquare;
51+
this.halfMoveClock = halfMoveClock;
52+
this.fullMoveClock = fullMoveClock;
53+
4754
this.whitePositions = whitePositions;
4855
this.blackPositions = blackPositions;
4956

@@ -79,6 +86,7 @@ void copyFrom(MinChessWorkspace other) {
7986
}
8087

8188
void doMoveImp(long from, long to) {
89+
halfMoveClock++;
8290
// Capture, clean to square
8391
if ((to & whitePositions) != 0 || (to & blackPositions) != 0) {
8492
if ((to & queenPositions) != 0) {
@@ -110,6 +118,7 @@ void doMoveImp(long from, long to) {
110118
} else {
111119
whitePositions &= ~to;
112120
}
121+
halfMoveClock = 0;
113122
}
114123

115124
if ((from & kingPositions) != 0) {
@@ -151,6 +160,7 @@ void doMoveImp(long from, long to) {
151160
if ((from & pawnPositions) != 0) {
152161
pawnPositions &= ~from;
153162
pawnPositions |= to;
163+
halfMoveClock = 0;
154164
}
155165

156166
enPassantSquare = 0;
@@ -161,7 +171,9 @@ void doMoveImp(long from, long to) {
161171
} else {
162172
blackPositions &= ~from;
163173
blackPositions |= to;
174+
fullMoveClock++;
164175
}
176+
165177
whiteTurn = !whiteTurn;
166178
}
167179

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package net.chesstango.gardel.minchess;
2+
3+
import net.chesstango.gardel.fen.FEN;
4+
import org.junit.jupiter.api.Test;
5+
6+
import static net.chesstango.gardel.minchess.MinChessConstants.encodeMove;
7+
import static org.junit.jupiter.api.Assertions.*;
8+
9+
/**
10+
* @author Mauricio Coria
11+
*/
12+
public class ClockTest {
13+
14+
@Test
15+
public void test_START_POSITION() {
16+
MinChess game = MinChess.from(FEN.START_POSITION);
17+
18+
assertTrue(game.getTurn());
19+
assertEquals(0, game.getHalfMoveClock());
20+
assertEquals(1, game.getFullMoveClock());
21+
22+
assertEquals(FEN.START_POSITION, game.toFEN());
23+
}
24+
25+
@Test
26+
public void test_START_POSITION_a2a4() {
27+
MinChess game = MinChess.from(FEN.START_POSITION);
28+
29+
game.doMove(encodeMove(Square.e2.bitPosition(), Square.e4.bitPosition()));
30+
31+
assertFalse(game.getTurn());
32+
assertEquals(0, game.getHalfMoveClock());
33+
assertEquals(1, game.getFullMoveClock());
34+
35+
assertEquals(FEN.from("rnbqkbnr/pppppppp/8/8/4P3/8/PPPP1PPP/RNBQKBNR b KQkq e3 0 1"), game.toFEN());
36+
}
37+
}

src/test/java/net/chesstango/gardel/pgn/PGNTest.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
public class PGNTest {
1717

1818
@Test
19-
public void testToEpd() throws IOException {
19+
public void testToEpd(){
2020
PGN pgn = new PGN();
2121

2222
pgn.setMoveList(List.of("a4"));
@@ -30,10 +30,12 @@ public void testToEpd() throws IOException {
3030
EPD epdFirst = epdList.getFirst();
3131

3232
assertEquals("a4", epdFirst.getSuppliedMoveStr());
33+
assertEquals("0", epdFirst.getHalfMoveClock());
34+
assertEquals("1", epdFirst.getFullMoveClock());
3335
}
3436

3537
@Test
36-
public void test_fromFEN() throws IOException {
38+
public void test_fromFEN() {
3739
PGN pgn = PGN.from(FEN.START_POSITION);
3840

3941
assertNull(pgn.getFen());

0 commit comments

Comments
 (0)