vendredi 4 novembre 2022

How can I compare three imageViews inside array to see and do something when all three matches?

Iam pretty new to Java and programming so need help. I want to make some fruit game when three same pictures lineup program will print "Jackpot".

For now RNG roll on click works, but need help how to check when all three pictures or (index) are the same in all ImageViews.

Thank you very much.

public class MainActivity extends AppCompatActivity {

    ImageView s1,s2,s3;
    TextView broj;
    Button roll;
    int images[]={R.drawable.banana,R.drawable.cherry,R.drawable.blackberry};
    int br=0;
    int i=0;
    int j=0;
    boolean flag;

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

        s1=findViewById(R.id.iv1);
        s2=findViewById(R.id.iv2);
        s3=findViewById(R.id.iv3);


        broj=findViewById(R.id.brojac);
        roll=findViewById(R.id.btnRoll);
        flag=true;

        roll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                getRandomImage();
                jackpot();
            }
        });
    }
    public void getRandomImage() {

        Random rng = new Random();
        s1.setImageResource(images[rng.nextInt(images.length)]);
        s2.setImageResource(images[rng.nextInt(images.length)]);
        s3.setImageResource(images[rng.nextInt(images.length)]);
        i++;
        if (i == 3) i = 0;

    }
    public void brojac()
    {

        br++;
        broj.setText(String.valueOf(br));


    }
    public void jackpot()
    {

    }
}

I don't know how to approach problem because lack of knowledge




Aucun commentaire:

Enregistrer un commentaire