Skip to content

Commit

Permalink
Fixes start position changes on and pieces not reverted before start
Browse files Browse the repository at this point in the history
  • Loading branch information
fathzer committed May 22, 2023
1 parent 397b29d commit 56dc243
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 24 deletions.
38 changes: 18 additions & 20 deletions src/main/java/com/fathzer/jchess/swing/GameSession.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
@Slf4j
public class GameSession {
public enum State {
PAUSED, RUNNING, ENDED
CREATED, PAUSED, RUNNING, ENDED
}

private final GamePanel panel;
Expand All @@ -44,27 +44,14 @@ public enum State {

public GameSession(GamePanel panel, GameSettings settings) {
this.panel = panel;
this.state = new Observable<>(State.ENDED);
this.state = new Observable<>(State.CREATED);
state.addListener(this::onStateChanged);
panel.getBoard().addPropertyChangeListener(ChessBoardPanel.TARGET, evt -> onMove((Move) evt.getNewValue()));
panel.setResignationHandler(this::resign);
setSettings(settings);
gameCount = 0;
}

private void firstGame() {
this.gameCount = 0;
initGame();
panel.getBoard().setReverted(Color.BLACK.equals(player1Color));
setEngine(player1Color, getEngine(settings.getVariant(), settings.getPlayer1().getEngine()));
setEngine(player1Color.opposite(), getEngine(settings.getVariant(), settings.getPlayer2().getEngine()));
if (onlyHumans()) {
panel.getBoard().setUpsideDownColor(player1Color.opposite());
}
panel.getBoard().setShowPossibleMoves(settings.isShowPossibleMoves());
panel.getBoard().setTouchMove(settings.isTouchMove());
}

private void onStateChanged(State old, State current) {
log.debug("State changed from {} to {}", old,current);
if (State.RUNNING.equals(current)) {
Expand Down Expand Up @@ -186,7 +173,14 @@ private Function<Board<Move>, Move> getEngine(Color color) {

public void start() {
if (State.ENDED.equals(getState())) {
firstGame();
this.gameCount = 0;
initGame();
}
setEngine(player1Color, getEngine(settings.getVariant(), settings.getPlayer1().getEngine()));
setEngine(player1Color.opposite(), getEngine(settings.getVariant(), settings.getPlayer2().getEngine()));
panel.getBoard().setReverted(Color.BLACK.equals(player1Color));
if (onlyHumans()) {
panel.getBoard().setUpsideDownColor(player1Color.opposite());
}
setState(State.RUNNING);
}
Expand Down Expand Up @@ -216,12 +210,11 @@ private void play(Game game, Move move) {
});
}


public State getState() {
return this.state.getValue();
}

public void setState(State state) {
private void setState(State state) {
this.state.setValue(state);
}

Expand Down Expand Up @@ -301,15 +294,20 @@ public void stop() {
}

public void setSettings(GameSettings settings) {
if (!State.ENDED.equals(getState())) {
throw new IllegalStateException("Can't change the game settings dutring the game");
if (State.RUNNING.equals(getState()) || State.PAUSED.equals(getState())) {
throw new IllegalStateException("Can't change the game settings during the game");
}
this.settings = settings;
this.rules = settings.getVariant().getRules();
panel.getBoard().setChessRules(rules);
panel.setPlayer1Human(settings.getPlayer1().getEngine()==null);
panel.setPlayer2Human(settings.getPlayer2().getEngine()==null);
panel.getBoard().setShowPossibleMoves(settings.isShowPossibleMoves());
panel.getBoard().setTouchMove(settings.isTouchMove());
player1Color = settings.getPlayer1Color().getColor();
if (onlyHumans()) {
panel.getBoard().setUpsideDownColor(player1Color.opposite());
}
initGame();
}
}
4 changes: 2 additions & 2 deletions src/main/java/com/fathzer/jchess/swing/JChess.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public void actionPerformed(ActionEvent e) {

@Override
public void actionPerformed(ActionEvent e) {
newGame();
startGame();
}
};
panel.setPlayAction(() -> this.startAction.actionPerformed(null));
Expand Down Expand Up @@ -118,7 +118,7 @@ protected JMenuBar buildMenuBar() {
return bar;
}

private void newGame() {
private void startGame() {
this.startAction.setEnabled(false);
this.panel.setMenuVisible(false);
this.game.start();
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/fathzer/jchess/swing/MenuPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ public class MenuPanel extends JPanel {

private final JButton settings;
@Setter
private Runnable settingsAction;
private transient Runnable settingsAction;
private final JButton play;
@Setter
private Runnable playAction;
private transient Runnable playAction;

/**
* Create the panel.
Expand Down

0 comments on commit 56dc243

Please sign in to comment.