创建随机颜色网格,所有相邻颜色不同

时间:2013-03-27 17:07:47

标签: java

所以我设置了以下问题:编写一个带有整数命令行参数N的程序,并使用两个嵌套的for循环来打印一个N-by-N板,它在由空格随机分隔的6种颜色之间交替。颜色用字母表示(例如,对于RED,' r'对于BLUE)。您不能在彼此旁边使用两种相同的颜色。

所以,我知道我可能需要数组来解决这个问题。我尝试了几种方法都出错了。以下是我最近的尝试之一,但我不确定如何现在通过网格并纠正它。代码所做的是使每行随机化,没有颜色左右相同,但列不固定。

请注意,我是第一年没有编程历史的CS学生。我猜这个问题的解决方案不是太复杂,但是,我看不出一个简单的解决方案......

    int N = StdIn.readInt();
    int array1[] = new int[N];
    for (int column = 0; column < N; column++) {
        int x = 0;

        for (int row = 0; row < N; row++) {

            int c = (int) (Math.random() * 6 + 1);

            while (x == c) {
                c = (int) (Math.random() * 6 + 1);
                array1[row] = c;
            }
            if (c == 1) {
                System.out.print("R ");
            }
            if (c == 2) {
                System.out.print("O ");
            }
            if (c == 3) {
                System.out.print("Y ");
            }
            if (c == 4) {
                System.out.print("G ");
            }
            if (c == 5) {
                System.out.print("B ");
            }
            if (c == 6) {
                System.out.print("I ");
            }

            x = c;

        }

        System.out.println();

    }
}

1 个答案:

答案 0 :(得分:1)

这是我解决问题的方法。虽然相当复杂,但它背后的逻辑是直截了当的。每次为2D阵列指定新颜色时,只需要在要分配新颜色的位置的顶部和左侧检查数组的值。只有在为阵列的第一行指定颜色后才能执行此操作,因此需要为第一行创建单独的条件。

public class ColourGrid {
    public static void main(String[] args) {
        int N = Integer.parseInt(args[0]);
        char[][] clrGrid = new char[N][N];
        char colours[] = {'r','b','y','w','o','g'} ;
        for (int counter = 0 ; counter < N; counter++) {
            for (int counter2 = 0 ; counter2 < N; counter2++) {
                if (counter == 0 && counter2 == 0) {
                    clrGrid[counter][counter2] = colours[(int)(Math.random()* 5 + 1)] ;
                }
                else if (counter != 0 && counter2 == 0) {
                    clrGrid[counter][counter2] = colours[(int)(Math.random()* 5 + 1)] ;
                    while (clrGrid[counter][counter2] == clrGrid[(counter)-1][counter2]) {
                        clrGrid[counter][counter2] = colours[(int)(Math.random()* 5 + 1)] ;
                    }
                }
                else if (counter == 0 && counter2 != 0) {
                    clrGrid[counter][counter2] = colours[(int)(Math.random()* 5 + 1)] ;
                    while (clrGrid[counter][counter2] == clrGrid[(counter)][counter2-1]) {
                        clrGrid[counter][counter2] = colours[(int)(Math.random()* 5 + 1)] ;
                    }
                }
                else if (counter != 0 && counter2 != 0) {
                    clrGrid[counter][counter2] = colours[(int)(Math.random()* 5 + 1)] ;
                    while (clrGrid[counter][counter2] == clrGrid[(counter)-1][counter2] || clrGrid[counter][counter2] == clrGrid[counter][(counter2)-1]) {
                        clrGrid[counter][counter2] = colours[(int)(Math.random()* 5 + 1)] ;
                    }
                }
                else {
                    clrGrid[counter][counter2] = colours[(int)(Math.random()* 5 + 1)] ;
                }
            }
        }
        for (int counter = 0 ; counter < N; counter++) {
            System.out.println("");
            for (int counter2 = 0 ; counter2 < N; counter2++) {
                System.out.print(clrGrid[counter][counter2] + " ");
            }
        }
    }
}