數獨遊戲介面功能
阿新 • • 發佈:2018-11-09
根據題目要求,本次採用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[][]; 14private 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設計介面,還是事件監控等,本次程式設計並不是完全由個人開發,很大程度是參考他人程式碼而實現