混合然后打印阵列,但它们最终都是相同的吗?

时间:2015-08-28 21:18:32

标签: java arrays

我正在进行数独游戏。我有一个数组(puzzle [] [] [])包含九个其他数组(zone1 [] [] - zone9 [] []),每个数组最初包含{{1,2,3},{4,5,6 },{7,8,9}}(并且他们已经从zone1.clone()中分配了这些值,因此它不是引用问题。)

我将这些值与:

混合
Random rand = new Random();
for(int z = 0; z < 9; z++) //cycles through zones
{
    for(int i = 0; i < 3; i++) //cycles through zone rows
    {
        for(int j = 0; j < 3; j++) //cycles through zone columns
        {
            int temp = puzzle[z][i][j];
            int newI = rand.nextInt(3);
            int newJ = rand.nextInt(3);
            puzzle[z][i][j] = puzzle[z][newI][newJ];
            puzzle[z][newI][newJ] = temp;
        }
    }
}

然后我通过将所有值添加到char数组(添加边框)将拼图打印到屏幕上。那里不应该有任何问题,因为我只是分别通过拼图[8] [2] [2]添加拼图[0] [0] [0]。

因此,当我运行该程序时,我希望得到9个&#34;区域&#34;具有不同的混合值。相反,我得到了这个:

/-----------------------\
| 2 1 5 | 2 1 5 | 2 1 5 |
| 6 9 8 | 6 9 8 | 6 9 8 |
| 4 3 7 | 4 3 7 | 4 3 7 |
|-------+-------+-------|
| 2 1 5 | 2 1 5 | 2 1 5 |
| 6 9 8 | 6 9 8 | 6 9 8 |
| 4 3 7 | 4 3 7 | 4 3 7 |
|-------+-------+-------|
| 2 1 5 | 2 1 5 | 2 1 5 |
| 6 9 8 | 6 9 8 | 6 9 8 |
| 4 3 7 | 4 3 7 | 4 3 7 |
\-----------------------/

我确定它很简单,但我只是没有看到它。

编辑:好的,这是打印部分。

