Working parser, file chooser and sample files

This commit is contained in:
Imbus 2023-12-10 15:54:20 +01:00
parent 175545d3d5
commit 521b3fb05b
6 changed files with 163 additions and 4 deletions

View file

@ -54,7 +54,23 @@ public class SudokuController {
}
});
view.addCellClickListener(new CellActionListener());
view.addFileButtonListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// Open a file, view handles the parsing internally via SudokuParser
int[][] newBoard = view.openFile();
// If the file was parsed successfully
if (newBoard != null) {
// Set the model
model.setBoard(newBoard);
// Update the view
view.updateView(model.getBoard());
}
}
});
view.addCellActionListener(new CellActionListener());
}
/** Start the GUI */

View file

@ -4,6 +4,8 @@ import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import sudoku.SudokuParser;
/**
* SolverView is a GUI for the SudokuSolver interface
*/
@ -17,6 +19,8 @@ public class SudokuView extends JFrame {
private JButton resetButton;
/** Button for random */
private JButton randomButton;
/** Button for picking a Sudoku-file */
private JButton fileButton;
/** Constructor */
public SudokuView() {
@ -52,11 +56,13 @@ public class SudokuView extends JFrame {
solveButton = new JButton("Solve");
resetButton = new JButton("Reset");
randomButton = new JButton("Randomize");
fileButton = new JButton("Open file");
JPanel buttonPanel = new JPanel();
buttonPanel.add(solveButton);
buttonPanel.add(resetButton);
buttonPanel.add(randomButton);
buttonPanel.add(fileButton);
add(buttonPanel, BorderLayout.SOUTH);
}
@ -105,6 +111,15 @@ public class SudokuView extends JFrame {
randomButton.addActionListener(listener);
}
/**
* Method to add ActionListener to file button
*
* @param listener the ActionListener to add
*/
public void addFileButtonListener(ActionListener listener) {
fileButton.addActionListener(listener);
}
/**
* Method to add ActionListener to individual cells in the grid
* <p>
@ -114,7 +129,7 @@ public class SudokuView extends JFrame {
*
* @param listener the ActionListener to add
*/
public void addCellClickListener(ActionListener listener) {
public void addCellActionListener(ActionListener listener) {
for (int row = 0; row < 9; row++) {
for (int col = 0; col < 9; col++) {
grid[row][col].addActionListener(listener);
@ -183,4 +198,35 @@ public class SudokuView extends JFrame {
public void showErrorMessage(String message) {
JOptionPane.showMessageDialog(this, message);
}
/**
* Method to open a file picker dialog
*
* @return 2D array of integers representing the Sudoku board
*/
public int[][] openFile() {
// Create a file chooser and set all related options
JFileChooser fileChooser = new JFileChooser();
fileChooser.setCurrentDirectory(new java.io.File("."));
fileChooser.setDialogTitle("Select a Sudoku file");
fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
fileChooser.setAcceptAllFileFilterUsed(false);
// Show the file chooser and return if the user cancels
int returnValue = fileChooser.showOpenDialog(this);
if (returnValue != JFileChooser.APPROVE_OPTION) {
return null;
}
// Get the path
String filepath = fileChooser.getSelectedFile().getAbsolutePath();
// Try to parse it
try {
return SudokuParser.parseSudoku(filepath);
} catch (Exception e) {
showErrorMessage(e.getMessage());
return null;
}
}
}