126 lines
3.8 KiB
Java
126 lines
3.8 KiB
Java
package gui;
|
|
|
|
import sudoku.SudokuSolver;
|
|
import java.awt.event.*;
|
|
|
|
/**
|
|
* SolverController is a controller for the SudokuSolver interface
|
|
*/
|
|
public class SudokuController {
|
|
SudokuSolver model;
|
|
SudokuView view;
|
|
|
|
/**
|
|
* Constructor
|
|
*
|
|
* @param model SudokuSolver model
|
|
* @param view SudokuView view
|
|
*/
|
|
public SudokuController(SudokuSolver model, SudokuView view) {
|
|
this.model = model;
|
|
this.view = view;
|
|
|
|
// Add action listeners to the buttons
|
|
view.addSolveButtonListener(new ActionListener() {
|
|
@Override
|
|
public void actionPerformed(ActionEvent e) {
|
|
// Solve the board
|
|
boolean solved = model.solve();
|
|
if (!solved) {
|
|
view.showErrorMessage("Could not solve the board.");
|
|
System.out.println("Could not solve the board.");
|
|
System.out.println(model.toString());
|
|
} else {
|
|
// Update the view
|
|
view.updateView(model.getBoard());
|
|
}
|
|
|
|
}
|
|
});
|
|
|
|
view.addResetButtonListener(new ActionListener() {
|
|
@Override
|
|
public void actionPerformed(ActionEvent e) {
|
|
// Clear the board
|
|
model.clear();
|
|
|
|
// Update the view
|
|
view.updateView(model.getBoard());
|
|
}
|
|
});
|
|
|
|
view.addRandomButtonListener(new ActionListener() {
|
|
@Override
|
|
public void actionPerformed(ActionEvent e) {
|
|
// Randomize the board
|
|
model.randomizeBoard();
|
|
|
|
// Update the view
|
|
view.updateView(model.getBoard());
|
|
}
|
|
});
|
|
|
|
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 */
|
|
public void start() {
|
|
view.setVisible(true);
|
|
}
|
|
|
|
/**
|
|
* CellActionListener is an ActionListener for the Sudoku grid cells
|
|
*/
|
|
private class CellActionListener implements ActionListener {
|
|
@Override
|
|
public void actionPerformed(ActionEvent e) {
|
|
// Get the row and column from the clicked cell
|
|
int row = view.getSelectedRow();
|
|
int col = view.getSelectedColumn();
|
|
|
|
// The value to be inserted into the cell
|
|
// Zero inicates an empty cell
|
|
// Negative values are invalid
|
|
int value = 0;
|
|
|
|
String cellValue = view.getCellValue(row, col);
|
|
|
|
// We need to check for null and empty string
|
|
if (cellValue == null || cellValue.equals("")) {
|
|
value = 0;
|
|
} else {
|
|
try {
|
|
value = Integer.parseInt(cellValue);
|
|
} catch (NumberFormatException ex) {
|
|
value = -1;
|
|
}
|
|
}
|
|
|
|
// If the input is invalid, value < 0 indicates parse error
|
|
if (!model.isLegal(row, col, value) || value < 0) {
|
|
value = 0;
|
|
view.showErrorMessage("Invalid input. Try again.");
|
|
}
|
|
|
|
// Update the model and view
|
|
model.set(row, col, value);
|
|
view.updateView(model.getBoard());
|
|
}
|
|
}
|
|
}
|