Android Development Example-automatic generation of Sudoku (one)

Source: Internet
Author: User

Reprint Please specify source: http://blog.csdn.net/einarzhang/article/details/44834259

This series focuses on using Android to develop a Sudoku game that automatically generates topics. The knowledge and techniques involved are as follows:

    • Automatic generation of Sudoku problem by burrowing algorithm
    • Implement a custom view to draw a Sudoku disc
    • Basic operations of the database

Looking at the market on the same Sudoku application, most of them come from the same open source application, the topic is fixed so that more than 100 way. Is there any way we can change the Sudoku problem? After Baidu search, finally found an automatic generation of Sudoku question Bank algorithm, thanks to the original author's theory and some code on the network. Algorithm document question bank: http://wenku.baidu.com/link?url=yA-IBTlo2hrjqcUOMcQ39ddYx1PaOnF0wjQycQTSYKIHJ5JUK-7WCK9Tz_BvDXQcio8I22k _xu1rzkwuyluqftzsa-jzyxgdfy3ga93u34u

The algorithm idea has been described in the document is very specific, we based on the algorithm and some of the code on the network package can generate arbitrary difficulty sudoku Entity Sudoku, the code is as follows:

Import Java.util.arraylist;import java.util.random;/** * is implemented using tunneling algorithms. Different difficulty will have different minimum padding and known number of squares * */public class Sudoku {private int[][] orgindata;//save initial state of Sudoku//Initialize generate array private int[][] Sudokuda Ta = {0, 0, 0, 0, 0, 0, 0, 0, 0},{0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0},{0, 0, 0, 0, 0, 0, 0 , 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0},{0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0},{0, 0, 0, 0, 0  , 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0}};//final disk array private int[][] resultdata;//min. padding lattice private int minfilled;//minimum known lattice number private int minknow;//Current difficulty level, 1-2 simple difficulty, 3 ordinary difficulty, 4 advanced difficulty, 5 level difficulty private int level;private random ran = new random ();p ublic Sudoku () {//th Is (3); The default is normal difficulty}public Sudoku (int level) {if (Level < 0 | | levels > 6) {this.level = 3;} else {this.level = level;} Switch (level) {case 1:case 2:int rannum = ran.nextint), if (Rannum > 4) {minknow = 5;} else {minknow = 4;} minfilled = rannum;break;//case + 2://minfilled = + ran.nextint (5);//minknow = 4;//breAk;case 3:minfilled = + ran.nextint (ten); Minknow = 3;break;case 4:minfilled = + + ran.nextint; minknow = 2;break;cas E 5:minfilled = + ran.nextint (ten); minknow = 0;break;default:break;} Gensuduku (); orgindata = new Int[9][9];for (int i = 0; i < 9; i++) {system.arraycopy (Sudokudata[i], 0, Orgindata[i], 0, 9 );}}  /** * Generate unique solution for Sudoku */public void Gensuduku () {int startX = Ran.nextint (9), Starty = Ran.nextint (9);//initial burrowing lattice int orignstartx = StartX, orignstarty = Starty; Staging initial lattice int filledcount = 81; The currently known number of squares//int curminknow = 9; The lower bound of the currently known row and column (Genshuduknow), if (Solve (Sudokudata, True)) {//assigns the final disk to the initial Sudoku array for (int i = 0; i < 9; i++) {System.Array Copy (Resultdata[i], 0, Sudokudata[i], 0, 9);} The equivalent exchange Sudokudata = Equalchange (Sudokudata) is implemented; Point NEXTP; Next Burrow location do {int temminknow = Getminknow (Sudokudata, StartX, starty); if (Isonlyanswer (Sudokudata, StartX, Starty) && Amp Temminknow >= Minknow) {Sudokudata[startx][starty] = 0;filledcount--;//curminknow = TemMinKnow;} NEXTP = Next (STARtX, starty); StartX = Nextp.x;starty = nextp.y;//Check if this hole has been dug while (sudokudata[startx][starty] = = 0 && (orignstartx!) = StartX | | Orignstarty! = starty)) {nextp = Next (StartX, starty); StartX = Nextp.x;starty = nextp.y;} Primary difficulty handling if (level = = 1 | | levels = = 2) {while (StartX = = Orignstartx && Starty = = orignstarty) {nextp = next (StartX, Starty); StartX = Nextp.x;starty = Nextp.y;}}} while (Filledcount > minfilled&& (orignstartx! = StartX | | Orignstarty! = starty));} else {//regenerate unique coefficient until successful Gensuduku ();}} /** * Get the next dig hole * * @param x * @param y * @return */public point Next (int x, int. y) {point P = Null;switch (level) {Case 1 : Case 2://difficulty 1, 2 are random algorithm p = new Point (Ran.nextint (9), Ran.nextint (9)), Break;case 3://Difficulty 3 interval algorithm if (x = = 8 && y = = 7 {p = new Point (0, 0);} else if (x = = 8 && y = = 8) {p = new Point (0, 1);} else if ((x% 2 = = 0 && y = 7) | | (x 2 = = 1) && y = = 0) {p = new Point (x + 1, y + 1);} else if ((x% 2 = = 0 && y = = 8) | |(x 2 = = 1) && y = = 1) {p = new Point (x + 1, y-1),} else if (x% 2 = = 0) {p = new point (x, y + 2);} else if (x % 2 = = 1) {p = new point (x, y-2);}  Break;case 4://Difficulty 4 serpentine algorithm p = new Point (), if (x = = 8 && y = = 8) {p.y = 0;} else if (x 2 = = 0 && y < 8) {//Serpentine order, the solution for the next position column P.y = y + 1;} else if ((x% 2 = = 0 && y = = 8) | | (x% 2 = = 1 && y = = 0)) {p.y = y;} else if (x% 2 = = 1 && y > 0) {p.y = y-1;} if (x = = 8 && y = = 8) {//Serpentine order, solve p.x = 0;} else if ((x 2 = = 0 && y = 8) | | (x 2 = = 1) && y = = 0) {p.x = x + 1;} else {p.x = x;} Break;case 5://From left to right, top to bottom p = new Point (), if (y = = 8) {if (x = = 8) {p.x = 0;} else {p.x = x + 1;} p.y = 0;} else {p.x = X;p.y = y + 1;} Default:break;} return p;}  /** * Generate a specified number of known squares * * @param n */public void genshuduknow (int n) {//generate n known lattice point[] knowponits = new Point[n];for (int i = 0; I < n; i++) {Knowponits[i] = new Point (Ran.nextint (9), Ran.nextint (9));//Detect repeat for (int k= 0; K < I; k++) {if (Knowponits[k].equals (Knowponits[i])) {I--;break;}}} fill digit int num; Point p;for (int i = 0; i < n; i++) {num = 1 + ran.nextint (9);p = knowponits[i];sudokudata[p.x][p.y] = num;if (!validat Eiandj (Sudokudata, p.x, p.y)) {//Generate lattice fill number error i--;}}} /** * Equal Exchange * * @param data * @return */public int[][] Equalchange (int[][] data) {Random Rand = new Random (); int NUM1 = 1 + rand.nextint (9), int num2 = 1 + rand.nextint (9), for (int i = 0, i < 9; i++) {for (int j = 0; J < 9; J + +) {if (data[ I][J] = = 1) {Data[i][j] = NUM1;} else if (data[i][j] = = NUM1) {Data[i][j] = 1;} if (data[i][j] = = 2) {Data[i][j] = num2;} else if (data[i][j] = = num2) {Data[i][j] = 2;}}} return data;} /** * Determine if the i,j position has been dug to see if there is a unique solution * * @param data * @param i * @param j * @return */public Boolean isonlyanswer (int[][] data, int i , int j) {int k = data[i][j];//The original number to burrow for (int num = 1; num <; num++) {Data[i][j] = num;if (num! = k && S Olve (data, False)) {//In addition to the number to be dug, there are other solutions, then return falsedata[i][j] = K;return false;}} DATA[I][J] = K;return true;} /** * After deleting the specified position, the new row and column lower bound * * @param data * @param p * @param q * @return */public int getminknow (int[][] data, int p, int q) {int temp = Data[p][q];int Minknow = 9;int Tempknow = 9;data[p][q] = 0;for (int i = 0; i < 9; i++) {//Line lower bound for (int j = 0; J < 9; J + +) {if (data[i][j] = = 0) {tempknow--;if (Tempknow < Minknow) {minknow = Tempknow;}}} Tempknow = 9;} Tempknow = 9;for (int j = 0; J < 9; J + +) {//column lower bound for (int i = 0; i < 9; i++) {if (data[i][j] = 0) {tempknow--;if (te Mpknow < Minknow) {minknow = Tempknow;}}} Tempknow = 9;} DATA[P][Q] = temp;//returns the lower bound of the deleted return Minknow;} /** * Determine if Sudoku can be solved * * @param data * @return */public Boolean solve (int[][] data, Boolean flag) {int blankcount = 0;//Save Empty Number int[][] TempData = new INT[9][9]; Intermediate array arraylist<point> blanklist = new arraylist<point> (70); Save each space coordinate for (int i = 0; i < 9; i++) {for (int j = 0; J < 9; J + +) {Tempdata[i][j] = data[i][j];if (Tempdata[i][j] = = 0) {Blankcount++;blaNklist.add (new Point (I, J));}}} Check Sudoku Legitimacy if (!validate (TempData)) {return false;} if (Blankcount = = 0) {//the player has successfully solved the Sudoku return true;} else if (Put (tempdata, 0, blanklist)) {if (flag) {////Intelligent solution out of the answer, for generating a Sudoku final disk using Resul Tdata = TempData;} return true;} return false;} /** * in the nth empty seat puts the number * * @param data * @param n * @param blanklist * @return */public Boolean put (int[][] data, int n, Array List<point> blanklist) {if (N < blanklist.size ()) {point P = blanklist.get (n), for (int i = 1; i < ten; i++) {da TA[P.X][P.Y] = i;if (VALIDATEIANDJ (data, p.x, p.y) && put (data, n + 1, blanklist)) {return true;}} DATA[P.X][P.Y] = 0;return false;} else {return true;}} /** * check x, y position fill number is feasible * * @param data * @param x * @param y * @return */public boolean validateiandj (int[][] data, int x, int y) {int m = 0, n = 0, p = 0, q = 0;//M,n is a counter, p,q is used to determine the test point's grid position for (m = 0; m < 9; m++) {if (M! = x && dat A[m][y] = = Data[x][y]) {return false;}} for (n = 0; n < 9; n++) {if (n! = y && data[x][n] = = DaTa[x][y]) {return false;}} for (p = X/3 * 3, m = 0; m < 3; m++) {for (q = Y/3 * 3, n = 0; n < 3; n++) {if (p + M! = X | | q + n! = y) && Amp (data[p + m][q + N] = = Data[x][y])) {return false;}}} return true;}  /** * Verifying the legality of the entire Sudoku array * * @param data * @return */public Boolean validate (int[][] data) {for (int i = 0; i < 9; i++) {for (int j = 0; J < 9; J + +) {if (data[i][j]! = 0 &&!validateiandj (data, I, J)) {//any one digit fill error, return Falsereturn false;}}} return true;} /** * Get conflict list * @return */public arraylist<point> validateforlist () {arraylist<point> pList = new arraylist< Point> (); for (int i = 0, i < 9; i++) {for (int j = 0; J < 9; J + +) {if (sudokudata[i][j]! = 0 &&!validate Iandj (Sudokudata, I, J)) {Plist.add (new Point (I, J));}}} return pList;} Public int[][] Myresultdata () {return resultdata;} Public int[][] Myinitdata () {return orgindata;} Public int[][] Mysudoku () {return sudokudata;} /** * Determine if the lattice is a known lattice * @param x * @param y * @return */public BooLean Isknowncell (int x, int y) {return orgindata[x][y]! = 0;} public void Maketoinitdata () {for (int i = 0; i < 9; i++) {system.arraycopy (Orgindata[i], 0, Sudokudata[i], 0, 9);}} public void set (int x, int y, int num) {sudokudata[x][y] = num;}  public Boolean issuccess () {for (int i = 0; i < 9; i++) {for (int j = 0; J < 9; J + +) {//if (sudokudata[i][j] = = 0 | | SUDOKUDATA[I][J] = Resultdata[i][j]) {//Return false;//} if (sudokudata[i][j] = = 0) {return false;}} } return validate (Sudokudata);//return true;} public void Setorgindata (int[][] orgindata) {this.orgindata = Orgindata;} public void Setsudokudata (int[][] sudokudata) {this.sudokudata = Sudokudata;} public void Setresultdata (int[][] resultdata) {this.resultdata = Resultdata;}}


Android Development Example-automatic generation of Sudoku (one)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.