0

Hello Working on a poker game. I have my cards randomly being called from a file, but I want there to be no duplicates. for example, no 2 five of clubs, or 2 jack of spades in the same hand. That's basically what I have been trying to do, and once I get that done, my game should be finished Here is some of the code

string[] CardDisplay = new string[5];

    for (int i = 0; i < 5; i++)
    {
        CardDisplay[i] = getRandomImage();
    }

   PokerCard[0] = PokerCard1.ImageUrl = Path.Combine("~/GameStyles/VideoPoker/Images/Poker/", CardDisplay[0]);
   PokerCard[1] = PokerCard2.ImageUrl = Path.Combine("~/GameStyles/VideoPoker/Images/Poker/", CardDisplay[1]);
   PokerCard[2] = PokerCard3.ImageUrl = Path.Combine("~/GameStyles/VideoPoker/Images/Poker/", CardDisplay[2]);
   PokerCard[3] = PokerCard4.ImageUrl = Path.Combine("~/GameStyles/VideoPoker/Images/Poker/", CardDisplay[3]);
   PokerCard[4] = PokerCard5.ImageUrl = Path.Combine("~/GameStyles/VideoPoker/Images/Poker/", CardDisplay[4]);


public string getRandomImage()
{

    string[] fileNames = Directory.GetFiles(MapPath("~/GameStyles/VideoPoker/Images/Poker/"));
    int CurrentPick;

    CurrentPick = rand.Next(fileNames.Length);

    string CardToShow = fileNames[CurrentPick];
    return Path.GetFileName(CardToShow);
}

Here is a screenshot of what I have

enter image description here

Blue
  • 22,608
  • 7
  • 62
  • 92
  • 1
    You could keep track of which cards you've already pulled and re-select a random number if one of those comes up. – itsme86 Oct 11 '16 at 18:30
  • 1
    "Generating random numbers without duplicates" already asked many times. Could be duplicate of http://stackoverflow.com/questions/273313/randomize-a-listt... – Alexei Levenkov Oct 11 '16 at 18:47
  • 1
    Why not follow the *original procedure*: load all images into a collection (say, array) - *pack*; then *shuffle* the collection; finally, *deal* - take first 5 cards? If 6th card is required, take the 6th array's item. – Dmitry Bychenko Oct 11 '16 at 19:51

1 Answers1

1

I recommend using LINQ to accomplish this:

string[] fileNames = Directory.GetFiles(MapPath("~/GameStyles/VideoPoker/Images/Poker/"));

var randomCards = fileNames
    .OrderBy(i => Guid.NewGuid())
    .Take(5)
    .Select(filePath => Path.Combine("~/GameStyles/VideoPoker/Images/Poker/", Path.GetFileName(filePath)))
    .ToArray();

PokerCard[0] = PokerCard1.ImageUrl = randomCards[0];
PokerCard[1] = PokerCard2.ImageUrl = randomCards[1];
PokerCard[2] = PokerCard3.ImageUrl = randomCards[2];
PokerCard[3] = PokerCard4.ImageUrl = randomCards[3];
PokerCard[4] = PokerCard5.ImageUrl = randomCards[4];

This will simply reorder the array of fileNames, sort by a random Guid (Which will essentially randomize the array), and then take the first 5 elements.

Here's a .NET fiddle showing how it will work: https://dotnetfiddle.net/c1996q

Blue
  • 22,608
  • 7
  • 62
  • 92
  • Sure. This is traditionally bad approach to shuffle array and you did not provide any explanation why it is superior to standard shuffle. Also this "C# shuffle" asked and answered plenty of times and your post does not add much to http://stackoverflow.com/questions/273313/randomize-a-listt (or many similar posts) - but that is not the reason for downvote... – Alexei Levenkov Oct 11 '16 at 18:45
  • Hey thanks a lot for your reply. When I use that method, then none of the cards are visible –  Oct 11 '16 at 18:47
  • 1
    Moving him down the path of sorting the array, instead of picking cards like he is, is one solution to his problem. The second answer you linked me appears to be the method I used in here, and this question was tailored to his answer. I disagree you should be using your downvotes in that manner. Also: He did not ask why this is superior to a standard shuffle, so there was no reason to add anything like that to the question. Downvoting signifies a post isn't helpful, and me providing a working example, goes against the spirit of what a downvote should be used for @AlexeiLevenkov – Blue Oct 11 '16 at 18:49
  • @StephenBrown You would still need to set the PokerCard array below. – Blue Oct 11 '16 at 18:49
  • Can you please show me an example of what you mean. thanks –  Oct 11 '16 at 19:21
  • Well I am not sure where I am going wrong, but my code was changed and looked just like your modifications, but every time I hit deal my cards dissapear. I mean I understand what you are doing, just not sure why it is not working correctly. Thanks –  Oct 11 '16 at 20:12
  • @Frankerz what about using someRandom.Next() instead of Guid.NewGuid? – Gian Paolo Oct 11 '16 at 20:55
  • What more information do you need. Im just not sure why when I use this method the cards dissapear –  Oct 11 '16 at 20:55
  • @AlexeiLevenkov I actually would suggest this or a similar method: why "This is traditionally bad approach to shuffle array"? and what is the "standard shuffle" you are referring to? No polemic, I just want to know why my (and Stephen Brown) approach it's not "the good one". – Gian Paolo Oct 11 '16 at 21:01
  • @GianPaolo sorting is O(n log n) plus creation of a lot of unrelated objects around sorting, getting fixed number of items from such sequence is indeed O(1)... but with Fisher-Yates shuffle you don't need to pay any sorting cost and can just get your fixed number of entries for O(1) if you don't need to shuffle all n cards. Even if you need to shuffle all Fisher-Yates shuffle is still O(n) and no extra allocations instead of O(n log n) for sorting. – Alexei Levenkov Oct 11 '16 at 21:15
  • I tried the random.next(), and it just gave me the same outcome as before –  Oct 11 '16 at 21:16
  • @AlexeiLevenkov I understand your concerns about better performance with other (Fisher-Yates ) algorithm, but really, is it really a problem for shuffling a 52 card deck??? for such scenario, I prefer use the one liner suggested by Frankerz instead of writing my algorithm. My executable can take some picoseconds more to shuffle my deck, but I don't have the risk to write my òwn algorithm code and maybe insert a bug in it. – Gian Paolo Oct 11 '16 at 21:37
  • @GianPaolo It's also why it was the second highest answer, with 147 upvotes. I again: Believe my answer was helpful, and the downvote goes against the spirit of the site. – Blue Oct 11 '16 at 21:38
  • @StephenBrown I didn't realize that your `getRandomImage` only returned the filename, and not the full path. My code returns the full path. Try the updated snippet I posted above. – Blue Oct 11 '16 at 21:41
  • @FrankerZ your answer is obviously not helpful to OP for whatever reason... And not helpful to the site too as this type of questions is already answered many times, including many "draw cards" solutions with complete code. I've removed downvote to make you happier so... (and prevent OP from deleting the question that shows no research :) ) – Alexei Levenkov Oct 11 '16 at 21:52
  • @AlexeiLevenkov I doubled up on the path names. Was my bad for not seeing the last line of his `getRandomImage` function. – Blue Oct 11 '16 at 21:53
  • @StephenBrown Has the updated snippet worked for you? – Blue Oct 13 '16 at 13:46
  • Hey thanks for getting back to me, but unfortunately when I use the updated snippet my cards disappear when I press deal, and I have been messing around with it. Can't figure out why. Its wierd –  Oct 14 '16 at 17:26