1

I am writing a card playing program as a way of learning C#.

I ran into an issue with an array going out of bounds.

Here is my code:

namespace Pcardconsole
{
    class Deck
    {
        public Deck()
        {
            // Assign standard deck to new deck object
            int j;
            for (int i = 0; i != CurrentDeck.Length; i++)
            {
                CurrentDeck[i] = originalCards[i];
            }
            // Fisher-Yates Shuffling Algorithim
            Random rnd = new Random();

            for (int k = CurrentDeck.Length - 1; k >= 0; k++)
            {
                int r = rnd.Next(0, k + 1);

                int tmp = CurrentDeck[k];
                CurrentDeck[k] = CurrentDeck[r];
                CurrentDeck[r] = tmp;
            //    Console.WriteLine(i);
            }
        }

        public void Shuffle()
        {
            // TODO
        }
     //   public int[] ReturnCards()
      //  {
         // TODO
      //  }

        public int[] originalCards = new int[54]
        {
            0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C,

            0x1D, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x2B, 

            0x2C, 0x2D, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 

            0x3B, 0x3C, 0x3D, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 

            0x4A, 0x4B, 0x4C, 0x4D, 0x50, 0x51
        };

        public int[] CurrentDeck = new int[54];

    }

    class Program
    {
        static void Main(string[] args)
        {
            // Create a Deck object
            Deck mainDeck = new Deck();
            Console.WriteLine("Here is the card array:");
            for (int index = 0; index != mainDeck.CurrentDeck.Length; index++)
            {
                string card = mainDeck.CurrentDeck[index].ToString("x");
                Console.WriteLine("0x" + card);
            }
        }
    }
}

The hexidecimal numbers stand for different cards.

When I compile it I get an array index out of bounds error, and a crash.

I don't understand what is wrong.

Any help would be much appreciated.

3 Answers 3

1

Shouldn't this be

for (int k = CurrentDeck.Length - 1; k >= 0; k--)

instead of

for (int k = CurrentDeck.Length - 1; k >= 0; k++)

Also you can store the length property to a variable and then check against that variable.

int currentDeckLength = CurrentDeck.Length;

for (int k = currentDeckLength - 1; k >= 0; k++)
Sign up to request clarification or add additional context in comments.

1 Comment

Wow, you guys are speedy. Thanks alot. I got this off another site, so I think they didn't test the code. I will try it now.
0

You should make your loop decrease: k-- instead of k++.

By incrementing it you are accessing an out of bound index since you initialized k at the max count to begin with. Your loop looks like it is setup to loop backwards, so decrement the index.

1 Comment

Thanks, I can't believe how fast you guys are. Do you just wait around for poor helpless programmers to beg information :)
0

You have for (int k = CurrentDeck.Length - 1; k >= 0; k++)

It should be for (int k = CurrentDeck.Length - 1; k >= 0; k--)

Do you see the difference?

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.