Compare commits
8 commits
ffd1d4bd51
...
072da88135
Author | SHA1 | Date | |
---|---|---|---|
|
072da88135 | ||
|
b02e846ec6 | ||
|
970aabd532 | ||
|
6ae83248ef | ||
|
f8adb7402e | ||
|
536f4e1b7d | ||
|
7eab9da318 | ||
|
9f25270371 |
4 changed files with 48 additions and 11 deletions
|
@ -1,6 +1,8 @@
|
||||||
package gui;
|
package gui;
|
||||||
|
|
||||||
import javax.swing.*;
|
import javax.swing.*;
|
||||||
|
import javax.swing.border.Border;
|
||||||
|
|
||||||
import java.awt.*;
|
import java.awt.*;
|
||||||
import java.awt.event.*;
|
import java.awt.event.*;
|
||||||
|
|
||||||
|
@ -27,24 +29,42 @@ public class SudokuView extends JFrame {
|
||||||
setTitle("Sudoku Solver");
|
setTitle("Sudoku Solver");
|
||||||
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||||
setLayout(new BorderLayout());
|
setLayout(new BorderLayout());
|
||||||
|
setIconImage(new ImageIcon("src/main/resources/sudoku.png").getImage());
|
||||||
|
|
||||||
initializeGrid();
|
initializeGrid();
|
||||||
initializeButtons();
|
initializeButtons();
|
||||||
|
|
||||||
pack();
|
setMinimumSize(new Dimension(500, 500));
|
||||||
setLocationRelativeTo(null);
|
pack(); // Resize the window to fit the components, if necessary
|
||||||
|
setLocationRelativeTo(null); // Center the window
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Initialize the grid, called by the constructor */
|
/** Initialize the grid, called by the constructor */
|
||||||
private void initializeGrid() {
|
private void initializeGrid() {
|
||||||
grid = new JTextField[9][9];
|
grid = new JTextField[9][9];
|
||||||
JPanel gridPanel = new JPanel(new GridLayout(9, 9));
|
JPanel gridPanel = new JPanel(new GridLayout(9, 9));
|
||||||
|
gridPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
|
||||||
|
|
||||||
|
int fontSize = 24;
|
||||||
|
String fontName = gridPanel.getFont().getName();
|
||||||
|
Border border = BorderFactory.createLineBorder(Color.BLACK);
|
||||||
|
Font font = new Font(fontName, Font.BOLD, fontSize);
|
||||||
|
|
||||||
for (int row = 0; row < 9; row++) {
|
for (int row = 0; row < 9; row++) {
|
||||||
for (int col = 0; col < 9; col++) {
|
for (int col = 0; col < 9; col++) {
|
||||||
grid[row][col] = new JTextField(2);
|
JTextField cell = new JTextField(2);
|
||||||
grid[row][col].setHorizontalAlignment(JTextField.CENTER);
|
cell.setHorizontalAlignment(JTextField.CENTER);
|
||||||
gridPanel.add(grid[row][col]);
|
cell.setFont(font);
|
||||||
|
cell.setBorder(border);
|
||||||
|
cell.setForeground(Color.BLACK);
|
||||||
|
|
||||||
|
// Set background color to gray for every third JTextField
|
||||||
|
if ((row / 3 + col / 3) % 2 == 1) {
|
||||||
|
cell.setBackground(Color.LIGHT_GRAY);
|
||||||
|
}
|
||||||
|
|
||||||
|
grid[row][col] = cell;
|
||||||
|
gridPanel.add(cell);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -20,10 +20,18 @@ public class Solver implements SudokuSolver {
|
||||||
this.board = board;
|
this.board = board;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** {@inheritDoc} */
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
* <p>
|
||||||
|
* Note that this returns a copy of the board, not the actual board
|
||||||
|
*/
|
||||||
@Override
|
@Override
|
||||||
public int[][] getBoard() {
|
public int[][] getBoard() {
|
||||||
return board;
|
int[][] copy = new int[9][9];
|
||||||
|
for (int row = 0; row < 9; row++) {
|
||||||
|
System.arraycopy(board[row], 0, copy[row], 0, 9);
|
||||||
|
}
|
||||||
|
return copy;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Resets the board to all zeros */
|
/** Resets the board to all zeros */
|
||||||
|
@ -34,7 +42,6 @@ public class Solver implements SudokuSolver {
|
||||||
row[i] = 0;
|
row[i] = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// board = new int[9][9];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* {@inheritDoc} */
|
/* {@inheritDoc} */
|
||||||
|
|
BIN
app/src/main/resources/sudoku.png
Normal file
BIN
app/src/main/resources/sudoku.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 8.5 KiB |
|
@ -17,8 +17,12 @@ class SolverTest {
|
||||||
Solver solver = new Solver();
|
Solver solver = new Solver();
|
||||||
int[][] board = new int[9][9];
|
int[][] board = new int[9][9];
|
||||||
solver.setBoard(board);
|
solver.setBoard(board);
|
||||||
assertEquals(board, solver.getBoard());
|
|
||||||
|
for (int row = 0; row < 9; row++) {
|
||||||
|
assertArrayEquals(board[row], solver.getBoard()[row]);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void randomizeBoardTest() {
|
void randomizeBoardTest() {
|
||||||
|
@ -28,6 +32,12 @@ class SolverTest {
|
||||||
assertNotEquals(board, solver.getBoard());
|
assertNotEquals(board, solver.getBoard());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void emptyTest() {
|
||||||
|
Solver solver = new Solver();
|
||||||
|
assertTrue(solver.solve());
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void legalTest() {
|
void legalTest() {
|
||||||
Solver solver = new Solver();
|
Solver solver = new Solver();
|
||||||
|
|
Loading…
Reference in a new issue