16个按钮的随机交换位置

时间:2018-08-20 11:57:27

标签: java android

我必须为学校找到一个双面游戏样本,我有16个按钮,每个按钮具有相同的图像。但是问题是,我想在打开游戏时随机设置这16个按钮的位置。 任何人都可以帮助我!

Mycode是

ImageButton bt1, bt2, bt3, bt4, bt5, bt6, bt7, bt8, bt9, bt10, bt11, bt12, bt13, bt14, bt15, bt16;

    int arraypic[]={R.drawable.elephant,R.drawable.giraffe,R.drawable.hog,R.drawable.lion,R.drawable.monkey,R.drawable.panda,R.drawable.pig,R.drawable.tiger};
    int button []= {R.id.bt1, R.id.bt2,R.id. bt3,R.id. bt4,R.id.bt5, R.id.bt6, R.id.bt7,R.id.bt8,R.id. bt9, R.id.bt10, R.id.bt11,R.id. bt12,R.id. bt13,R.id. bt14, R.id.bt15,R.id. bt16};
    Random random = new Random();
    int rr= random.nextInt(button.length);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ingame);
        bt1 = findViewById(R.id.bt1);
        bt2 = findViewById(R.id.bt2);
        bt3 = findViewById(R.id.bt3);
        bt4 = findViewById(R.id.bt4);
        bt5 = findViewById(R.id.bt5);
        bt6 = findViewById(R.id.bt6);
        bt7 = findViewById(R.id.bt7);
        bt8 = findViewById(R.id.bt8);
        bt9 = findViewById(R.id.bt9);
        bt10 = findViewById(R.id.bt10);
        bt11 = findViewById(R.id.bt11);
        bt12 = findViewById(R.id.bt12);
        bt13 = findViewById(R.id.bt13);
        bt14 = findViewById(R.id.bt14);
        bt15 = findViewById(R.id.bt15);
        bt16 = findViewById(R.id.bt16);

        bt1.setBackgroundResource(arraypic[1]);
        bt2.setBackgroundResource(arraypic[1]);
        bt3.setBackgroundResource(arraypic[2]);
        bt4.setBackgroundResource(arraypic[2]);
        bt5.setBackgroundResource(arraypic[3]);
        bt6.setBackgroundResource(arraypic[3]);
        bt7.setBackgroundResource(arraypic[4]);
        bt8.setBackgroundResource(arraypic[4]);
        bt9.setBackgroundResource(arraypic[5]);
        bt10.setBackgroundResource(arraypic[5]);
        bt11.setBackgroundResource(arraypic[6]);
        bt12.setBackgroundResource(arraypic[6]);
        bt13.setBackgroundResource(arraypic[7]);
        bt14.setBackgroundResource(arraypic[7]);
        bt15.setBackgroundResource(arraypic[0]);
        bt16.setBackgroundResource(arraypic[0]);

2 个答案:

答案 0 :(得分:1)

我认为,最简单的方法如下,使用ListsCollections.shuffle()方法:

// collect your resources in Lists
List<Integer> pics = Arrays.asList(R.drawable.elephant, R.drawable.giraffe, R.drawable.hog, R.drawable.lion, R.drawable.monkey, R.drawable.panda, R.drawable.pig, R.drawable.tiger);
List<Integer> buttonResources = Arrays.asList(R.id.bt1, R.id.bt2, R.id.bt3, R.id.bt4, R.id.bt5, R.id.bt6, R.id.bt7, R.id.bt8, R.id.bt9, R.id.bt10, R.id.bt11, R.id.bt12, R.id.bt13,R.id.bt14, R.id.bt15, R.id.bt16);
List<ImageButton> buttons = new ArrayList<ImageButton>();

// randomize order of the pictures
Collections.shuffle(pics);

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_ingame);

    for(int i = 0; i < buttonResources.size(); i++)
    {
        ImageButton bt = findViewById(buttonResources.get(i));
        buttons.add(bt);

        bt.setBackgroundResource(pics.get(i/2)); // hack: integer division, works because the number of pictures is half the number of buttons
    }

    // access the buttons later like this:
    bt1 = buttons.get(0);
}

使用Collections.shuffle(list)方法随机化图片的顺序。这样可以让您顺序设置BackgroundResouces。

通过这种方式,您将在List中收集所有按钮实例。这使您可以使用循环而不是手动列出所有按钮。如果以后要访问单个按钮,请使用索引访问权限buttons.get(index)

答案 1 :(得分:0)

示例代码,针对您的问题:

Integer arraypic[]={R.drawable.elephant,R.drawable.giraffe,R.drawable.hog,R.drawable.lion,R.drawable.monkey,R.drawable.panda,R.drawable.pig,R.drawable.tiger};
int button []= {R.id.bt1, R.id.bt2,R.id. bt3,R.id. bt4,R.id.bt5, R.id.bt6, R.id.bt7,R.id.bt8,R.id. bt9, R.id.bt10, R.id.bt11,R.id. bt12,R.id. bt13,R.id. bt14, R.id.bt15,R.id. bt16};
List<Integer> randomList = randomizedList(arraypic);

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_ingame);
    bt1 = findViewById(R.id.bt1);
    bt2 = findViewById(R.id.bt2);
    bt3 = findViewById(R.id.bt3);
    bt4 = findViewById(R.id.bt4);
    bt5 = findViewById(R.id.bt5);
    bt6 = findViewById(R.id.bt6);
    bt7 = findViewById(R.id.bt7);
    bt8 = findViewById(R.id.bt8);
    bt9 = findViewById(R.id.bt9);
    bt10 = findViewById(R.id.bt10);
    bt11 = findViewById(R.id.bt11);
    bt12 = findViewById(R.id.bt12);
    bt13 = findViewById(R.id.bt13);
    bt14 = findViewById(R.id.bt14);
    bt15 = findViewById(R.id.bt15);
    bt16 = findViewById(R.id.bt16);

    bt1.setBackgroundResource(randomList.get(0));
    bt2.setBackgroundResource(ramdomList.get(1));
    ....
    bt16.setBackgroundResource(randomList.get(31));
}

public List<Integer> randomizedList(Integer []objs) {
    List<Integer> initial = new ArrayList<>(Arrays.asList(objs));
    initial.addAll(Arrays.asList(objs));
    List<Integer> randomList = new ArrayList<>();
    Random rand = new Random ();
    for(int i=0; i<objs.length;i++) {
        int r = rand.nextInt(initial.size());
        randomList.add(initial.get(r));
        initial.remove(r);
    }
    return randomList;
}

randomizedList方法将图像资源值的数组作为其参数。它会在数组外创建一个列表,列表中的每个数组元素都有两个副本。

然后它随机获取一个索引,将该索引处的元素放入randomList中,并将其从原始列表中删除。