Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 648957
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T21:53:24+00:00 2026-05-13T21:53:24+00:00

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

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T21:53:24+00:00Added an answer on May 13, 2026 at 9:53 pm

    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++)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am learning c++ and I am writing a card dealer program. When I
I have a problem writing to the SD card, here is the code: (Sorry
I am writing a code that collects all the credit card information of each
I'm writing a simple program using SDL, but I'm running into a strange problem.
I have been playing around with writing some simple card games in Python for
I've got problem with writing files on the SD card in the emulator. Here
While writing a card shuffling algorithm I realized that there are 52! ~= 2^225
I am writing a card game in java where I need to spread cards
I'm writing a 7 card poker hand evaluator as one of my pet projects.
I'm writing the simple card game War for homework and now that the game

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.