0

I am making this card game app, when the user clicks the card this image will turn into a random other card. Here is an example:

public class MainActivity extends Activity {

int[] cards={R.drawable.aceofspades,R.drawable.aceofhearts,R.drawable.aceofclubs};
static Random r = new Random();


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

}


public void imageClick(View view) { 

      int n=r.nextInt(cards.length);

    {
    ImageView image = (ImageView) findViewById(R.id.imageDice1);
    image.setImageResource(cards[n]);
    }


 } 

}

I would like to alter the code so that it won't be possible that the same card will show twice, not untill all the cards passed. So in this case when the card is "aceofspades" the next card and the card after that should not be "aceofspades". The first image that users will see is "R.drawable.cardback". I would like a code that changes the image back to "cardback" when all cards have been shown. Any help is appreciated.

Siebrand Romeyn
  • 152
  • 1
  • 9
  • Just to commment that you should really keep the image resources in an array so you dont have to type out 52 if statements. Secondly you should use a if else statement as your program will continue to check counter even once its found a match – CarbonAssassin Apr 06 '14 at 19:28

2 Answers2

1

You need to put the cards (or just the values 1 to 52) in an array or a list, then shuffle them. The requirement you're describing is exactly what shuffling does.

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
0

See this answer to get unique random numbers in Java

You need basically the same structure and process.

Community
  • 1
  • 1
JAC
  • 287
  • 1
  • 10