Jump to content

shuffle cards probem

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
4 replies to this topic

#1
jeremy1984

jeremy1984

    Newbie

  • Members
  • Pip
  • 3 posts
i'm doing a card game, and i'm having error with my shuffling routine
it's not working probably ,, what should i do?

this is my shuffling routine, would you plz tell me what is the problem with my code...
 public void shuffle()
                      {
    
                       List<card> shufCards=new List<card>(52);
                       Random rand=new Random();
                       for (int i = 0; i < cards.Count; i++)
                         {
                           int c = rand.Next(cards.Count); 
                           shufCards.Add(cards[c]);
                           cards.RemoveAt(c);
                         }
                                              
                        cards = shufCards;
                       }

Edited by Jaan, 15 November 2009 - 12:14 PM.
Please use code tags when you are posting your codes !


#2
bennis

bennis

    Newbie

  • Members
  • Pip
  • 1 posts
As cards.count is changing according to cards.removeAt(),
so, it should be

int temp=cards.count;

for(int i=0;i<temp;i++){

}



#3
Orjan

Orjan

    Writes binary right handed and hex left handed

  • Moderators
  • 3,298 posts
why shuffle the deck? why not just randomize one at a time from the deck?
__________________________________________
I study Information Systems at Karlstad University when I'm not on CodeCall

#4
FlashM

FlashM

    Learning Programmer

  • Members
  • PipPipPip
  • 90 posts
I think this depends on the card game... Is there only one deck? If true, Orjan has the best solution for now. Just randomize one at a time from the deck.

#5
Orjan

Orjan

    Writes binary right handed and hex left handed

  • Moderators
  • 3,298 posts
That method works with as many decks as well, just having an array to check off used cards in... and of course, you randomizes all decks then...
__________________________________________
I study Information Systems at Karlstad University when I'm not on CodeCall