生成Sukodu随机数

时间:2015-09-14 09:43:32

标签: java android sudoku

目前我正在开发Sudoku应用程序。我需要生成一个给定长度的随机数。我知道我可以循环那么多次。但是如何在android中实现。

我直接将数独字符串包含为

Public static final String  E1="020178030040302090100000006"+ "008603500300000004006709200"+"900000002080901060010436050"; 

但是我想随机生成这个字符串(这个81位数字)

1 个答案:

答案 0 :(得分:0)

经过大量的参考,我得出结论,随机找到数独生成

公共课Sudokugeneration {

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 } };

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

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

 int gc = 0;

切换行  随机选择要更改的两行中的一行

用于生成拼图

    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("firstcol"+"="+firstcol);
            System.out.println("secondcol"+"="+secondcol);*/

            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);

    for (int k = 0; k < shuffle; k++) {
        firstnum = 1 + R_exnum.nextInt(9);
        secondnum = 1 + R_exnum.nextInt(9);


        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

用于保存拼图

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

    gc = 1;

    return blockc;