char[][] display = {{'/','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','\\'},
        {'|',' ',(char)('0' + puzzle[0][0][0]),' ',(char)('0' + puzzle[0][0][1]),' ',(char)('0' + puzzle[0][0][2]),' ','|',' ',(char)('0' + puzzle[1][0][0]),' ',(char)('0' + puzzle[1][0][1]),' ',(char)('0' + puzzle[1][0][2]),' ','|',' ',(char)('0' + puzzle[2][0][0]),' ',(char)('0' + puzzle[2][0][1]),' ',(char)('0' + puzzle[2][0][2]),' ','|'},
        {'|',' ',(char)('0' + puzzle[0][1][0]),' ',(char)('0' + puzzle[0][1][1]),' ',(char)('0' + puzzle[0][1][2]),' ','|',' ',(char)('0' + puzzle[1][1][0]),' ',(char)('0' + puzzle[1][1][1]),' ',(char)('0' + puzzle[1][1][2]),' ','|',' ',(char)('0' + puzzle[2][1][0]),' ',(char)('0' + puzzle[2][1][1]),' ',(char)('0' + puzzle[2][1][2]),' ','|'},
        {'|',' ',(char)('0' + puzzle[0][2][0]),' ',(char)('0' + puzzle[0][2][1]),' ',(char)('0' + puzzle[0][2][2]),' ','|',' ',(char)('0' + puzzle[1][2][0]),' ',(char)('0' + puzzle[1][2][1]),' ',(char)('0' + puzzle[1][2][2]),' ','|',' ',(char)('0' + puzzle[2][2][0]),' ',(char)('0' + puzzle[2][2][1]),' ',(char)('0' + puzzle[2][2][2]),' ','|'},
        {'|','-','-','-','-','-','-','-','+','-','-','-','-','-','-','-','+','-','-','-','-','-','-','-','|'},
        {'|',' ',(char)('0' + puzzle[3][0][0]),' ',(char)('0' + puzzle[3][0][1]),' ',(char)('0' + puzzle[3][0][2]),' ','|',' ',(char)('0' + puzzle[4][0][0]),' ',(char)('0' + puzzle[4][0][1]),' ',(char)('0' + puzzle[4][0][2]),' ','|',' ',(char)('0' + puzzle[5][0][0]),' ',(char)('0' + puzzle[5][0][1]),' ',(char)('0' + puzzle[5][0][2]),' ','|'},
        {'|',' ',(char)('0' + puzzle[3][1][0]),' ',(char)('0' + puzzle[3][1][1]),' ',(char)('0' + puzzle[3][1][2]),' ','|',' ',(char)('0' + puzzle[4][1][0]),' ',(char)('0' + puzzle[4][1][1]),' ',(char)('0' + puzzle[4][1][2]),' ','|',' ',(char)('0' + puzzle[5][1][0]),' ',(char)('0' + puzzle[5][1][1]),' ',(char)('0' + puzzle[5][1][2]),' ','|'},
        {'|',' ',(char)('0' + puzzle[3][2][0]),' ',(char)('0' + puzzle[3][2][1]),' ',(char)('0' + puzzle[3][2][2]),' ','|',' ',(char)('0' + puzzle[4][2][0]),' ',(char)('0' + puzzle[4][2][1]),' ',(char)('0' + puzzle[4][2][2]),' ','|',' ',(char)('0' + puzzle[5][2][0]),' ',(char)('0' + puzzle[5][2][1]),' ',(char)('0' + puzzle[5][2][2]),' ','|'},
        {'|','-','-','-','-','-','-','-','+','-','-','-','-','-','-','-','+','-','-','-','-','-','-','-','|'},
        {'|',' ',(char)('0' + puzzle[6][0][0]),' ',(char)('0' + puzzle[6][0][1]),' ',(char)('0' + puzzle[6][0][2]),' ','|',' ',(char)('0' + puzzle[7][0][0]),' ',(char)('0' + puzzle[7][0][1]),' ',(char)('0' + puzzle[7][0][2]),' ','|',' ',(char)('0' + puzzle[8][0][0]),' ',(char)('0' + puzzle[8][0][1]),' ',(char)('0' + puzzle[8][0][2]),' ','|'},
        {'|',' ',(char)('0' + puzzle[6][1][0]),' ',(char)('0' + puzzle[6][1][1]),' ',(char)('0' + puzzle[6][1][2]),' ','|',' ',(char)('0' + puzzle[7][1][0]),' ',(char)('0' + puzzle[7][1][1]),' ',(char)('0' + puzzle[7][1][2]),' ','|',' ',(char)('0' + puzzle[8][1][0]),' ',(char)('0' + puzzle[8][1][1]),' ',(char)('0' + puzzle[8][1][2]),' ','|'},
        {'|',' ',(char)('0' + puzzle[6][2][0]),' ',(char)('0' + puzzle[6][2][1]),' ',(char)('0' + puzzle[6][2][2]),' ','|',' ',(char)('0' + puzzle[7][2][0]),' ',(char)('0' + puzzle[7][2][1]),' ',(char)('0' + puzzle[7][2][2]),' ','|',' ',(char)('0' + puzzle[8][2][0]),' ',(char)('0' + puzzle[8][2][1]),' ',(char)('0' + puzzle[8][2][2]),' ','|'},
        {'\\','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','/'}};

for(int i = 0; i < 13; i++)
{
    for(int j = 0; j < 25; j++)
    {
        System.out.print(display[i][j]);
    }
    System.out.println();
}

2 个答案:

答案 0 :(得分:0)

感谢您添加显示代码!我像这样优雅地把它们混为一谈:

import java.util.Random;

public class SudokuArray
{
    public static void main(String[] args)
    {
    new SudokuArray();
    }

