An issue in designing a Card Trick Game using C# -


i want create card trick game using c#. i've designed picture boxes on form cards (backside). created click method each of pictures creates random number between 0, 51 , use number set image imagelist.

        random random = new random();         int = random.next(0, 51);         picturebox1.image = imagelist1.images[i]; 

my problem same numbers (example: 2 jacks of spades), how can prevent that?! (i mean if, example, got (5), may (5))

store numbers have selected in hashset<int> , continue selecting until current nunber not in hashset:

// private hashset<int> seen = new hashset<int>(); // private random random = new random();   if (seen.count == imagelist1.images.count) {     // no cards left... }  int card = random.next(0, imagelist1.images.count); while (!seen.add(card)) {     card = random.next(0, imagelist1.images.count); }  picturebox1.image = imagelist1.images[card]; 

or, if need select many numbers, can fill array sequential numbers , swap number in each index number random index. take top n needed items randomized array.


Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

javascript - jQuery .height() return 0 when visible but non-0 when hidden -