This question has already been answered:
It is necessary to mix the array.
Program class:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Poker { class Program { static void Main() { Deck d = new Deck(); d.PrintCards(); Console.ReadKey(); } } } Deck class:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Poker { class Deck { string[] ranks = new string[13]; string[] suits = new string[4]; public Card[] cards = new Card[52]; public Deck() { for (int a = 0; a < 13; a++) { for (int b = 0; b < 4; b++) { cards[b * 13 + a] = new Card(); cards[b * 13 + a].Rank = a; cards[b * 13 + a].Suit = b; } } ranks[0] = "2"; ranks[1] = "3"; ranks[2] = "4"; ranks[3] = "5"; ranks[4] = "6"; ranks[5] = "7"; ranks[6] = "8"; ranks[7] = "9"; ranks[8] = "10"; ranks[9] = "J"; ranks[10] = "Q"; ranks[11] = "K"; ranks[12] = "A"; suits[0] = "c"; suits[1] = "h"; suits[2] = "s"; suits[3] = "d"; } public void ShuffleDeck() { int x; Card tempcard; for (int a = 0; a < 52; a++) { Random rand = new Random(); x = rand.Next(1, 100); tempcard = cards[a]; cards[a] = cards[x]; cards[x] = tempcard; } } public void PrintCards() { for (int a = 0; a < 52; a++) { Console.Write(ranks[cards[a].Rank] + suits[cards[a].Suit] + " "); } } } } In the ShuffleDeck method ShuffleDeck should be a shuffle of the deck, but it does not work.
ArrayIndexOutOfBoundsExceptiondue to the fact thatxtakes values ​​from 1 to 100, whilecardsonly 52 elements. - Regent4,13and52scattered around the code. Yes, it is unlikely that once you will have not 4 suits with 13 cards in each, but nobody canceled a good programming style. - Regent