如何从t [] []中的a []中随机地对int进行排序,以使形成a []的所有int彼此相邻?

时间:2018-06-24 20:38:48

标签: java arrays multidimensional-array random while-loop

我的问题用言语描述有点复杂,所以我将从现有知识开始。以下代码将a []中的所有1随机地移到t [] []中-每个1覆盖找到的0:

import java.util.Arrays;
import java.util.Random;

public class test {

    public static void main (String[] args) {


        Random r = new Random();

        int a[]  = {1,1,1,1,1,1,1,1,1,1};

        int t[][] = {{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, //12 * 12, to avoid
                {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},      //out-of-bounds ex;
                {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},     //first and last 
                {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},     //lines and rows  
                {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},     //always stay 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}};


        int line = r.nextInt(10)+1;  //1 - 11 to avoid out of bounds ex
        int column = r.nextInt(10)+1;
        t[line][column] = a[0]; //set first 1 in t[]

        //move line and row away from the first one
        //to avoid out-of-bounds ex in while loop below
        //(I have to think about if this is really necessary,
        //but let's not focus on this ;)
        if (line <=5) {
            line = (line+2);
        }
        else {
            line =(line-2);
        }

        if (column <=5) {
            column = (column+2);
        } else {
            column =(column-2);
        }

        //begin moving 1s
        for(int i = 1; i < a.length; i++) { //i=1, because first 1 is 
                                            //already set

            //the field has to be a new one, so it does not overwrite
            //an existing 1.

            while(   (t[line][column]) !=0) { 
                line = r.nextInt(10)+1; 
                column = r.nextInt(10)+1; 

            }
            t[line][column] = a[i];

        }

        for (int j = 0; j < t.length; j++) {
            System.out.println(Arrays.toString(t[j]));
        }
    }
}

但是现在我想成为彼此相邻的人,例如:

        {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, 1, 1, 1, 0, 0, 0},
        {0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0},
        {0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0},
        {0, 0, 0, 0, 0, 0, 1, 1, 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};

我的目标是使它们看起来像一个迷宫(对于迷你迷宫探索器游戏。我不在乎结果是否看起来像我现在迷宫一样,但是我想了解出了什么问题) 。我试图像这样修改while循环:

//while the int at the field is not zero
        while(   (t[line][column]) !=0 &&

                //and all neighbours are 0,         
                ((t[line+1][column]) == 0 && //above
                (t[line-1][column]) == 0 &&  //below
                (t[line][column-1]) == 0 &&  //left
                (t[line][column+1]) == 0))  //right

        { 
            //generate new random field.
            line = r.nextInt(10)+1; 
            column = r.nextInt(10)+1; 

        }

它不起作用,我不确定为什么。这个想法对我来说很有意义,我也不知道代码哪里出了问题。我也是一个初学者,所以虽然可能有更好的方法来执行类似的操作,但由于我的代码无法正常工作的原因,我希望得到详细的帮助:-)

非常感谢您!

1 个答案:

答案 0 :(得分:0)

您的代码有更改

import java.util.Arrays;
import java.util.Random;

public class Game {
    private final static int DIMENSTION = 10;

    public static void main(String[] args) {
        final Random r = new Random();
        final int field[][] = new int[10][10];

        for(int line=0; line < DIMENSTION; line ++) {
            for(int column=0; column < DIMENSTION; column ++) {
                field[line][column] = 0;
            }
        }

        //begin moving 1s
        for(int i = 0; i < 8; i++) {
            int line = r.nextInt(DIMENSTION);
            int column = r.nextInt(DIMENSTION);

            while(field[line][column] == 0) {
                field[line][column] = 1;

                int direction = r.nextInt(4);
                switch (direction){
                    case 0:
                        if (line < DIMENSTION -1) {
                            line ++;
                        }
                        break;
                    case 1:
                        if (line >= 1) {
                            line --;
                        }
                        break;
                    case 2:
                        if (column >= 1) {
                            column --;
                        }
                        break;
                    case 3:
                        if (column < DIMENSTION - 1) {
                            column ++;
                        }
                        break;
                    default:
                        throw new UnsupportedOperationException("Can move wrong direction " + direction);
                }
            }
        }

        for (int j = 0; j < field.length; j++) {
            System.out.println(Arrays.toString(field[j]));
        }
    }

}

结果

[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 1, 1, 0, 0, 0, 0, 0, 0]
[0, 0, 1, 1, 1, 1, 1, 0, 0, 0]
[0, 0, 0, 1, 1, 0, 1, 0, 0, 0]
[0, 0, 0, 1, 1, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 1, 0, 0, 1, 1, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 1]
[0, 0, 0, 0, 0, 0, 1, 1, 1, 1]
[0, 1, 1, 1, 1, 0, 1, 0, 0, 0]
[0, 1, 1, 0, 0, 0, 0, 0, 0, 0]

也不要使用更大的数组来避免超出范围