When I was learning JavaScript, I wrote a Sudoku game in JavaScript. I recently read some java content, so I wanted to create a Java version of Sudoku game.
Now I will share all the code and learn from you. Of course, there are various problems and shortcomings in the code. I hope you will criticize and give me some advice.
The following is the core algorithm for generating Sudoku maps. The algorithm is not very good, and I have also referred to some ideas online before:
package hlc.shudu.src;/* * Sudoku help class, which provides all the algorithms required for data*/public class ShuduHelper { //Sudoku map array private static int[][] maps = new int[9][9]; //The number of positions that can be placed in each nine-meter grid private static int[] canPutSum = new int[9]; //Use to store previously placed static int[] used = new int[9]; //Whether the map generation has been completed static boolean isOk = true; /* * Get the Sudoku map array*/ public static int[][] getMap() { //Judge whether the map has been generated and regenerate if it is not completed. // From this we can see that the algorithm still needs to be optimized. If the backtrack is good, it can be regenerated through backtracking, and here it is regenerated by re-executing the generation algorithm. I hope that interested friends can implement the following. do{ isOk = true; initMaps(); }while(!isOk); return maps; } /* * Initialize maps */ private static void initMaps() { // Initialize map array without any number for (int i = 0; i < 9; i++) { for (int j = 0; j < 9; j++) { maps[i][j] = -1; } } // Fill in 1~9 for (int num = 1; num <= 9; num++) { for (int i = 0; i < 9; i++) { used[i] = -1; canPutSum[i] = -1; } // traverse each ninth grid in the big nine grid for (int i = 0; i < 9; i++) { if (canPutSum[i]==-1) { canPutSum[i] = getCanPutSum(i, num); } if (canPutSum[i]==1) { used[i] = -1; } if (canPutSum[i] == 0) { canPutSum[i] = -1; used[i] = -1; // If the number num cannot be placed in the current nine grid, then return to the previous nin if (i > 0) { // Clear the position where num is placed in the previous nine grid if (used[i-1]!=-1) { //maps[(int) (Math.floor(used[i-1]/3)+Math.floor((i-1)/3)*3)][used[i-1]%3+((i-1)%3)*3]=-1; clearNum(i - 1, num); } // i falls back one, because the for loop gray will add one to i, so 2 is subtracted here i -= 2; continue; } else { isOk = false; return; } } else { // Put num into the current ninth grid boolean flag = false; while (!flag) { int j = (int) (Math.random() * 9); // The horizontal coordinate of the current small square int ii = (i / 3) * 3 + j / 3; // The current vertical coordinate of the small square int jj = (i % 3) * 3 + j % 3; //System.out.println("num:"+num+"/tii:"+ii+"/tjj:"+jj); // If num can be placed, place if (maps[ii][jj] == -1 && j!=used[i] && isCanPut(ii, jj, num)) { maps[ii][jj] = num; used[i] = j; canPutSum[i] -= 1; flag = true; } } } } } } } } /* * Clear num in the ith ninth ninth grid */ private static void clearNum(int i, int num) { for (int j = 0; j < 9; j++) { // The horizontal coordinate of the current small square int ii = (i / 3) * 3 + j / 3; // The vertical coordinate of the current small square int jj = (i % 3) * 3 + j % 3; // Determine whether the current small square can be placed if (maps[ii][jj] == num) { maps[ii][jj] = -1; } } } /* * Get the number of positions where the current nine-client can be placed in the number num*/ private static int getCanPutSum(int i, int num) { int sum = 0; // Traverse the nine-client for (int j = 0; j < 9; j++) { // The horizontal coordinate of the current small square int ii = (i / 3) * 3 + j / 3; // The vertical coordinate of the current small square int jj = i % 3 * 3 + j % 3; // Determine whether the current small square can be placed if (maps[ii][jj] == -1 && isCanPut(ii, jj, num)) { ++sum; } } return sum; } /* * Specify whether the horizontal and vertical coordinate points can be placed num */ private static boolean isCanPut(int ii, int jj, int num) { // Determine whether the same number of the same row or column of the specified coordinate point has the same number. If there is, it is false for (int i = 0; i < 9; i++) { if (maps[ii][i] == num) { return false; } if (maps[i][jj] == num) { return false; } } return true; }}The complete package can be downloaded on GitHub: https://github.com/houlongchao/s
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.