Aswin Balaji

Aswin Balaji

186 Sadasivam Nagar,Madipakkam,Chennai, TN 600091
Aswin Balaji

Android Sudoku Random Generation

September 15, 2015, by Aswin Balaji, category ANDROID
Sudoku took the world by storm in 2005, quickly becoming the latest puzzle craze in both print and online formats. Often referred to as a Japanese number puzzle, in actual fact, Sudoku has a long history with ancient origins. It is thought that Latin Squares, known since medieval times, are the progenitors of the modern grid.

Contrary to popular belief, you do not need to be good at maths to solve a Sudoku! The puzzles are based on the principles of logic and recent variants have been created which use systems of letters, shapes or colours instead of numbers.

Scientific studies have recently shown that the focused mental activity used to solve puzzles can help prevent memory loss and degenerative brain disorders such as Alzheimer's - another reason to get your daily dose of puzzling fun!
Read the Wikipedia article on Sudoku:http://en.wikipedia.org/wiki/Sudoku

How do you play sudoku ?

To solve YouPlay’s online Sudoku, every number from 1 to 9 must appear in:

  • Each of the nine vertical columns
  • Each of the nine horizontal rows
  • Each of the nine 3 x 3 boxes

Use the mouse to click up (or down) through the numbers 1 to 9 in each square. The small black arrows on each side of the square tell you whether you are counting from 1 to 9 (right arrow) or from 9 down to 1 (left arrow).

Using the keyboard, you can type in up to eight practice numbers above the line at the top of each box.

When you have correctly completed a line, column or a 3 x 3 box it will be filled with colour and any practice numbers will disappear.

import java.util.Random;

