Hi all, I've got a code here, as the title says it is a sudoku puzzle. It can create only one puzzle which is there hard coded. Now, the problem is how to add another puzzle? Or maybe I can turn this into a random generated puzzle which is hard I presume. What should be added or deleted in the code. I'll post it below. Please do consider that I am just learning Java. Thanks
Code1 filename Sudoku
package sudoku3;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class Sudoku extends JFrame {
private static final String INITIAL_BOARD =
"8156....4/" +
"6...75.8./" +
"....9..../" +
"9...417../" +
".4.....2./" +
"..623...8/" +
"....5..../" +
".5.91...6/" +
"1....7895";
private SudokuModel _sudokuLogic = new SudokuModel(INITIAL_BOARD);
private SudokuBoardDisplay _sudokuBoard = new SudokuBoardDisplay(_sudokuLogic);
private JTextField _rowTF = new JTextField(2);
private JTextField _colTF = new JTextField(2);
private JTextField _valTF = new JTextField(2);
public Sudoku() {
JButton moveBtn = new JButton("Move");
JPanel controlPanel = new JPanel();
controlPanel.setLayout(new FlowLayout());
controlPanel.add(new JLabel("Row (1-9):"));
controlPanel.add(_rowTF);
controlPanel.add(new JLabel("Col (1-9):"));
controlPanel.add(_colTF);
controlPanel.add(new JLabel("Val:"));
controlPanel.add(_valTF);
controlPanel.add(moveBtn);
moveBtn.addActionListener(new MoveListener());
JPanel content = new JPanel();
content.setLayout(new BorderLayout());
content.add(controlPanel, BorderLayout.NORTH);
content.add(_sudokuBoard, BorderLayout.CENTER);
setContentPane(content);
setTitle("Sudoku 3");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
pack();
setLocationRelativeTo(null);
}
class MoveListener implements ActionListener {
public void actionPerformed(ActionEvent ae) {
try {
int row = Integer.parseInt(_rowTF.getText().trim()) - 1;
int col = Integer.parseInt(_colTF.getText().trim()) - 1;
int val = Integer.parseInt(_valTF.getText().trim());
if (_sudokuLogic.isLegalMove(row, col, val)) {
_sudokuLogic.setVal(row, col, val);
_sudokuBoard.repaint();
} else {
JOptionPane.showMessageDialog(null, "Illegal row, col, or value.");
}
} catch (NumberFormatException nfe) {
JOptionPane.showMessageDialog(null, "Please enter numeric values.");
}
}
}
public static void main(String[] args) {
new Sudoku().setVisible(true);
}
}
Code2 filename SudokuBoardDisplay
package sudoku3;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import javax.swing.JComponent;
public class SudokuBoardDisplay extends JComponent {
private static final int CELL_PIXELS = 50; // Size of each cell.
private static final int PUZZLE_SIZE = 9; // Number of rows/cols
private static final int SUBSQUARE = 3; // Size of subsquare.
private static final int BOARD_PIXELS = CELL_PIXELS * PUZZLE_SIZE;
private static final int TEXT_OFFSET = 15; // Fine tuning placement of text.
private static final Font TEXT_FONT = new Font("Sansserif", Font.BOLD, 24);
private SudokuModel _model; // Set in constructor.
public SudokuBoardDisplay(SudokuModel model) {
setPreferredSize(new Dimension(BOARD_PIXELS + 2, BOARD_PIXELS + 2));
setBackground(Color.WHITE);
_model = model;
}
@Override public void paintComponent(Graphics g) {
g.setColor(getBackground());
g.fillRect(0, 0, getWidth(), getHeight());
g.setColor(Color.BLACK);
drawGridLines(g);
drawCellValues(g);
}
private void drawGridLines(Graphics g) {
for (int i = 0; i <= PUZZLE_SIZE; i++) {
int acrossOrDown = i * CELL_PIXELS;
g.drawLine(acrossOrDown, 0, acrossOrDown, BOARD_PIXELS);
g.drawLine(0, acrossOrDown, BOARD_PIXELS, acrossOrDown);
if (i % SUBSQUARE == 0) {
acrossOrDown++; // Move one pixel and redraw as above
g.drawLine(acrossOrDown, 0, acrossOrDown, BOARD_PIXELS);
g.drawLine(0, acrossOrDown, BOARD_PIXELS, acrossOrDown);
}
}
}
private void drawCellValues(Graphics g) {
g.setFont(TEXT_FONT);
for (int i = 0; i < PUZZLE_SIZE; i++) {
int yDisplacement = (i+1) * CELL_PIXELS - TEXT_OFFSET;
for (int j = 0; j < PUZZLE_SIZE; j++) {
if (_model.getVal(i, j) != 0) {
int xDisplacement = j * CELL_PIXELS + TEXT_OFFSET;
g.drawString("" + _model.getVal(i, j), xDisplacement, yDisplacement);
}
}
}
}
}
Code3 filename SudokuModel
package sudoku3;
public class SudokuModel {
private static final int BOARD_SIZE = 9;
private int[][] _board;
public SudokuModel() {
_board = new int[BOARD_SIZE][BOARD_SIZE];
}
public SudokuModel(String initialBoard) {
this(); // Call no parameter constructor first.
initializeFromString(initialBoard);
}
public void initializeFromString(final String boardStr) {
clear(); // Clear all values from the board.
int row = 0;
int col = 0;
for (int i = 0; i < boardStr.length(); i++) {
char c = boardStr.charAt(i);
if (c >= '1' && c <='9') {
if (row > BOARD_SIZE || col > BOARD_SIZE) {
throw new IllegalArgumentException("SudokuModel: "
+ " Attempt to initialize outside 1-9 "
+ " at row " + (row+1) + " and col " + (col+1));
}
_board[row][col] = c - '0';
col++;
} else if (c == '.') {
col++;
} else if (c == '/') {
row++;
col = 0;
} else {
throw new IllegalArgumentException("SudokuModel: Character '" + c
+ "' not allowed in board specification");
}
}
}
public boolean isLegalMove(int row, int col, int val) {
return row>=0 && row<BOARD_SIZE && col>=0 && col<BOARD_SIZE
&& val>0 && val<=9 && _board[row][col]==0;
}
public void setVal(int r, int c, int v) {
_board[r][c] = v;
}
public int getVal(int row, int col) {
return _board[row][col];
}
public void clear() {
for (int row = 0; row < BOARD_SIZE; row++) {
for (int col = 0; col < BOARD_SIZE; col++) {
setVal(row, col, 0);
}
}
}
}