The examples in this article share the specific code of the Java Sudoku game for your reference. The specific content is as follows
I plan to install the components required by javaFx and use javaFx directly, but it seems that the version of eclipse is wrong and cannot be used even after installation...
Sudoku code was written during the winter vacation. I learned Java for a month. It seems to be a bit insufficient now, but it is the first program after all, so I put it on it directly. The implementation of Sudoku final disk directly uses violence, and the time complexity is a bit high. I am too lazy to change it and directly put the code.
Final disk implementation:
import java.util.Random; public class SudokuPuzzleGenerator { private Random random = new Random(); private static final int MAX_CALL_RANDOM_ARRAY_TIMES = 220; private int currentTimes = 0; public int[][] generatePuzzleMatrix() { int[][] randomMatrix = new int[9][9]; for (int row = 0; row < 9; row++) { if (row == 0) { currentTimes = 0; randomMatrix[row] = buildRandomArray(); } else { int[] tempRandomArray = buildRandomArray(); for (int col = 0; col < 9; col++) { if (currentTimes < MAX_CALL_RANDOM_ARRAY_TIMES) { if (!isCandidateNmbFound(randomMatrix, tempRandomArray, row, col)) { resetValuesInRowToZero(randomMatrix, row); row -= 1; col = 8; tempRandomArray = buildRandomArray(); } } else { row = -1; col = 8; resetValuesToZeros(randomMatrix); currentTimes = 0; } } } return randomMatrix; } private void resetValuesInRowToZero(int[][] matrix, int row) { for (int j = 0; j < 9; j++) { matrix[row][j] = 0; } } private void resetValuesToZeros(int[][] matrix) { for (int row = 0; row < 9; row++) { for (int col = 0; col < 9; col++) { matrix[row][col] = 0; } } } private boolean isCandidateNmbFound(int[][] randomMatrix, int[] randomArray, int row, int col) { for (int i = 0; i < 9; i++) { randomMatrix[row][col] = randomArray[i]; if (noConflict(randomMatrix, row, col)) { return true; } } return false; } private boolean noConflict(int[][] candidateMatrix, int row, int col) { return noConflictInRow(candidateMatrix, row, col)&&noConflictInColumn(candidateMatrix, row, col) && noConflictInBlock(candidateMatrix, row, col); } private boolean noConflictInRow(int[][] candidateMatrix, int row, int col) { int currentValue = candidateMatrix[row][col]; for (int colNum = 0; colNum < col; colNum++) { if (currentValue == candidateMatrix[row][colNum]) { return false; } } return true; } private boolean noConflictInColumn(int[][] candidateMatrix, int row, int col) { int currentValue = candidateMatrix[row][col]; for (int rowNum = 0; rowNum < row; rowNum++) { if (currentValue == candidateMatrix[rowNum][col]) { return false; } } return true; } private boolean noConflictInBlock(int[][] candidateMatrix, int row, int col) { int baseRow = row / 3 * 3; int baseCol = col / 3 * 3; for (int rowNum = 0; rowNum < 8; rowNum++) { if (candidateMatrix[baseRow + rowNum / 3][baseCol + rowNum % 3] == 0) { continue; } for (int colNum = rowNum + 1; colNum < 9; colNum++) { if (candidateMatrix[baseRow + rowNum / 3][baseCol + rowNum % 3] == candidateMatrix[baseRow + colNum / 3][baseCol + colNum % 3]) { return false; } } } return true; } private int[] buildRandomArray() { currentTimes++; int[] array = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; int randomInt = 0; for (int i = 0; i < 20; i++) { randomInt = random.nextInt(8) + 1; int temp = array[0]; array[0] = array[randomInt]; array[randomInt] = temp; } return array; } public int getCurrentTimes() { return currentTimes; } public void setCurrentTimes(int currentTimes) { this.currentTimes = currentTimes; } } Interface and judgment:
Written with swing
import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.util.Random; public class ShuD extends JFrame{ private static final long serialVersionUID = 5952689219411916553L; //Serialize field private static JTextField a[][] = new JTextField[9][9]; //Storage the numbers in the text box static int ans[][] = new int[9][9]; //Storing the input two-digit array SudokuPuzzleGenerator example = new SudokuPuzzleGenerator(); public int right[][] = example.generatePuzzleMatrix(); public int rights[][]; private int[][] Wk(int a[][]){ //Hidden Random r = new Random(); int a1, a2; a1 = r.nextInt(9); a2 = r.nextInt(9); for(int i = 0; i < 100; i++) { a[a1][a2] = 0; a1 = r.nextInt(9); a2 = r.nextInt(9); } return a; } public ShuD(){ Container c = getContentPane(); c.setLayout(new BorderLayout(2, 1)); //Border layout JMenuItem jmiOk = new JMenuItem("Submit"); //Define menu JMenuItem jmiExplain = new JMenuItem("Details"); JMenuItem jmiMessage = new JMenuItem("Information"); JPanel panel = new JPanel(); //Define a container panel.add(jmiOk); //Show the menu in the container panel.add(jmiExplain); panel.add(jmiMessage); JPanel p1 = new JPanel(new GridLayout(9, 9, 5, 5)); //Define the grid layout of 9 rows and 9 columns add(panel,BorderLayout.NORTH); //Place the menu to the north add(p1,BorderLayout.CENTER); //Place the numbers in the middle rightans = Wk(right); for(int k = 0;k<9; k ++) { for(int n=0;n<9;n++) { if(rightans[k][n] != 0) { a[k][n] = new JTextField("" + rightans[k][n]); a[k][n].setHorizontalAlignment(JTextField.CENTER);//Center the number horizontally a[k][n].setEditable(false); //Only display unmodified p1.add(a[k][n]); //Add text box} else { a[k][n] = new JTextField(); a[k][n].setHorizontalAlignment(JTextField.CENTER); p1.add(a[k][n]); } } add(p1); //Show the digital panel in the container jmiOk.addActionListener(new ActionListener(){//Create anonymous event listener public void actionPerformed(ActionEvent e) { if(gettext() == 1) { if(judge() == true) { JOptionPane.showMessageDialog(null, "Your answer is right!","Result",JOptionPane.INFORMATION_MESSAGE); } else { JOptionPane.showMessageDialog(null, "Your answer is right!","Result",JOptionPane.INFORMATION_MESSAGE); } else { JOptionPane.showMessageDialog(null, "Your answer is wrong!","Result",JOptionPane.INFORMATION_MESSAGE); } } } } }); explainListenerClass listener2 = new explainListenerClass(); jmiExplain.addActionListener(listener2); messageListenerClass listener3 = new messageListenerClass(); jmiMessage.addActionListener(listener3); } static int gettext() //Get the text box's text { int i,j; for(i = 0; i < 9; i++) { for(j = 0; j < 9 ; j ++) { ans[i][j] = 0; } } for(int k = 0;k < 9; k++) { for(int n = 0;n < 9; n++) { try //Exception handling { ans[k][n] = Integer.parseInt(a[k][n].getText()); //Convert the answer type to ans } catch(NumberFormatException nfe) { JOptionPane.showMessageDialog(null,"Include non-numbers in the data, please re-enter"); return 0; } } } return 1; } public static boolean judge() //Judge whether the input answer is correct { int i,j,k; int [][]answer = ans; for(i = 0; i < 9; i ++) { if(j(answer[i]) == false) //Judge whether there are duplicate numbers in each column return false; } for(j = 0; j < 9; j ++) //Judge whether there are duplicate numbers in each row { int[] newAnswerColumn = new int[9]; for(i = 0; i < 9; i ++) { newAnswerColumn[i] = answer[i][j]; } if(j(newAnswerColumn) == false) return false; } for(i = 0; i < 3; i ++) //Judge whether there are duplicate numbers in each ninth grid{ for(j = 0; j < 3; j ++) { k = 0; int[] newAnswer = new int[9]; for(int m = i * 3; m < i * 3 + 3; m ++) { for(int n = j * 3; n < j * 3 + 3; n ++) { newAnswer[k] = answer[m][n]; k++; } } if(judge9(newAnswer) == false) { return false; } } return true; } public static boolean judge9(int[] answer) { int i,j; for(i = 0; i < 9; i ++) { for(j = 0; j < 9; j ++) { if(i == j) continue; if(answer[i] == answer[j]) //If there are duplicate numbers, return false { return false; } } } return true; //No duplicate numbers, return true } public static void main(String[] args) { JFrame frame = new ShuD(); frame.setTitle("SuDuku"); frame.setSize(600,900); frame.setLocationRelativeTo(null); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } } class explainListenerClass implements ActionListener{ //Event Listenerpublic void actionPerformed(ActionEvent e){ JOptionPane.showMessageDialog(null, "Fill in numbers to ensure that there is no duplication of numbers in each row, each column and each small nine-grid", "Explain",JOptionPane.INFORMATION_MESSAGE); } } class messageListenerClass implements ActionListener{ public void actionPerformed(ActionEvent e){ JOptionPane.showMessageDialog(null, "made by wyx","Message",JOptionPane.INFORMATION_MESSAGE); } }The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.