Card shuffle program code help needed
-
The code applies to any version of C#. There's really nothing to it. All it's doing is returning a string with the value of
face
andsuit
converted to strings. Those two variables have to be supplied by other code in your class. There's no way to tell you what you're doing wrong since we can't see the code for the typedeck1
is, nor can we see the code for what, I assume, your card class is. You're going to have to hit the "Improve question" button to add the relevant code to your question. Also, stay away from YouTube videos for learning to code. I haven't seen a good one yet.Asking questions is a skill CodeProject Forum Guidelines Google: C# How to debug code Seriously, go read these articles.
Dave Kreskowiakusing 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
-
Firstly,
Return
is not a C# keyword: it's case sensitive, remember - so it'sreturn
instead. Secondly, whatreturn face + " of " + suit;
returns depends on the type of face and suit - if they are both strings, it'll work. If
face
is an integer, it'll work. If it's a special class, then it'll only work if you have overriddenToString
in that class. But basically, we can't directly help you - we can't run your code in isolation and get the same results you do. So, it's going to be up to you. Fortunately, you have a tool available to you which will help you find out what is going on: the debugger. If you don't know how to use it then a quick Google for "Visual Studio debugger" should give you the info you need. Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why. Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging!"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony AntiTwitter: @DalekDave is now a follower!
Hi Griff. 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. I have sent the complete code in hope that you might spot an error. I posted the complete code as a reply to my first reply which I think was from yourself. Hoping you can help Brian
-
Hi Griff. 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. I have sent the complete code in hope that you might spot an error. I posted the complete code as a reply to my first reply which I think was from yourself. Hoping you can help Brian
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!
-
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
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#. -
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
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
-
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
Hi Richard. Thanks fro the suggestion. I did as you suggested by got an error of "Input string was not in correct format". Brian
-
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!
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
-
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#.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
-
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!
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
-
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
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.
-
Hi Richard. Thanks fro the suggestion. I did as you suggested by got an error of "Input string was not in correct format". Brian
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
-
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
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); -
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);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
-
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);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
-
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
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