    public SudokuArray()
    {
    int[][][] puzzle = {{{1,2,3},{4,5,6},{7,8,9}},
                {{1,2,3},{4,5,6},{7,8,9}},
                {{1,2,3},{4,5,6},{7,8,9}},
                {{1,2,3},{4,5,6},{7,8,9}},
                {{1,2,3},{4,5,6},{7,8,9}},
                {{1,2,3},{4,5,6},{7,8,9}},
                {{1,2,3},{4,5,6},{7,8,9}},
                {{1,2,3},{4,5,6},{7,8,9}},
                {{1,2,3},{4,5,6},{7,8,9}}};
    Random rand = new Random();
    for(int z = 0; z < 9; z++) //cycles through zones
    {
        for(int i = 0; i < 3; i++) //cycles through zone rows
        {
        for(int j = 0; j < 3; j++) //cycles through zone columns
        {
            int temp = puzzle[z][i][j];
            int newI = rand.nextInt(3);
            int newJ = rand.nextInt(3);
            puzzle[z][i][j] = puzzle[z][newI][newJ];
            puzzle[z][newI][newJ] = temp;
        }
        }
    }
    char[][] display = {{'/','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','\\'},
                {'|',' ',(char)('0' + puzzle[0][0][0]),' ',(char)('0' + puzzle[0][0][1]),' ',(char)('0' + puzzle[0][0][2]),' ','|',' ',(char)('0' + puzzle[1][0][0]),' ',(char)('0' + puzzle[1][0][1]),' ',(char)('0' + puzzle[1][0][2]),' ','|',' ',(char)('0' + puzzle[2][0][0]),' ',(char)('0' + puzzle[2][0][1]),' ',(char)('0' + puzzle[2][0][2]),' ','|'},
                {'|',' ',(char)('0' + puzzle[0][1][0]),' ',(char)('0' + puzzle[0][1][1]),' ',(char)('0' + puzzle[0][1][2]),' ','|',' ',(char)('0' + puzzle[1][1][0]),' ',(char)('0' + puzzle[1][1][1]),' ',(char)('0' + puzzle[1][1][2]),' ','|',' ',(char)('0' + puzzle[2][1][0]),' ',(char)('0' + puzzle[2][1][1]),' ',(char)('0' + puzzle[2][1][2]),' ','|'},
                {'|',' ',(char)('0' + puzzle[0][2][0]),' ',(char)('0' + puzzle[0][2][1]),' ',(char)('0' + puzzle[0][2][2]),' ','|',' ',(char)('0' + puzzle[1][2][0]),' ',(char)('0' + puzzle[1][2][1]),' ',(char)('0' + puzzle[1][2][2]),' ','|',' ',(char)('0' + puzzle[2][2][0]),' ',(char)('0' + puzzle[2][2][1]),' ',(char)('0' + puzzle[2][2][2]),' ','|'},
                {'|','-','-','-','-','-','-','-','+','-','-','-','-','-','-','-','+','-','-','-','-','-','-','-','|'},
                {'|',' ',(char)('0' + puzzle[3][0][0]),' ',(char)('0' + puzzle[3][0][1]),' ',(char)('0' + puzzle[3][0][2]),' ','|',' ',(char)('0' + puzzle[4][0][0]),' ',(char)('0' + puzzle[4][0][1]),' ',(char)('0' + puzzle[4][0][2]),' ','|',' ',(char)('0' + puzzle[5][0][0]),' ',(char)('0' + puzzle[5][0][1]),' ',(char)('0' + puzzle[5][0][2]),' ','|'},
                {'|',' ',(char)('0' + puzzle[3][1][0]),' ',(char)('0' + puzzle[3][1][1]),' ',(char)('0' + puzzle[3][1][2]),' ','|',' ',(char)('0' + puzzle[4][1][0]),' ',(char)('0' + puzzle[4][1][1]),' ',(char)('0' + puzzle[4][1][2]),' ','|',' ',(char)('0' + puzzle[5][1][0]),' ',(char)('0' + puzzle[5][1][1]),' ',(char)('0' + puzzle[5][1][2]),' ','|'},
                {'|',' ',(char)('0' + puzzle[3][2][0]),' ',(char)('0' + puzzle[3][2][1]),' ',(char)('0' + puzzle[3][2][2]),' ','|',' ',(char)('0' + puzzle[4][2][0]),' ',(char)('0' + puzzle[4][2][1]),' ',(char)('0' + puzzle[4][2][2]),' ','|',' ',(char)('0' + puzzle[5][2][0]),' ',(char)('0' + puzzle[5][2][1]),' ',(char)('0' + puzzle[5][2][2]),' ','|'},
                {'|','-','-','-','-','-','-','-','+','-','-','-','-','-','-','-','+','-','-','-','-','-','-','-','|'},
                {'|',' ',(char)('0' + puzzle[6][0][0]),' ',(char)('0' + puzzle[6][0][1]),' ',(char)('0' + puzzle[6][0][2]),' ','|',' ',(char)('0' + puzzle[7][0][0]),' ',(char)('0' + puzzle[7][0][1]),' ',(char)('0' + puzzle[7][0][2]),' ','|',' ',(char)('0' + puzzle[8][0][0]),' ',(char)('0' + puzzle[8][0][1]),' ',(char)('0' + puzzle[8][0][2]),' ','|'},
                {'|',' ',(char)('0' + puzzle[6][1][0]),' ',(char)('0' + puzzle[6][1][1]),' ',(char)('0' + puzzle[6][1][2]),' ','|',' ',(char)('0' + puzzle[7][1][0]),' ',(char)('0' + puzzle[7][1][1]),' ',(char)('0' + puzzle[7][1][2]),' ','|',' ',(char)('0' + puzzle[8][1][0]),' ',(char)('0' + puzzle[8][1][1]),' ',(char)('0' + puzzle[8][1][2]),' ','|'},
                {'|',' ',(char)('0' + puzzle[6][2][0]),' ',(char)('0' + puzzle[6][2][1]),' ',(char)('0' + puzzle[6][2][2]),' ','|',' ',(char)('0' + puzzle[7][2][0]),' ',(char)('0' + puzzle[7][2][1]),' ',(char)('0' + puzzle[7][2][2]),' ','|',' ',(char)('0' + puzzle[8][2][0]),' ',(char)('0' + puzzle[8][2][1]),' ',(char)('0' + puzzle[8][2][2]),' ','|'},
                {'\\','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','/'}};

    for(int i = 0; i < 13; i++)
    {
        for(int j = 0; j < 25; j++)
        {
        System.out.print(display[i][j]);
        }
        System.out.println();
    }
    }
}

