Skip to content

Commit

Permalink
Implements a quick and dirty way to define start position by its fen
Browse files Browse the repository at this point in the history
  • Loading branch information
fathzer committed May 14, 2024
1 parent 75f5356 commit 0f5dbfc
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 14 deletions.
22 changes: 19 additions & 3 deletions src/main/java/com/fathzer/jchess/settings/Settings.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package com.fathzer.jchess.settings;

import java.util.Random;
import java.util.function.Function;
import java.util.function.Supplier;

import com.fathzer.games.Color;
import com.fathzer.jchess.Board;
import com.fathzer.jchess.Move;
import com.fathzer.jchess.fen.FENUtils;
import com.fathzer.jchess.GameBuilders;

import lombok.AllArgsConstructor;
Expand All @@ -20,6 +22,8 @@ public class Settings {
private static final Random RANDOM_GENERATOR = new Random();

private Variant variant = Variant.STANDARD;
//TODO fen should be obtained by Settings panel
private String fen = buildFEN();
private boolean tabletMode = true;
private boolean showPossibleMoves = true;
private boolean touchMove = false;
Expand All @@ -29,18 +33,30 @@ public class Settings {
private ColorSetting player1Color = ColorSetting.RANDOM;
private PlayerSettings player2 = new PlayerSettings();

private static final String buildFEN() {
var fen = System.getProperty("fen");
final String result = fen==null ? fen : fen.replace('_', ' ');
if (result!=null) {
System.out.println("Fen of start position was retrieved in 'fen' system property: "+result);
}
return result;
}

@AllArgsConstructor
public enum Variant {
STANDARD(GameBuilders.STANDARD), CHESS960(GameBuilders.CHESS960);
private final Supplier<Board<Move>> rules;
STANDARD(toFunction(GameBuilders.STANDARD)), CHESS960(toFunction(GameBuilders.CHESS960));
private final Function<String, Board<Move>> rules;

/** Gets a Supplier that can create a new game.
* <br>Typically, it is able to create the representation of a new game (for example, a chess board at the beginning of the game)
*/
public Supplier<Board<Move>> getRules() {
public Function<String, Board<Move>> getRules() {
return rules;
}

private static Function<String, Board<Move>> toFunction(Supplier<Board<Move>> s) {
return fen -> fen==null ? s.get() : FENUtils.from(fen);
}
}

public enum ColorSetting {
Expand Down
10 changes: 1 addition & 9 deletions src/main/java/com/fathzer/jchess/swing/ChessBoardPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import java.io.UncheckedIOException;
import java.util.Arrays;
import java.util.List;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import java.util.stream.Stream;

Expand Down Expand Up @@ -35,7 +34,6 @@ public class ChessBoardPanel extends JPanel implements MouseListener {

private final transient com.fathzer.jchess.Dimension dimension;
private final transient SpriteMover sprites;
private transient Supplier<Board<Move>> rules;
private int selected;
private boolean reverted;
private transient Board<Move> board;
Expand Down Expand Up @@ -89,12 +87,6 @@ public void setBoard(Board<Move> board) {
this.repaint();
}

public void setChessRules(Supplier<Board<Move>> rules) {
this.rules = rules;
this.updatePossibleMoves();
this.repaint();
}

public void setCellColors(Color whiteColor, Color blackColor) {
this.whiteColor = whiteColor;
this.blackColor = blackColor;
Expand Down Expand Up @@ -122,7 +114,7 @@ public void setManualMoveEnabled(boolean enabled) {

private void updatePossibleMoves() {
this.targets = new int[0];
if (board!=null && rules!=null) {
if (board!=null) {
this.moveList = board.getLegalMoves();
}
}
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/com/fathzer/jchess/swing/GameSession.java
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ private void doRevenge() {
}

private void initGame() {
this.game = new Game(settings.getVariant().getRules().get(), buildClock());
this.game = new Game(settings.getVariant().getRules().apply(settings.getFen()), buildClock());
this.game.setStartClockAfterFirstMove(settings.isStartClockAfterFirstMove());
panel.setPlayer1Color(player1Color);
panel.setClock(game.getClock());
Expand Down Expand Up @@ -348,7 +348,6 @@ public void setSettings(Settings settings) {
throw new IllegalStateException("Can't change the game settings during the game");
}
this.settings = settings;
panel.getBoard().setChessRules(settings.getVariant().getRules());
panel.setPlayer1Human(settings.getPlayer1().getEngine()==null);
panel.setPlayer2Human(settings.getPlayer2().getEngine()==null);
panel.getBoard().setShowPossibleMoves(settings.isShowPossibleMoves());
Expand Down

0 comments on commit 0f5dbfc

Please sign in to comment.