John Smith
John Smith

Reputation: 97

Random Swap position of 16 buttons

I have to make a find dublicate game sample for school, I have 16 buttons with each2 buttons have same image. But the problem is, I want to randomely set position of that 16 buttons when I opened the game. Anyone help me please!

Mycode is

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]);

Upvotes: 1

Views: 108

Answers (2)

Sparkofska
Sparkofska

Reputation: 1320

In my opinion, the most simple way is like follows, using Lists and the Collections.shuffle() method:

// 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);
}

Using the Collections.shuffle(list) method randomizes the order of the pictures. Doing so lets you setBackgroundResouces sequentially.

In this way you will have all the button instances collected in a List. This enables you to use loops instead of manual listing of all buttons. If you want to acces a single button later on, use the index access buttons.get(index).

Upvotes: 1

Susmit Agrawal
Susmit Agrawal

Reputation: 3764

Sample code, for your problem:

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;
}

The randomizedList method takes in the array of image resource values as its parameter. It creates a list out of the array, with two copies of every array element in the list.

It then randomly takes an index, puts the element at that index into randomList, and removes it from the original list.

Upvotes: 0

Related Questions