输出如下:

$ java SudokuArray
/-----------------------\
| 4 1 2 | 6 2 3 | 1 6 7 |
| 5 6 7 | 9 4 8 | 2 5 3 |
| 9 8 3 | 5 1 7 | 8 4 9 |
|-------+-------+-------|
| 3 4 2 | 5 2 3 | 3 1 5 |
| 7 8 5 | 4 6 1 | 7 4 9 |
| 9 6 1 | 8 9 7 | 6 2 8 |
|-------+-------+-------|
| 5 6 1 | 6 5 8 | 8 1 7 |
| 9 3 8 | 3 9 2 | 5 2 3 |
| 2 4 7 | 4 7 1 | 9 4 6 |
\-----------------------/

因此,您的代码片段可以完成他们的工作(尽管生成的电路板对于Sudoku无效)。 问题出在其他地方。

答案 1 :(得分:0)

此解决方案只需one random valueone loop即可启动益智游戏。

    for(byte r = zero, c; nine > r; r ++) {
        if(three == r || six == r) {
            rand = maze[r - three][zero];
            rand = nine == rand ? one : (byte)(rand + one);
        } else {
            rand = zero < r ? maze[r - one][six] : rand;
        }
        for(c = zero; nine > c; c ++) {
            maze[r][c] = rand;
            rand = nine == rand ? one : (byte)(rand + one);
        }
    }

这是我的github存储库链接: Full code

直播Demo