Playing cards: Difference between revisions
Content added Content deleted
Line 347: | Line 347: | ||
public void Shuffle() { |
public void Shuffle() { |
||
// using Knuth Shuffle (see at http://rosettacode.org/wiki/Knuth_shuffle) |
|||
// HACK: Sort with a custom comparator. (A random number between -1 and 1.) |
|||
Random |
Random random = new System.Random(); |
||
Card temp; |
|||
int j; |
|||
this.deck.Sort(delegate { return r.Next(-1, 1); }); |
|||
for (int i = 0; i < deck.Count; i++){ |
|||
⚫ | |||
j = random.Next(deck.Count); |
|||
catch { |
|||
temp = deck[i]; |
|||
deck[i] = deck[j]; |
|||
deck[j] = temp; |
|||
⚫ | |||
} |
} |
||
public Card Deal() { |
public Card Deal() { |
||
this.Shuffle(); |
|||
Card r = this.deck[0]; |
Card r = this.deck[0]; |
||
this.deck.RemoveAt(0); |
this.deck.RemoveAt(0); |