![Screebshot][1]]
Is there anyway I can have a different number generate as x, between 0 and 3, every time i call the gamePlay()
method below? The gameplay
method is on a loop and I want it so that every time the method is called a new value for x between 0 and 3 is created. I tried a lot of things such as the random generator in the code below but I cant figure it out.
public class MainActivity extends AppCompatActivity {
final Handler h = new Handler();
RelativeLayout rLayout;
Button play, retry;
TextView title, lose, score;
ImageView rc, gc, bc, yc, tyc, trc, tgc, tbc,tblc;
int gscore = 0;
protected void gamePlay(){
x = r.nextInt(4);
if(x == 0){
tbc.setVisibility(View.VISIBLE);
}
else if(x == 1){
tgc.setVisibility(View.VISIBLE);
}
else if(x == 2){
trc.setVisibility(View.VISIBLE);
}
else if(x == 3){
tyc.setVisibility(View.VISIBLE);
}
rLayout.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if(x == 0){
if(tbc.getY() < bc.getY()+50 && tbc.getY() > bc.getY()-50){
gscore = gscore+1;
score.setText(String.valueOf(gscore +""));
tgc.setY(tblc.getY());
tbc.setY(tblc.getY());
trc.setY(tblc.getY());
tyc.setY(tblc.getY());
}
else{
gameLost();
}
}
else if(x==1)
if(tbc.getY() < gc.getY()+50 && tbc.getY() > gc.getY()-50){
gscore = gscore+1;
score.setText(String.valueOf(gscore+"" ));
tgc.setY(tblc.getY());
tbc.setY(tblc.getY());
trc.setY(tblc.getY());
tyc.setY(tblc.getY());
}
else{
gameLost();
}
else if(x==2)
if(tbc.getY() < rc.getY()+50 && tbc.getY() > rc.getY()-50){
gscore = gscore+1;
score.setText(String.valueOf(gscore +""));
tgc.setY(tblc.getY());
tbc.setY(tblc.getY());
trc.setY(tblc.getY());
tyc.setY(tblc.getY());
}
else{
gameLost();
}
else if(x==3)
if(tbc.getY() < yc.getY()+50 && tbc.getY() > yc.getY()-50){
gscore = gscore+1;
score.setText(String.valueOf(gscore +""));
tgc.setY(tblc.getY());
tbc.setY(tblc.getY());
trc.setY(tblc.getY());
tyc.setY(tblc.getY());
}
else{
gameLost();
}
}
});
}