从6种颜色中生成4种颜色,并将它们分配给4个按钮

时间:2019-05-23 16:50:43

标签: java

我是新来的...如果您很友善,我需要一点帮助。我正在用Android(策划者)做游戏。我有6种不同的颜色,我需要生成一个随机组合,该组合将生成4种颜色(不重复)并将它们分配给4个按钮。我的代码生成4种颜色,无需重复。到目前为止,一切都很好。我在“ if”中遇到了问题,它为2个按钮分配了2种不同的颜色(imageButtonAdivinar3和imageButtonAdivinar4),因此,例如,如果buttonAdiviar1内部存在相同的颜色,则不要填充它。我最好放上代码,因为我不知道如何解释自己...

    public int obtenerCombinacionAleatoria() {

    ArrayList<Integer> listaNumeros = new ArrayList();
    int totalNumerosAgenerar = 0;
    int numero = 0;

    while (totalNumbersToGenerate != 4) {
        number = ((int) (Math.random() * 6) + 1);
        if (!listNumbers.contains(number)) {
            totalNumbersToGenerate++;
            listNumbers.add(number);

        }
        if (number == 1) {
            imageButton1.setImageResource(R.drawable.yellow);
        }
        if (number == 2) {
            imageButton2.setImageResource(R.drawable.blue);
        }
        if (number == 3) {
            imageButton3.setImageResource(R.drawable.purple);
        }
        if (number == 4) {
            imageButton4.setImageResource(R.drawable.red);
        }
        if (number == 5) {
            imageButton3.setImageResource(R.drawable.pink);
        }
        if (number == 6) {
            imageButton1.setImageResource(R.drawable.green);
        }
    }
    return 0;

}

In the image, the gray button is not filled. This is because, the number 3 and the number 5, which are assigned to the same button, have entered the description of the image here

0 个答案:

没有答案