Skip to main content
gamedev.net
🔒 Locked

c# help - multi dimensional array problem

Started by c_helmin Feb 21, 2009 at 3:05 PM 3 replies 700+ views
Original Post
c_helmin
c_helmin
So I just started messing with arrays that have more than one index. i declared this string [,] cards = new string [5,2]; I have a loop that sets 5 cards : for (int i = 0; i < 5; i++) cards[i,1] = Cards.GetCards(randNum(0,13)); This part works fine, but when I try to use something similar to set the suits of the cards, it fails before it sets a single suit. for (int = 0; i < 5; i++) cards[i,2] = Cards.GetSuits(randNum(0,4)); Says outside of array range, and as I said, this is my first time dabbling with these kinds of arrays. How should I be inputing data into its second set?
SiCrane
SiCrane
Valid values for your second array index are 0 and 1, not 2. Indices are zero based for all dimensions.
ernow
ernow
Uhm...
for (int i = 0; i < 5; i++){  cards[i,2] = Cards.GetCards(randNum(0,13));}


But you might want to use a different approach:
put all the cards in a single dimension array and then shuffle.
Shuffle by looping through the array and swapping each card with a randomly picked other card.

That way you do not have to keep track of what card you've already generated.
c_helmin
c_helmin
Ok, I'm a dummy.

My mind slipped on the fact that a [5,2] array would hold data in [0-4, 0-1]

Disregard my stupidity :/
c_helmin
c_helmin
thank you both, I figured out my initial problem right after I posted, and ernow, that idea's brilliant :)

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.