Skip to content

Commit

Permalink
Merge pull request #40 from ctabin/piece-capture-support
Browse files Browse the repository at this point in the history
Adds captured pieces support
  • Loading branch information
ctabin authored Jul 23, 2023
2 parents 13a3225 + e765bf0 commit b671b0a
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 0 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,18 @@ The status can simply be retrieved after any move with the code below:
Status status = game.getStatus();
```

### Captured pieces

The easy way to get the captured pieces of a game is simple to use the `JChessGame.getCaptured` method:

```java
JChessGame game = JChessGame.newGame();
game.play("e4", "d5", "exd5");

game.getCaptured(Color.WHITE); //empty
game.getCaptured(Color.BLACK); //contains 1 Pawn entity
```

### Import games from PGN files

It is possible to import basic [PGN](https://en.wikipedia.org/wiki/Portable_Game_Notation) files
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/ch/astorm/jchess/JChessGame.java
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,18 @@ public List<Move> back(int nbMoves) {
return dropped;
}

/**
* Returns the captured pieces of the specified {@code Color} based on the current
* position move histoy.
*/
public List<Moveable> getCaptured(Color color) {
return position.getMoveHistory().stream().
map(m -> m.getCapturedEntity()).
filter(m -> m!=null).
filter(m -> m.getColor()==color).
collect(Collectors.toList());
}

/**
* Returns the {@link Color} that has the move.
*/
Expand Down
16 changes: 16 additions & 0 deletions src/test/java/ch/astorm/jchess/JChessGameTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import ch.astorm.jchess.core.entities.Bishop;
import ch.astorm.jchess.core.entities.King;
import ch.astorm.jchess.core.entities.Knight;
import ch.astorm.jchess.core.entities.Pawn;
import ch.astorm.jchess.core.entities.Queen;
import ch.astorm.jchess.core.entities.Rook;
import ch.astorm.jchess.core.rules.RuleManager;
Expand Down Expand Up @@ -37,6 +38,8 @@ public void testInitialPosition() {
JChessGame game = JChessGame.newGame();
assertNull(game.get("e5"));
assertNotNull(game.get("a1"));
assertTrue(game.getCaptured(Color.WHITE).isEmpty());
assertTrue(game.getCaptured(Color.BLACK).isEmpty());

assertEquals(20, game.getAvailableMoves().size());

Expand Down Expand Up @@ -204,4 +207,17 @@ public void testDrawByStalemate() {
assertEquals(Status.DRAW_STALEMATE, game.play("Qg6"));
assertThrows(IllegalStateException.class, () -> game.play("Qg5"));
}

@Test
public void testCapturedPieces() {
JChessGame game = JChessGame.newGame();
game.play("e4", "d5", "exd5");

assertTrue(game.getCaptured(Color.WHITE).isEmpty());

List<Moveable> capturedBlack = game.getCaptured(Color.BLACK);
assertEquals(1, capturedBlack.size());
assertEquals(Color.BLACK, capturedBlack.get(0).getColor());
assertTrue(capturedBlack.get(0) instanceof Pawn);
}
}

0 comments on commit b671b0a

Please sign in to comment.