Click here to Skip to main content
15,891,184 members
Please Sign up or sign in to vote.
1.80/5 (2 votes)
See more:
C#
static string[] ranks = new string[13] { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K" };
public static int rankCounter = 0;
public static string[] suits = new string[4] { "♠", "♣", "♦", "♥" };
public static int suitsCounter = 0;
public static int shuffle;
public static Random rnd = new Random();
public static int[] value = new int[52];
public static string numbers = string.Empty;
public static string[] cards = new string[52];
 
static string[] ShuffleCardSystem()
{
    string temp;
    for (int i = 0; i < 52; i++)
    {
        cards[i] = ranks[rankCounter] + suits[suitsCounter];
        rankCounter++;
        if (rankCounter == 13)
        {
            rankCounter = 0;
            suitsCounter += 1;
        }
    }
    for (int i = 51; i >= 0; i--)
    {
        numbers = string.Empty;
        shuffle = rnd.Next(0, i);
        temp = cards[shuffle];
        cards[shuffle] = cards[i];
        cards[i] = temp;
        for (int position = 0; position < cards[i].Length; position++)
        {
            if (char.IsDigit(cards[i][position]))
            {
                numbers += cards[i][position];

                value[i] = Convert.ToInt32(numbers]);
                if (value[i] == 1)
                {
                    value[i] = 11;
                }
            }
            else if (cards[i].Any(char.IsLetter))
            {
                value[i] = 10;
                numbers += cards[i][position];
            }            
        }
    }
}


vs this...

C#
static string[] ShuffleCardSystem()
{
    string temp;
    for (int i = 0; i < 52; i++)
    {
        cards[i] = ranks[rankCounter] + suits[suitsCounter];
        rankCounter++;
        if (rankCounter == 13)
        {
            rankCounter = 0;
            suitsCounter += 1;
        }
    }
    for (int i = 51; i >= 0; i--)
    {
        numbers = string.Empty;
        shuffle = rnd.Next(0, i);
        temp = cards[shuffle];
        cards[shuffle] = cards[i];
        cards[i] = temp;
        for (int position = 0; position < cards[i].Length; position++)
        {
            if (char.IsDigit(cards[i][position]))
            {
                static string[] ShuffleCardSystem()   // WTF ?
                {
                    string temp;
                    for (int i = 0; i < 52; i++)
                    {
                        cards[i] = ranks[rankCounter] + suits[suitsCounter];
                        rankCounter++;
                        if (rankCounter == 13)
                        {
                            rankCounter = 0;
                            suitsCounter += 1;
                        }
                    }
                    for (int i = 51; i >= 0; i--)
                    {
                        numbers = string.Empty;
                        shuffle = rnd.Next(0, i);
                        temp = cards[shuffle];
                        cards[shuffle] = cards[i];
                        cards[i] = temp;
                        for (int position = 0; position < cards[i].Length; position++)
                        {
                            if (char.IsDigit(cards[i][position]))
                            {
                                value[i] = Convert.ToInt32(cards[i][position]);
                                if (value[i] == 1)
                                {
                                    value[i] = 11;
                                }
                            }
                            else if (cards[i].Any(char.IsLetter))
                            {
                                value[i] = 10;
                                numbers += cards[i][position];  
                            }
                        }
                        value[i] = Convert.ToInt32(numbers);
                        if (value[i] == 1)
                        {
                            value[i] = 11;
                        }
                    }
                    else if (cards[i].Any(char.IsLetter))
                    {
                        value[i] = 10;
                    }
                }
            }
        }
    }
}


Having "numbers" should not matter because it is equal to [i][position]

What I have tried:

I tried to clean up my codes but instead of showing 1,2,3 as a value it gives 49,50,51 I just want to know why
Posted
Updated 26-Jun-16 20:00pm
v3

1 solution

In one version, you "assemble" a number in numbers and convert it:
C#
numbers += cards[i][position];
value[i] = Convert.ToInt32(numbers]);
So each time round the position loop, you convert a longer number.
In the other, you still assemble a longer value in numbers, but you don;t use it:
C#
value[i] = Convert.ToInt32(cards[i][position]);

So the value you get in value[i] is going to be different.
Make a copy of your solution folder, then open two instances of VS - one for each - and run each version of the code through the debugger. If you step through each program, you should see what differences you get, and why fairly easily.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900