/** * Created by Windows8 on 15-Sep-15. */public class Sudokugeneration {

   static int[][] blockS = { { 4, 3, 5, 8, 7, 6, 1, 2, 9 },
                      { 8, 7, 6, 2, 1, 9, 3, 4, 5 },
                      { 2, 1, 9, 4, 3, 5, 7, 8, 6 },
                      { 5, 2, 3, 6, 4, 7, 8, 9, 1 },
                      { 9, 8, 1, 5, 2, 3, 4, 6, 7 },
                      { 6, 4, 7, 9, 8, 1, 2, 5, 3 },
                      { 7, 5, 4, 1, 6, 8, 9, 3, 2 },
                      { 3, 9, 2, 7, 5, 4, 6, 1, 8 },
                      { 1, 6, 8, 3, 9, 2, 5, 7, 4 } };

    static Random R_num = new Random(); // random numbers to exchange rows
 static Random Grid_R_num = new Random();// random numbers to exchange GRIDS
static Random R_exnum = new Random();
    static Random H_Rnum = new Random();
    static int carry[] = new int[9];
    static int[][] blockh = new int[9][9];
    static int[][] blockc = new int[9][9];

    static  int firstrow,secondrow,firstcol,secondcol,firstgrid,secondgrid;

    static int gc = 0;


   public static int[][] generate() { // //switching the rows
 // randomly choosing one of the tow rows to be changed
 int x = 10 + R_num.nextInt(10);

        for (int y = 0; y < x; y++) {
            for (int a = 0; a < 3; a++) {
                // System.out.println("a="+a);
                if (a == 0) {
                    firstrow = R_num.nextInt(3);
                    secondrow = R_num.nextInt(3);
                }

                else if (a == 1) {
                    firstrow = 3 + R_num.nextInt(3);
                    secondrow = 3 + R_num.nextInt(3);
                }

                else if (a == 2) {
                    firstrow = 6 + R_num.nextInt(3);
                    secondrow = 6 + R_num.nextInt(3);
                }

                System.out.println("firstrow"+"="+firstrow);
                System.out.println("secondrow"+"="+secondrow);

                for (int i = 0; i < 9; i++) {
                    carry[i] = blockS[firstrow][i];
                    blockS[firstrow][i] = blockS[secondrow][i];
                    blockS[secondrow][i] = carry[i];
                }
            }
            // switching the rows complete
            // Switchicng the column            for (int a = 0; a < 3; a++) {
                System.out.println("a="+a);

                if (a == 0) {
                    firstcol = R_num.nextInt(3);
                    secondcol = R_num.nextInt(3);
                }

                else if (a == 1) {
                    firstcol = 3 + R_num.nextInt(3);
                    secondcol = 3 + R_num.nextInt(3);
                }

                else if (a == 2) {
                    firstcol = 6 + R_num.nextInt(3);
                    secondcol = 6 + R_num.nextInt(3);
                }

                System.out.println("firstrow"+"="+firstrow);
                System.out.println("secondrow"+"="+secondrow);

                for (int i = 0; i < 9; i++) {
                    carry[i] = blockS[i][firstcol];
                    blockS[i][firstcol] = blockS[i][secondcol];
                    blockS[i][secondcol] = carry[i];
                }
            }
        }
        // Switchicng the column complet        // Switchicng the grids
firstgrid = 1 + Grid_R_num.nextInt(3);
        secondgrid = 1 + Grid_R_num.nextInt(3);

        System.out.println("firstgrid"+"="+firstgrid);
        System.out.println("secondgrid"+"="+secondgrid);

        if ((firstgrid == 1 && secondgrid == 2) || (firstgrid == 2 && secondgrid == 1)) {
            for (int i = 0; i < 3; i++)
                for (int j = 0; j < 9; j++) {
                    carry[j] = blockS[i][j];
                    blockS[i][j] = blockS[i + 3][j];
                    blockS[i + 3][j] = carry[j];
                }
        } else if ((firstgrid == 2 && secondgrid == 3) || (firstgrid == 3 && secondgrid == 2)) {
            for (int i = 3; i < 6; i++)
                for (int j = 0; j < 9; j++) {
                    carry[j] = blockS[i][j];
                    blockS[i][j] = blockS[i + 3][j];
                    blockS[i + 3][j] = carry[j];
                }
        } else if ((firstgrid == 1 && secondgrid == 3) || (firstgrid == 3 && secondgrid == 1)) {
            for (int i = 0; i < 3; i++)
                for (int j = 0; j < 9; j++) {
                    carry[j] = blockS[i][j];
                    blockS[i][j] = blockS[i + 6][j];
                    blockS[i + 6][j] = carry[j];
                }
        }
        // Swithing complete of tow grids
        // suffling the puzzle        int firstnum, secondnum, shuffle;

        shuffle = 3 + R_num.nextInt(6);
        System.out.println("Shufflenum="+shuffle);
        for (int k = 0; k < shuffle; k++) {
            firstnum = 1 + R_exnum.nextInt(9);
            secondnum = 1 + R_exnum.nextInt(9);

            System.out.println("firstnum="+firstnum);
            System.out.println("secondnum="+secondnum);

            for (int i = 0; i < 9; i++)
                for (int j = 0; j < 9; j++) {
                    if (blockS[i][j] == firstnum) {
                        blockS[i][j] = secondnum;
                        continue;
                    }

                    if (blockS[i][j] == secondnum)
                        blockS[i][j] = firstnum;
                }
        }
        return blockS;
    }

    // will save the complet puzzle  public   static int[][] save() {

        if (gc == 0)
            blockc = generate();

        gc = 1;

        return blockc;
    }


    // will hide number of puzzle
  public   static int[][] hide() {
        for (int i = 0; i < 9; i++)
            for (int j = 0; j < 9; j++)
                blockh[i][j] = blockc[i][j];

        int row, column, hidingnum;

        hidingnum = 50 + R_num.nextInt(10);

        for (int i = 0; i < hidingnum; i++) {
            row = H_Rnum.nextInt(9);
            column = H_Rnum.nextInt(9);
            System.out.println("n row,col = ("+row+","+column+")");
            blockh[row][column] = 0;
        }
        return blockh;
    }


}

By Converting the 2D array into the String by using stringbuffer

static public String toPuzzleString(int[][] puz) {
    StringBuilder buf = new StringBuilder();
    for(int row = 0; row < puz.length; row++) {
        for(int col = 0; col < puz[row].length; col++) {
            buf.append(puz[row][col]);
        }
    }
    return buf.toString();
}