数独游戏界面功能
根据题目要求,本次采用JAVA添加GUI界面,并根据上次老师点评的要求下对原数独实现进行一定改进,因此,JAVA源码如下:
1、GUI界面设计如下
1 package JAVA练习; 2 import javax.swing.*; 3 import java.awt.*; 4 import java.awt.event.*; 5 import java.util.Random; 6 7 public class ShuDu extends JFrame{ 8 private static final long serialVersionUID = 5952689219411916553L; //序列化字段 9 private static JTextField a[][] = new JTextField[9][9]; //存储文本框中的数字 10 static int ans[][] = new int[9][9]; //存储输入后的两位数组 11 SudoGenerator example = new SudoGenerator(); 12 public int right[][] = example.generatePuzzleMatrix(); 13 public int rightans[][]; 14 private int[][] Wk(int a[][]){ //挖空 15 Random r = new Random(); 16 int a1, a2; 17 a1 = r.nextInt(9); 18 a2 = r.nextInt(9); 19 for(int i = 0; i < 100; i++) 20 { 21 a[a1][a2] = 0; 22 a1 = r.nextInt(9); 23 a2 = r.nextInt(9); 24 } 25 return a; 26 } 27 public ShuDu(){ 28 Container c = getContentPane(); 29 c.setLayout(new BorderLayout(2, 1)); //边框布局 30 JMenuItem jmiOk = new JMenuItem("提交"); //定义菜单 31 JMenuItem jmiExplain = new JMenuItem("详情"); 32 JMenuItem jmiMessage = new JMenuItem("信息"); 33 34 JPanel panel = new JPanel(); //定义一个容器 35 panel.add(jmiOk); //将菜单在容器内显示 36 panel.add(jmiExplain); 37 panel.add(jmiMessage); 38 JPanel p1 = new JPanel(new GridLayout(9, 9, 5, 5)); //定义9行9列的网格布局 39 add(panel,BorderLayout.NORTH); //将菜单放置在北面 40 add(p1,BorderLayout.CENTER); //将数字放置在正中间 41 rightans = Wk(right); 42 for(int k = 0;k<9; k ++) 43 { 44 for(int n=0;n<9;n++) 45 { 46 if(rightans[k][n] != 0) 47 { 48 a[k][n] = new JTextField("" + rightans[k][n]); 49 a[k][n].setHorizontalAlignment(JTextField.CENTER);//将数字水平居中 50 a[k][n].setEditable(false); //只可显示不可修改 51 p1.add(a[k][n]); //添加文本框 52 } 53 else 54 { 55 a[k][n] = new JTextField(); 56 a[k][n].setHorizontalAlignment(JTextField.CENTER); 57 p1.add(a[k][n]); 58 } 59 } 60 } 61 add(p1); //将数字面板显示在容器里 62 jmiOk.addActionListener(new ActionListener(){//匿名创建事件监听器 63 public void actionPerformed(ActionEvent e) 64 { 65 if(gettext() == 1) 66 { 67 if(judge() == true) 68 { 69 JOptionPane.showMessageDialog(null, "Your answer is right!","Result",JOptionPane.INFORMATION_MESSAGE); 70 } 71 else 72 { 73 JOptionPane.showMessageDialog(null, "Your answer is wrong!","Result",JOptionPane.INFORMATION_MESSAGE); 74 } 75 } 76 } 77 }); 78 explainListenerClass listener2 = new explainListenerClass(); 79 jmiExplain.addActionListener(listener2); 80 messageListenerClass listener3 = new messageListenerClass(); 81 jmiMessage.addActionListener(listener3); 82 } 83 84 static int gettext() //获取文本框的文字 85 { 86 int i,j; 87 for(i = 0; i < 9; i++) 88 { 89 for(j = 0; j < 9 ; j ++) 90 { 91 ans[i][j] = 0; 92 } 93 } 94 for(int k = 0;k < 9; k++) 95 { 96 for(int n = 0;n < 9; n++) 97 { 98 try //异常处理 99 { 100 ans[k][n] = Integer.parseInt(a[k][n].getText()); //将答案类型转换之后传给ans 101 } 102 catch(NumberFormatException nfe) 103 { 104 JOptionPane.showMessageDialog(null,"数据中包括非数字,请重新输入"); 105 return 0; 106 } 107 } 108 } 109 return 1; 110 } 111 public static boolean judge() //判断输入的答案是否正确 112 { 113 int i,j,k; 114 int [][]answer = ans; 115 116 for(i = 0; i < 9; i ++) 117 { 118 if(judge9(answer[i]) == false) //判断每列是否有重复数字 119 return false; 120 } 121 for(j = 0; j < 9; j ++) //判断每行是否有重复数字 122 { 123 124 int[] newAnswerColumn = new int[9]; 125 for(i = 0; i < 9; i ++) 126 { 127 newAnswerColumn[i] = answer[i][j]; 128 } 129 if(judge9(newAnswerColumn) == false) 130 return false; 131 } 132 for(i = 0; i < 3; i ++) //判断每个小九宫格内是否有重复数字 133 { 134 for(j = 0; j < 3; j ++) 135 { 136 k = 0; 137 int[] newAnswer = new int[9]; 138 for(int m = i * 3; m < i * 3 + 3; m ++) 139 { 140 for(int n = j * 3; n < j * 3 + 3; n ++) 141 { 142 newAnswer[k] = answer[m][n]; 143 k++; 144 } 145 } 146 if(judge9(newAnswer) == false) 147 { 148 return false; 149 } 150 } 151 } 152 return true; 153 } 154 public static boolean judge9(int[] answer) 155 { 156 int i,j; 157 for(i = 0; i < 9; i ++) 158 { 159 for(j = 0; j < 9; j ++) 160 { 161 if(i == j) 162 continue; 163 if(answer[i] == answer[j]) //如果有重复的数字,返回false 164 { 165 return false; 166 } 167 } 168 } 169 return true; //没有重复数字,返回true 170 } 171 172 public static void main(String[] args) { 173 JFrame frame = new ShuDu(); 174 frame.setTitle("软件工程数独求解"); 175 frame.setSize(600,700); 176 frame.setLocationRelativeTo(null); 177 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 178 frame.setVisible(true); 179 } 180 } 181 class explainListenerClass implements ActionListener{ //事件监听器 182 public void actionPerformed(ActionEvent e){ 183 JOptionPane.showMessageDialog(null, "填入数字保证每行每列及每个小的九宫格内数字无重复","Explain",JOptionPane.INFORMATION_MESSAGE); 184 } 185 } 186 class messageListenerClass implements ActionListener{ 187 public void actionPerformed(ActionEvent e){ 188 JOptionPane.showMessageDialog(null, "made by wyx","Message",JOptionPane.INFORMATION_MESSAGE); 189 } 190 }
2、后端处理如下
package JAVA练习; import java.util.Random; public class SudoGenerator { 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; } }
3、程序界面如下
4、程序分析:本次作业基于上一次的数独作业,改正了数独生成个数0<N<1000000,并且代码已上传至conding,
https://git.coding.net/dhlg_201810812005/ShuDu.git
5、学习心得
本次处理根据上一次老师点评的要求,有了较大的改进,但还是有很多的地方不是很懂,为此,进行了较长时间的学习,特别是添加GUI设计界面,还是事件监控等,本次程序设计并不是完全由个人开发,很大程度是参考他人代码而实现