jeudi 1 juillet 2021

Random next activity button

I am really new to coding. So If I explain bad or do dumb mistakes just tell me, so I can get better.

Anyways. I am creating a game and my intention is to make a button open a random activity. Like this: activity1 -> activity5 -> activity4 ->activity 18 and so on. Without opening the same activity that has already been played. But now my issue is that I have 20 activities, but sometimes it only appears 12 before my app says "Game Over". How can I make sure its always gonna load max activites? thanx.

My code works like this:

Mainactivity.class

     int r = new Random().nextInt(activitylist.size());
            Class activity = activitylist.remove(r);

            switch (r) {
                case 1:
                    activity = defransman.class;
                    activitylist.remove(defransman.class);
                    break;
                case 2:
                    activity = crazycaps.class;
                    activitylist.remove(crazycaps.class);
                    break;
                case 3:
                    activity = max.class;
                    activitylist.remove(max.class);
                    break;
                case 4:
                    activity = michael.class;
                    activitylist.remove(michael.class);
                    break;
                case 5:
                    activity = youngest.class;
                    activitylist.remove(youngest.class);
                    break;
                case 6:
                    activity = driverlincense.class;
                    activitylist.remove(driverlincense.class);
                    break;

            }

            Intent startgame = new Intent(getBaseContext(), activity);
            startgame.putExtra("ACTIVITY_LIST", activitylist);
            startActivity(startgame);

        }
    });
}

}

Activity2.class:

public class bestclothes extends AppCompatActivity {

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

    Button nextpage = findViewById(R.id.BTBesteKleren);
    nextpage.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            ArrayList<Class> aktivitet = new ArrayList<>();
            Bundle extras = getIntent().getExtras();
            aktivitet = (ArrayList<Class>) extras.get("ACTIVITY_LIST");

            if (aktivitet.size() ==0) {
                Toast.makeText(bestclothes.this, "Game over", Toast.LENGTH_SHORT).show();
            } else {
                int r = new Random().nextInt(aktivitet.size());
                Class activity = aktivitet.remove(r);

                switch (r) {
                    case 1:
                        activity = aktivitet.get(0);
                        aktivitet.remove(0);
                        break;
                    case 2:
                        activity = aktivitet.get(1);
                        aktivitet.remove(1);
                        break;
                    case  3:
                        activity = aktivitet.get(2);
                        aktivitet.remove(2);
                        break;
                    case 4:
                        activity = aktivitet.get(3);
                        aktivitet.remove(3);
                        break;
                    case 5:
                        activity = aktivitet.get(4);
                        aktivitet.remove(4);
                        break;
                    case 6:
                        activity = aktivitet.get(5);
                        aktivitet.remove(5);
                        break;  
}  
Intent intent = new Intent(getBaseContext(), activity);
                intent.putExtra("ACTIVITY_LIST", aktivitet);
                startActivity(intent);

and so on. I




Aucun commentaire:

Enregistrer un commentaire