Android development instance-automatically generate the data independence of the Question Bank (1)

Source: Internet
Author: User

Android development instance-automatically generate the data independence of the Question Bank (1)

 

This series of articles mainly introduces how to use Android to develop a Sudoku game that automatically generates questions. The knowledge and technology involved are as follows:

The digging algorithm automatically generates the sudoku question. Custom View is used to draw the basic operations of the sudoku database.

 

Looking at the same number of unique applications on the market, most of them come from the same open-source application. The questions are fixed as many as 100. Is there no way to change the sudoku question? After Baidu search, I finally found an algorithm for automatically generating a data-independence question bank. Thanks to the original author's theory and some code on the network. Algorithm document library: IBTlo2hrjqcUOMcQ39ddYx1PaOnF0wjQycQTSYKIHJ5JUK-7WCK9Tz_BvDXQcio8I22k_xu1RZkwUYlUqFTZSa-jzyxgDfY3Ga93U34u

The algorithm IDEA has been described in this document. Based on the algorithm and some code on the network, we encapsulate Sudoku, an entity that can generate any number of difficulties. The Code is as follows:

Import java. util. ArrayList; import java. util. Random;/*** implemented using a hole mining algorithm. The minimum number of filled grids and the number of known grids vary depending on the difficulty. **/public class Sudoku {private int [] [] orginData; // Save the sudoku in the initial state // initialize and generate the array private int [] [] sudokuData = {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, 0, 0, 0, 0, 0, 0, 0, 0, 0} }; // The final disk array private int [] [] resultData; // the minimum number of filled grids private int minFilled; // the minimum number of known grids private int minKnow; // the current difficulty level; 1-2 simple difficulty; 3 General difficulty; 4 advanced difficulty; 5 hardcore difficulty private int level; private Random ran = new Random (); public Sudoku () {// this (3 ); // The default value is general difficulty} public Sudoku (int level) {if (level <0 | level> 6) {this. level = 3;} else {this. level = level;} switch (level) {case 1: case 2: int ranNum = ran. nextInt (10); if (ranNum> 4) {mi NKnow = 5;} else {minKnow = 4;} minFilled = 45 + ranNum; break; // case 2: // minFilled = 45 + ran. nextInt (5); // minKnow = 4; // break; case 3: minFilled = 31 + ran. nextInt (10); minKnow = 3; break; case 4: minFilled = 21 + ran. nextInt (10); minKnow = 2; break; case 5: minFilled = 17 + ran. nextInt (10); minKnow = 0; break; default: break;} genSuduku (); orginData = new int [9] [9]; for (int I = 0; I <9; I ++) {System. arraycopy (su DokuData [I], 0, orginData [I], 0, 9) ;}/ *** generate a unique solution for a Sudoku */public void genSuduku () {int startX = ran. nextInt (9), startY = ran. nextInt (9); // The initial dig-in Lattice int orignStartX = startX, orignStartY = startY; // The initial checkpoint int filledCount = 81; // number of currently known grids // int curMinKnow = 9; // the lower limit genShuduKnow (11) of the currently known rows and columns; if (solve (sudokuData, true )) {// assign the end disk to the initial Sudoku array for (int I = 0; I <9; I ++) {System. arraycopy (resultData [I], 0, sudo KuData [I], 0, 9);} // perform the equivalent exchange sudokuData = Transaction change (sudokuData); Point nextP; // do {int temMinKnow = getMinknow (sudokuData, startX, startY); if (isOnlyAnswer (sudokuData, startX, startY) & temMinKnow> = minKnow) {sudokuData [startX] [startY] = 0; filledCount --; // curMinKnow = temMinKnow;} nextP = next (startX, startY); startX = nextP. x; startY = nextP. y; // check whether this hole has been dug while (sudokuData [startX] [startY] = 0 & (OrignStartX! = StartX | orignStartY! = StartY) {nextP = next (startX, startY); startX = nextP. x; startY = nextP. y;} // if (level = 1 | level = 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 the unique solution Sudoku until it succeeds ();}} /*** get the next digging Point ** @ 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 all random algorithms 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 snake algorithm p = new Point (); if (x = 8 & y = 8) {p. y = 0;} else if (x % 2 = 0 & y <8) {// solve the next position column in the snake-like order 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) {// solve the next position row in the snake-like order 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, from 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 Grid knows ** @ param n */public void genShuduKnow (int n) {// generate n known grid points [] knowPonits = new Point [n]; for (int I = 0; I <n; I ++) {knowPonits [I] = new Point (ran. nextInt (9), ran. nextInt (9); // checks whether duplicate for (int k = 0; k <I; k ++) {if (knowPonits [k]. equals (knowPonits [I]) {I --; break ;}}// fill in the number 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; I F (! ValidateIandJ (sudokuData, p. x, p. y) {// error I --;}}} /*** equivalent swapping ** @ param data * @ return */public int [] [] Random change (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;}/*** after I is mined, is there 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 be dug for (int num = 1; num <10; num ++) {data [I] [j] = num; if (num! = K & solve (data, false) {// returns falsedata [I] [j] = k in addition to the number to be dug; return false;} data [I] [j] = k; return true;}/*** after deleting a specified location, new row and column lower limit ** @ 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 ++) {// lower limit of the row 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 ++) {// lower limit of the column for (int I = 0; I <9; I ++) {if (data [I] [j] = 0) {tempKnow --; if (tempKnow <minKnow) {minKnow = tempKnow ;}} tempKnow = 9 ;} data [p] [q] = temp; // return the lower limit after deletion. return minKnow ;} /*** determine whether a Sudoku can be solved ** @ param data * @ return */public boolean solve (int [] [] data, boolean flag) {int blankCount = 0; // Save the number of empty spaces int [] [] tempData = new int [9] [9]; // The ArrayList of the intermediate Array
 
  
BlankList = new ArrayList
  
   
(70); // Save the space coordinates 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) ;}// verify the legitimacy of a Sudoku if (! Validate (tempData) {return false;} if (blankCount = 0) {// the player has successfully solved the return true;} else if (put (tempData, 0, blankList) {if (flag) {// calculate the answer intelligently to generate a Sudoku ultimate disk using resultData = tempData;} return true;} return false ;} /*** place the number in the nth empty seat ** @ param data * @ param n * @ param blankList * @ return */public boolean put (int [] [] data, int n, ArrayList
   
    
Blanclist) {if (n <blanclist. size () {Point p = blankList. get (n); for (int I = 1; I <10; I ++) {data [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;}/*** verify x, y: Is the number filled in? ** @ 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 the counter, p, q is used to determine the square position of the test point for (m = 0; m <9; m ++) {if (m! = X & data [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) & (data [p + m] [q + n] = data [x] [y]) {return false ;}} return true ;} /*** verify the validity 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) {// If any digit is filled incorrectly, falsereturn false ;}} return true ;} /*** get the conflict list ** @ return */public ArrayList
    
     
ValidateForList () {ArrayList
     
      
PList = new ArrayList
      
        (); For (int I = 0; I <9; I ++) {for (int j = 0; j <9; j ++) {if (sudokuData [I] [j]! = 0 &&! ValidateIandJ (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 whether the grid is a known grid * @ 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 ;}}
      
     
    
   
  
 

 

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.