restart function enabled

This commit is contained in:
cleme 2025-04-22 16:08:51 +02:00
parent 11a8971e18
commit 29dacdaf3c
1 changed files with 252 additions and 224 deletions

View File

@ -1,268 +1,296 @@
package windowInterface; package windowInterface;
import java.awt.BorderLayout; import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout; import java.awt.GridLayout;
import javax.swing.JFrame; import javax.swing.JFrame;
import javax.swing.JPanel; import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.border.EmptyBorder; import javax.swing.border.EmptyBorder;
import javax.swing.event.ChangeEvent; import javax.swing.SwingUtilities;
import javax.swing.event.ChangeListener;
import backend.Game; import backend.Game;
import javax.swing.JButton; import javax.swing.JButton;
import javax.swing.JFileChooser; import javax.swing.JFileChooser;
import javax.swing.JLabel; import javax.swing.JLabel;
import javax.swing.JCheckBox;
import java.awt.event.ActionListener; import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.FileReader; import java.io.FileReader;
import java.io.FileWriter; import java.io.FileWriter;
import java.io.IOException; import java.io.IOException;
import java.util.Arrays; import java.util.Arrays;
import java.util.LinkedList; import java.util.LinkedList;
import java.awt.event.ActionEvent;
import javax.swing.JList;
import javax.swing.AbstractListModel;
import javax.swing.JToggleButton;
import javax.swing.JRadioButton;
import javax.swing.JCheckBox;
public class MyInterface extends JFrame { public class MyInterface extends JFrame {
private static final long serialVersionUID = -6840815447618468846L; private static final long serialVersionUID = -6840815447618468846L;
private JPanel contentPane; private JPanel contentPane;
private JLabel turnLabel; private JLabel turnLabel;
private JLabel borderLabel; private JLabel borderLabel;
private JLabel speedLabel; private JLabel speedLabel;
private JPanelChessBoard panelDraw; private JPanelChessBoard panelDraw;
private Game game; private Game game;
private JLabel actionLabel; private JLabel actionLabel;
private JCheckBox chckbxBlackAI; private JCheckBox chckbxBlackAI;
private JCheckBox chckbxWhiteAI; private JCheckBox chckbxWhiteAI;
/** /**
* Create the frame. * Create the frame.
*/ */
public MyInterface() { public MyInterface() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(10, 10, 650, 650); setBounds(10, 10, 650, 650);
contentPane = new JPanel(); contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0)); contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane); setContentPane(contentPane);
JPanel panelTop = new JPanel(); JPanel panelTop = new JPanel();
contentPane.add(panelTop, BorderLayout.NORTH); contentPane.add(panelTop, BorderLayout.NORTH);
JPanel panelRight = new JPanel(); JPanel panelRight = new JPanel();
contentPane.add(panelRight, BorderLayout.EAST); contentPane.add(panelRight, BorderLayout.EAST);
panelRight.setLayout(new GridLayout(4,1)); panelRight.setLayout(new GridLayout(4, 1));
actionLabel = new JLabel("Waiting For Start"); actionLabel = new JLabel("Waiting For Start");
panelTop.add(actionLabel); panelTop.add(actionLabel);
JButton btnGo = new JButton("Start/Restart"); JButton btnGo = new JButton("Start/Restart");
btnGo.addActionListener(new ActionListener() { btnGo.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) { public void actionPerformed(ActionEvent arg0) {
clicButtonStart(); clicButtonStart();
} }
}); });
panelTop.add(btnGo); panelTop.add(btnGo);
turnLabel = new JLabel("Turn : X"); turnLabel = new JLabel("Turn : X");
panelTop.add(turnLabel); panelTop.add(turnLabel);
JButton btnLoad = new JButton("Load File");
btnLoad.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
clicLoadFileButton();
}
});
panelRight.add(btnLoad);
JButton btnSave = new JButton("Save To File"); JButton btnLoad = new JButton("Load File");
btnSave.addActionListener(new ActionListener() { btnLoad.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) { public void actionPerformed(ActionEvent arg0) {
clicSaveToFileButton(); clicLoadFileButton();
} }
}); });
panelRight.add(btnSave); panelRight.add(btnLoad);
JButton btnAdder = new JButton("Add Piece"); JButton btnSave = new JButton("Save To File");
btnAdder.addActionListener(new ActionListener() { btnSave.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) { public void actionPerformed(ActionEvent arg0) {
clickButtonAdder(); clicSaveToFileButton();
} }
}); });
panelRight.add(btnAdder); panelRight.add(btnSave);
JButton btnPieceSelector = new JButton("Piece Select");
btnPieceSelector.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
clickButtonSelector();
}
});
panelRight.add(btnPieceSelector);
JButton btnUndo = new JButton("Undo"); JButton btnAdder = new JButton("Add Piece");
btnUndo.addActionListener(new ActionListener() { btnAdder.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) { public void actionPerformed(ActionEvent arg0) {
clicUndoButton(); clickButtonAdder();
} }
});
panelRight.add(btnAdder);
}); JButton btnPieceSelector = new JButton("Piece Select");
panelTop.add(btnUndo); btnPieceSelector.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
chckbxWhiteAI = new JCheckBox("WhiteAI"); clickButtonSelector();
chckbxWhiteAI.addActionListener(new ActionListener() { }
public void actionPerformed(ActionEvent arg0) { });
clicAIToggle(true); panelRight.add(btnPieceSelector);
}
}); JButton btnUndo = new JButton("Undo");
panelTop.add(chckbxWhiteAI); btnUndo.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
chckbxBlackAI = new JCheckBox("BlackAI"); clicUndoButton();
chckbxBlackAI.addActionListener(new ActionListener() { }
public void actionPerformed(ActionEvent arg0) { });
clicAIToggle(false); panelTop.add(btnUndo);
}
}); chckbxWhiteAI = new JCheckBox("WhiteAI");
panelTop.add(chckbxBlackAI); chckbxWhiteAI.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
clicAIToggle(true);
}
});
panelTop.add(chckbxWhiteAI);
panelDraw = new JPanelChessBoard(this); chckbxBlackAI = new JCheckBox("BlackAI");
contentPane.add(panelDraw, BorderLayout.CENTER); chckbxBlackAI.addActionListener(new ActionListener() {
} public void actionPerformed(ActionEvent arg0) {
clicAIToggle(false);
}
});
panelTop.add(chckbxBlackAI);
public void setStepBanner(String s) { panelDraw = new JPanelChessBoard(this);
turnLabel.setText(s); contentPane.add(panelDraw, BorderLayout.CENTER);
} }
public void setBorderBanner(String s) { public void setStepBanner(String s) {
borderLabel.setText(s); turnLabel.setText(s);
} }
public JPanelChessBoard getPanelDessin() { public void setBorderBanner(String s) {
return panelDraw; borderLabel.setText(s);
} }
public void instantiateSimu() {
if(game==null) {
game = new Game(this);
panelDraw.setGame(game);
game.start();
}
}
public void clicButtonStart() { public JPanelChessBoard getPanelDessin() {
this.instantiateSimu(); return panelDraw;
} }
public void clickButtonAdder() {
panelDraw.toggleAdderMode();
}
public void clickButtonSelector() {
panelDraw.togglePieceSelector();
}
private void clicUndoButton() { public void instantiateSimu() {
if(game == null) { // If a game already exists, stop it cleanly
System.out.println("error : can't undo while no game present"); if (game != null) {
} else { // Disable AI to prevent interference
game.undoLastMove(); if (chckbxWhiteAI.isSelected()) {
} game.toggleAI(true);
chckbxWhiteAI.setSelected(false);
}
if (chckbxBlackAI.isSelected()) {
game.toggleAI(false);
chckbxBlackAI.setSelected(false);
}
} // Interrupt the game thread
public void clicAIToggle(boolean isWhite) { game.interrupt();
if(game == null) { try {
System.out.println("error : can't activate AI while no game present"); game.join(); // Wait for the thread to fully stop
if(isWhite) { } catch (InterruptedException e) {
chckbxWhiteAI.setSelected(false); System.err.println("Interrupted while waiting for game thread to stop: " + e.getMessage());
}else { Thread.currentThread().interrupt(); // Restore interrupted status
chckbxBlackAI.setSelected(false); }
} }
} else {
game.toggleAI(isWhite);
}
}
public void clicLoadFileButton() {
Game loadedSim = new Game(this);
String fileName=SelectFile();
LinkedList<String> lines = new LinkedList<String>();
if (fileName.length()>0) {
try {
BufferedReader fileContent = new BufferedReader(new FileReader(fileName));
String line = fileContent.readLine();
int colorID = 0;
while (line != null) {
lines.add(line);
line = fileContent.readLine();
}
loadedSim.setBoard(Arrays.stream(lines.toArray()).map(Object::toString).toArray(String[]::new));
fileContent.close();
} catch (Exception e) {
e.printStackTrace();
}
game = loadedSim;
panelDraw.setGame(game);
this.repaint();
}
}
public void clicSaveToFileButton() { // Reset UI state
String fileName=SelectFile(); if (panelDraw.isPieceAdderMode()) {
if (fileName.length()>0) { panelDraw.toggleAdderMode();
String[] content = game.getFileRepresentation(); }
writeFile(fileName, content); if (panelDraw.isPieceSelectorMode()) {
} panelDraw.togglePieceSelector();
} }
public String SelectFile() {
String s;
JFileChooser chooser = new JFileChooser();
chooser.setCurrentDirectory(new java.io.File("."));
chooser.setDialogTitle("Choose a file");
chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
chooser.setAcceptAllFileFilterUsed(true);
if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
s=chooser.getSelectedFile().toString();
} else {
System.out.println("No Selection ");
s="";
}
return s;
}
public void writeFile(String fileName, String[] content) {
FileWriter csvWriter;
try {
csvWriter = new FileWriter(fileName);
for (String row : content) {
csvWriter.append(row);
csvWriter.append("\n");
}
csvWriter.flush();
csvWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public void update(int turnCount, boolean turnIsWhite) {
turnLabel.setText("Turn : "+turnCount+", "+ (turnIsWhite?"White":"Black"));
actionLabel.setText(panelDraw.isPieceAdderMode()?"Adding Piece":
(panelDraw.isPieceSelectorMode()?"Selecting Piece to Add":
"Playing"));
this.repaint();
}
public void eraseLabels() {
this.setStepBanner("Turn : X");
}
} // Create a new game
game = new Game(this);
panelDraw.setGame(game);
// Reset the board to the default setup
game.setDefaultSetup();
// Start the new game thread on the EDT to ensure synchronization
SwingUtilities.invokeLater(() -> {
game.start();
// Force an immediate UI update
panelDraw.repaint();
update(game.getTurnNumber(), game.isWhiteTurn());
});
}
public void clicButtonStart() {
this.instantiateSimu();
}
public void clickButtonAdder() {
panelDraw.toggleAdderMode();
}
public void clickButtonSelector() {
panelDraw.togglePieceSelector();
}
private void clicUndoButton() {
if (game == null) {
System.out.println("error: can't undo while no game present");
} else {
game.undoLastMove();
}
}
public void clicAIToggle(boolean isWhite) {
if (game == null) {
System.out.println("error: can't activate AI while no game present");
if (isWhite) {
chckbxWhiteAI.setSelected(false);
} else {
chckbxBlackAI.setSelected(false);
}
} else {
game.toggleAI(isWhite);
}
}
public void clicLoadFileButton() {
Game loadedSim = new Game(this);
String fileName = SelectFile();
LinkedList<String> lines = new LinkedList<String>();
if (fileName.length() > 0) {
try {
BufferedReader fileContent = new BufferedReader(new FileReader(fileName));
String line = fileContent.readLine();
int colorID = 0;
while (line != null) {
lines.add(line);
line = fileContent.readLine();
}
loadedSim.setBoard(Arrays.stream(lines.toArray()).map(Object::toString).toArray(String[]::new));
fileContent.close();
} catch (Exception e) {
e.printStackTrace();
}
game = loadedSim;
panelDraw.setGame(game);
this.repaint();
}
}
public void clicSaveToFileButton() {
String fileName = SelectFile();
if (fileName.length() > 0) {
String[] content = game.getFileRepresentation();
writeFile(fileName, content);
}
}
public String SelectFile() {
String s;
JFileChooser chooser = new JFileChooser();
chooser.setCurrentDirectory(new java.io.File("."));
chooser.setDialogTitle("Choose a file");
chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
chooser.setAcceptAllFileFilterUsed(true);
if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
s = chooser.getSelectedFile().toString();
} else {
System.out.println("No Selection ");
s = "";
}
return s;
}
public void writeFile(String fileName, String[] content) {
FileWriter csvWriter;
try {
csvWriter = new FileWriter(fileName);
for (String row : content) {
csvWriter.append(row);
csvWriter.append("\n");
}
csvWriter.flush();
csvWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public void update(int turnCount, boolean turnIsWhite) {
turnLabel.setText("Turn: " + turnCount + ", " + (turnIsWhite ? "White" : "Black"));
actionLabel.setText(panelDraw.isPieceAdderMode() ? "Adding Piece" :
(panelDraw.isPieceSelectorMode() ? "Selecting Piece to Add" : "Playing"));
this.repaint();
}
public void eraseLabels() {
this.setStepBanner("Turn: X");
}
}