1

I'm working on a Blackjack simulator. I have the following string array called deck (I prefer to use an array over an enum):

string[] deck = { "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A", };

User will enter the deck number with Input Dialog:

byte deckNumber;
deckNumber = Convert.ToByte(Interaction.InputBox("Enter The Deck Number", "Deck Number", "3", 10, 10));

How can I duplicate the array with deckNumber? For example user enter 2, array will be:

{ "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A","2","3","4","5","6","7","8","9","10","J","Q","K","A" };

I think I need a for loop but I couldn't manage to do it.

10
  • 2
    Actually, a deck is a group of 52 cards.. not just 13 cards... Commented Oct 30, 2014 at 21:11
  • 1
    well writing here 52cards does not make sense :( Commented Oct 30, 2014 at 21:16
  • You're not serious, are you ? do you plan to write a software with only 13 cards out of the 52 ??? then, when you use twice, you don't want the first 3 to be different than the second 3 :??? they are all the same ?? just 3's ??? Commented Oct 30, 2014 at 21:19
  • 1
    @mlwn In fact, There are two colors Black and Red :) Commented Oct 30, 2014 at 21:25
  • 1
    @poster... please consider using the deck as a range 0 to 51... range(52) .. that would help you.. trust me... Commented Oct 30, 2014 at 21:25

3 Answers 3

4
string[] deck = { "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A", };
var newDeck = Enumerable.Repeat(deck, 2).SelectMany(x => x).ToArray();
Sign up to request clarification or add additional context in comments.

Comments

1

If you are using .NET 3.5 or later, you can use LINQ as L.B. has shown:

var newDeck = Enumerable.Repeat(deck, 2).SelectMany(x => x).ToArray();

Enumerable.Repeat(deck, 2) gives you two elements that are each a string[], .SelectMany(x => x) flattens this into one series of strings, and .ToArray() obviously converts the series into an array.

Otherwise, you can use a loop:

var newDeck = new string[deck.Length * deckNumber];

for (int d = 0; d < deckNumber; d++)
{
    for (int i = 0; i < deck.Length; i++)
    {
        newDeck[d * deck.Length + i] = deck[i];
    }
}

Comments

0

var source = new string[]{"a","b"};

var copy = new List(source).ToArray();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.