Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Card shuffle program code help needed

Card shuffle program code help needed

Scheduled Pinned Locked Moved C#
helpquestionannouncement
20 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Brian_TheLion

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;

    namespace Card_Shuffle
    {
    public class Deck
    {
    private Card[] deck;
    private int currentCard;
    private const int NUMBER_OF_CARDS = 52;
    private Random ranNum;

        public Deck()
        {
            string\[\] faces = { "Ace", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nin", "Ten", "Jack", "Queen", "King" };
            string\[\] suits = { "Hearts", "Clubs", "Diamonds", "Spades" };
            deck = new Card\[NUMBER\_OF\_CARDS\];
            currentCard = 0;
            ranNum = new Random();
            for (int count = 0; count < deck.Length; count++)
                deck\[count\] = new Card(faces\[count % 11\],
                     suits\[count / 13\]);
        }
    
        public void Shuffle()
        {
            currentCard = 0;
            for (int first = 0; first < deck.Length; first++)
            {
                int second = ranNum.Next(NUMBER\_OF\_CARDS);
                Card temp = deck\[first\];
                deck\[first\] = deck\[second\];
                deck\[second\] = temp;
            }
        }
        public Card DealCard()
        {
            if (currentCard < deck.Length)
                return deck\[currentCard++\];
            else return null;
        }
    
        public class Card
        {
            private string face;
            private string suit;
            public Card(string cardFace, string cardSuit)
            {
                face = cardFace;
                suit = cardSuit;
            }
            public override string ToString()
            {
                return face + " of " + suit;
            }
    
        }
    
    
        static void Main(string\[\] args)
        {
            Deck deck1 = new Deck();
            deck1.Shuffle();
            for (int i = 0; i < 52; i++)
            {
                Console.Write("0-19)", deck1.DealCard());
             
                if ((i + 1) % 4 == 0)
                
                    Console.WriteLine();
                }
                Console.ReadLine();
            }
    
        }
    }
    

    At the moment I get 13 rows of the below text displayed. 0-19)0-19)0-19)0-19) Brian

    L Offline
    L Offline
    Lost User
    wrote on last edited by
    #9

    Brian_TheLion wrote:

    At the moment I get 13 rows of the below text displayed.

    Quite right because that is what your code is doing. Look at the line:

    Console.Write("0-19)", deck1.DealCard());

    It says print the string "0-19)"; the second part deck1.DealCard() returns a string but does nothing with it. The rest of the code does very little of any use. As already suggested you should stop wasting your time on YouTube and get hold of a decent book on C#.

    B 1 Reply Last reply
    0
    • B Brian_TheLion

      using System;
      using System.Collections.Generic;
      using System.Linq;
      using System.Text;
      using System.Threading.Tasks;

      namespace Card_Shuffle
      {
      public class Deck
      {
      private Card[] deck;
      private int currentCard;
      private const int NUMBER_OF_CARDS = 52;
      private Random ranNum;

          public Deck()
          {
              string\[\] faces = { "Ace", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nin", "Ten", "Jack", "Queen", "King" };
              string\[\] suits = { "Hearts", "Clubs", "Diamonds", "Spades" };
              deck = new Card\[NUMBER\_OF\_CARDS\];
              currentCard = 0;
              ranNum = new Random();
              for (int count = 0; count < deck.Length; count++)
                  deck\[count\] = new Card(faces\[count % 11\],
                       suits\[count / 13\]);
          }
      
          public void Shuffle()
          {
              currentCard = 0;
              for (int first = 0; first < deck.Length; first++)
              {
                  int second = ranNum.Next(NUMBER\_OF\_CARDS);
                  Card temp = deck\[first\];
                  deck\[first\] = deck\[second\];
                  deck\[second\] = temp;
              }
          }
          public Card DealCard()
          {
              if (currentCard < deck.Length)
                  return deck\[currentCard++\];
              else return null;
          }
      
          public class Card
          {
              private string face;
              private string suit;
              public Card(string cardFace, string cardSuit)
              {
                  face = cardFace;
                  suit = cardSuit;
              }
              public override string ToString()
              {
                  return face + " of " + suit;
              }
      
          }
      
      
          static void Main(string\[\] args)
          {
              Deck deck1 = new Deck();
              deck1.Shuffle();
              for (int i = 0; i < 52; i++)
              {
                  Console.Write("0-19)", deck1.DealCard());
               
                  if ((i + 1) % 4 == 0)
                  
                      Console.WriteLine();
                  }
                  Console.ReadLine();
              }
      
          }
      }
      

      At the moment I get 13 rows of the below text displayed. 0-19)0-19)0-19)0-19) Brian

      Richard DeemingR Offline
      Richard DeemingR Offline
      Richard Deeming
      wrote on last edited by
      #10

      Brian_TheLion wrote:

      Console.Write("0-19)", deck1.DealCard());

      I suspect that should be:

      Console.Write("{0,-19}", deck1.DealCard());

      Composite formatting | Microsoft Docs[^]


      "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

      "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

      B 2 Replies Last reply
      0
      • Richard DeemingR Richard Deeming

        Brian_TheLion wrote:

        Console.Write("0-19)", deck1.DealCard());

        I suspect that should be:

        Console.Write("{0,-19}", deck1.DealCard());

        Composite formatting | Microsoft Docs[^]


        "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

        B Offline
        B Offline
        Brian_TheLion
        wrote on last edited by
        #11

        Hi Richard. Thanks fro the suggestion. I did as you suggested by got an error of "Input string was not in correct format". Brian

        Richard DeemingR 1 Reply Last reply
        0
        • OriginalGriffO OriginalGriff

          Brian_TheLion wrote:

          I did try stepping through the code but with the way the program is written I can't always get exact values for some of the variables.

          Cobblers. If you can't get "exact values" with the debugger then you are using it wrong - or not using it at all - when you run code in the debugger it is executing your code, so you get to see exactly what you code does when it is running. If the values you see are not what you expected, then that's a problem with your code, not with the debugger - and that's what the debugger is there for, to help you find the palces where your code isn;t doing what you expected it to. Shuffling cards is trivial: a very simple way to do it is to put all the cards in an array and use an random number generator to give you two indexes: swap the two cards in those locations. Repeat that at least 5 or 6 times the number of cards involved (52 in a normal pack, so 260 to 312) and the cards are shufffled ...

          "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony AntiTwitter: @DalekDave is now a follower!

          B Offline
          B Offline
          Brian_TheLion
          wrote on last edited by
          #12

          Hi Griff. I'll give you an example why I'm not getting enought info when debugging. Please refer to my code that I posted a day ago . In the line return deck[CurrentCard++], If I put on cursor on deck[CurrentCard++] up pops the message "deck1 Card_Shuffle.Deck)" In the Console.Write line of code, if I put my cursor on deck1.DealCard() then up pops the message "deck1 Card_Shuffle.Deck). I can't see how I can find out what is wrong by the messages I get when checking these variables in the code. Brian

          F 1 Reply Last reply
          0
          • L Lost User

            Brian_TheLion wrote:

            At the moment I get 13 rows of the below text displayed.

            Quite right because that is what your code is doing. Look at the line:

            Console.Write("0-19)", deck1.DealCard());

            It says print the string "0-19)"; the second part deck1.DealCard() returns a string but does nothing with it. The rest of the code does very little of any use. As already suggested you should stop wasting your time on YouTube and get hold of a decent book on C#.

            B Offline
            B Offline
            Brian_TheLion
            wrote on last edited by
            #13

            Hi Richard. Do you think there is some code missing? If this is the case then I may have to give up on getting this code to work unless you could suggest some extra code I could add to the existing code. Brian

            L 1 Reply Last reply
            0
            • OriginalGriffO OriginalGriff

              Dave Kreskowiak wrote:

              stay away from YouTube videos for learning to code. I haven't seen a good one yet.

              I suspect that most of 'em are created by people who 1) Have no idea how to make a good video And 2) Have no idea how to code. They have code that works, but they have no idea why it does - so they blindly type it in and then show "working" results - that most times are unrelated to the code they actually typed.

              "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony AntiTwitter: @DalekDave is now a follower!

              B Offline
              B Offline
              Brian_TheLion
              wrote on last edited by
              #14

              Hi Griff. Can you suggest somewhere that I might be able to find C# code listing for a card shuffing program or any card program. Brian

              1 Reply Last reply
              0
              • B Brian_TheLion

                Hi Richard. Do you think there is some code missing? If this is the case then I may have to give up on getting this code to work unless you could suggest some extra code I could add to the existing code. Brian

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #15

                Quite frankly I don't think it's the best design of a card class that I have seen. Also, nearly all the code we see here that is taken code from a Youtube video, turns out to be not very well written. Youtubers tend not to be particularly skilled coders. If you want to learn C# properly then get hold of a decent book on the subject, it will be time and money far better spent.

                1 Reply Last reply
                0
                • B Brian_TheLion

                  Hi Richard. Thanks fro the suggestion. I did as you suggested by got an error of "Input string was not in correct format". Brian

                  Richard DeemingR Offline
                  Richard DeemingR Offline
                  Richard Deeming
                  wrote on last edited by
                  #16

                  With that change, the code you've shown works fine for me. Card shuffle | C# Online Compiler | .NET Fiddle[^]


                  "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                  "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

                  1 Reply Last reply
                  0
                  • B Brian_TheLion

                    Hi Griff. I'll give you an example why I'm not getting enought info when debugging. Please refer to my code that I posted a day ago . In the line return deck[CurrentCard++], If I put on cursor on deck[CurrentCard++] up pops the message "deck1 Card_Shuffle.Deck)" In the Console.Write line of code, if I put my cursor on deck1.DealCard() then up pops the message "deck1 Card_Shuffle.Deck). I can't see how I can find out what is wrong by the messages I get when checking these variables in the code. Brian

                    F Offline
                    F Offline
                    F ES Sitecore
                    wrote on last edited by
                    #17

                    That's just the basic type info for what you're hovering over. When you hover over deck1 it shows the info for that object. If you want to know what "deck1[x]" is then you can highlight the whole string including the square brackets, right click and select "Quick Watch" from the context menu. Another debugging tip is that compound statements like this

                    Console.Write("0-19)", deck1.DealCard());

                    are harder to debug. While still in the coding process it will often help to split things up into individual lines

                    Card card = deck1.DealCard();
                    Console.Write("0-19)", card);

                    You can now inspect "card" to see what DealCard has returned. If it looks like what you expect then you know it is the Console.Write that isn't doing what you expect. By splitting the code into two separate stages you can test them separately...you can test DealCard is doing what you want, then Console.Write. With your original code you are doing two things at once so it is harder to know which of those things is failing. Once you have your bugs ironed out and the code working, you can go back to the code in line if you wish. The reason your Console.Write isn't working is because you are using the two param method, so the first param ("0-19)") is the "format" and the second param ("card") is the data you want to show in that format. However "0-19)" is not a proper formatting string so all you will get is that text out verbatim. A formatting string has placeholders for the values, so {0} for the first param, {1} for the second etc. To see this in action try;

                    Card card = deck1.DealCard();
                    Console.Write("card {0} = {1} ", i, card);

                    B 2 Replies Last reply
                    0
                    • F F ES Sitecore

                      That's just the basic type info for what you're hovering over. When you hover over deck1 it shows the info for that object. If you want to know what "deck1[x]" is then you can highlight the whole string including the square brackets, right click and select "Quick Watch" from the context menu. Another debugging tip is that compound statements like this

                      Console.Write("0-19)", deck1.DealCard());

                      are harder to debug. While still in the coding process it will often help to split things up into individual lines

                      Card card = deck1.DealCard();
                      Console.Write("0-19)", card);

                      You can now inspect "card" to see what DealCard has returned. If it looks like what you expect then you know it is the Console.Write that isn't doing what you expect. By splitting the code into two separate stages you can test them separately...you can test DealCard is doing what you want, then Console.Write. With your original code you are doing two things at once so it is harder to know which of those things is failing. Once you have your bugs ironed out and the code working, you can go back to the code in line if you wish. The reason your Console.Write isn't working is because you are using the two param method, so the first param ("0-19)") is the "format" and the second param ("card") is the data you want to show in that format. However "0-19)" is not a proper formatting string so all you will get is that text out verbatim. A formatting string has placeholders for the values, so {0} for the first param, {1} for the second etc. To see this in action try;

                      Card card = deck1.DealCard();
                      Console.Write("card {0} = {1} ", i, card);

                      B Offline
                      B Offline
                      Brian_TheLion
                      wrote on last edited by
                      #18

                      Thanks F-ES Sitecore for the suggested changes to the code. I finally have got this program to print out the cards. Good to make some progress. I altered the your code to Console.Write(card+","); so they would print in my preferred format. Brian

                      1 Reply Last reply
                      0
                      • F F ES Sitecore

                        That's just the basic type info for what you're hovering over. When you hover over deck1 it shows the info for that object. If you want to know what "deck1[x]" is then you can highlight the whole string including the square brackets, right click and select "Quick Watch" from the context menu. Another debugging tip is that compound statements like this

                        Console.Write("0-19)", deck1.DealCard());

                        are harder to debug. While still in the coding process it will often help to split things up into individual lines

                        Card card = deck1.DealCard();
                        Console.Write("0-19)", card);

                        You can now inspect "card" to see what DealCard has returned. If it looks like what you expect then you know it is the Console.Write that isn't doing what you expect. By splitting the code into two separate stages you can test them separately...you can test DealCard is doing what you want, then Console.Write. With your original code you are doing two things at once so it is harder to know which of those things is failing. Once you have your bugs ironed out and the code working, you can go back to the code in line if you wish. The reason your Console.Write isn't working is because you are using the two param method, so the first param ("0-19)") is the "format" and the second param ("card") is the data you want to show in that format. However "0-19)" is not a proper formatting string so all you will get is that text out verbatim. A formatting string has placeholders for the values, so {0} for the first param, {1} for the second etc. To see this in action try;

                        Card card = deck1.DealCard();
                        Console.Write("card {0} = {1} ", i, card);

                        B Offline
                        B Offline
                        Brian_TheLion
                        wrote on last edited by
                        #19

                        Hi F-ES Sitecore. I had another look at the original and found that I had made a mistake. Console.Write("0-19)", deck1.DealCard); should have been Console.Write("{0,-19}", deck1.DealCard); I don't think a debugging the code would have lead me to the correct code but I can see your point in expanding the code over several lines. Brian

                        1 Reply Last reply
                        0
                        • Richard DeemingR Richard Deeming

                          Brian_TheLion wrote:

                          Console.Write("0-19)", deck1.DealCard());

                          I suspect that should be:

                          Console.Write("{0,-19}", deck1.DealCard());

                          Composite formatting | Microsoft Docs[^]


                          "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                          B Offline
                          B Offline
                          Brian_TheLion
                          wrote on last edited by
                          #20

                          You were correct Richard. When I tried the format code you offered I had left the comma out of the code and got an error I tried Console.Write("{0-19}", deck1.DealCard()); instead of Console.Write("{0,-19}", deck1.DealCard()); All is working find now thanks. Brian

                          1 Reply Last reply
                          0
                          Reply
                          • Reply as topic
                          Log in to reply
                          • Oldest to Newest
                          • Newest to Oldest
                          • Most Votes


                          • Login

                          • Don't have an account? Register

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • World
                          • Users
                          • Groups