Fixed game state related bugs
This commit is contained in:
parent
ef8c43be1f
commit
a2d479bba0
@ -24,10 +24,10 @@ public class Game {
|
|||||||
private BoardComponent boardComponent;
|
private BoardComponent boardComponent;
|
||||||
|
|
||||||
public Game(Map<Color, Player> players, BoardPane boardPane) {
|
public Game(Map<Color, Player> players, BoardPane boardPane) {
|
||||||
this.players = players;
|
this.players = players;
|
||||||
this.overlayComponent = boardPane.getOverlayComponent();
|
this.overlayComponent = boardPane.getOverlayComponent();
|
||||||
this.boardComponent = boardPane.getBoardComponent();
|
this.boardComponent = boardPane.getBoardComponent();
|
||||||
this.board = boardComponent.getBoard();
|
this.board = boardComponent.getBoard();
|
||||||
|
|
||||||
// Initialize the game variable in each player
|
// Initialize the game variable in each player
|
||||||
players.values().forEach(player -> player.setGame(this));
|
players.values().forEach(player -> player.setGame(this));
|
||||||
@ -46,10 +46,9 @@ public class Game {
|
|||||||
System.out.printf("%s in check!%n", player.color.opposite());
|
System.out.printf("%s in check!%n", player.color.opposite());
|
||||||
default:
|
default:
|
||||||
boardComponent.repaint();
|
boardComponent.repaint();
|
||||||
overlayComponent.displayArrow(move);
|
|
||||||
players.get(player.color.opposite()).requestMove();
|
players.get(player.color.opposite()).requestMove();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
overlayComponent.displayArrow(move);
|
||||||
} else player.requestMove();
|
} else player.requestMove();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -57,12 +56,11 @@ public class Game {
|
|||||||
players.get(Color.WHITE).requestMove();
|
players.get(Color.WHITE).requestMove();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void restart() {
|
public void reset() {
|
||||||
players.forEach((k, v) -> v.cancelMove());
|
players.forEach((k, v) -> v.cancelMove());
|
||||||
board.initializeDefaultPositions();
|
board.initializeDefaultPositions();
|
||||||
boardComponent.repaint();
|
boardComponent.repaint();
|
||||||
overlayComponent.clearDots();
|
overlayComponent.clearDots();
|
||||||
overlayComponent.clearArrow();
|
overlayComponent.clearArrow();
|
||||||
start();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -65,7 +65,7 @@ public class MainWindow {
|
|||||||
mframe.getContentPane().add(toolPanel, BorderLayout.NORTH);
|
mframe.getContentPane().add(toolPanel, BorderLayout.NORTH);
|
||||||
|
|
||||||
JButton btnRestart = new JButton("Restart");
|
JButton btnRestart = new JButton("Restart");
|
||||||
btnRestart.addActionListener((evt) -> { if (game != null) game.restart(); });
|
btnRestart.addActionListener((evt) -> { if (game != null) game.reset(); game.start(); });
|
||||||
toolPanel.add(btnRestart);
|
toolPanel.add(btnRestart);
|
||||||
mframe.pack();
|
mframe.pack();
|
||||||
mframe.setLocationRelativeTo(null);
|
mframe.setLocationRelativeTo(null);
|
||||||
|
@ -80,9 +80,11 @@ public class MenuBar extends JMenuBar {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void startGame() {
|
private void startGame() {
|
||||||
// TODO: Re-init board and overlay component
|
|
||||||
Game game = new Game(players, boardPane);
|
Game game = new Game(players, boardPane);
|
||||||
mainWindow.setGame(game);
|
mainWindow.setGame(game);
|
||||||
|
|
||||||
|
// Update board and board component
|
||||||
|
game.reset();
|
||||||
game.start();
|
game.start();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user