Creating a deck of cards in c#
So I'm trying to create a deck of cards for one of my programming classes. I've never really done anything like this so sorry if I made some stupid mistake. I'm coding this in Visual Studio (per class rules). I am trying to create an array of Card objects for my Deck. The problem I am getting is that when I try and print the array out all I get is 52 lines of Card_Games.Card (the namespace is Card_Games). What did I do wrong in my Card class to not properly assign the value and suit of a card to that Card object?
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
Deck.FillDeck();
Deck.PrintDeck();
}
}
class Card
{
public int Value;
public static string SuitsArray = new string {"Hearts", "Diamonds", "Clubs", "Spades"};
public string Suit;
public Card(int value, string suit)
{
Value = value;
Suit = suit;
}
public Card(string input)
{
string tempValue = "";
string suitSentence = "";
switch (Value)
{
case 11:
tempValue = "Jack";
break;
case 12:
tempValue = "Queen";
break;
case 13:
tempValue = "King";
break;
case 14:
tempValue = "Ace";
break;
default:
tempValue = Value.ToString();
break;
}
switch (Suit)
{
case "Hearts":
suitSentence = " of Hearts";
break;
case "Diamonds":
suitSentence = " of Diamonds";
break;
case "Clubs":
suitSentence = " of Clubs";
break;
case "Spades":
suitSentence = " of Spades";
break;
}
input = tempValue + suitSentence;
}
}
class Deck
{
public static Object deck = new Object[52];
public static void FillDeck()
{
int index = 0;
foreach (string suit in Card.SuitsArray)
{
for (int value = 2; value <= 14; value++)
{
Card card = new Card(value, suit);
deck[index] = card;
index++;
}
}
}
public static void PrintDeck()
{
for (int i=0; i<52; i++)
{
System.Diagnostics.Debug.WriteLine(deck[i]);
}
}
}
c#
|
show 6 more comments
So I'm trying to create a deck of cards for one of my programming classes. I've never really done anything like this so sorry if I made some stupid mistake. I'm coding this in Visual Studio (per class rules). I am trying to create an array of Card objects for my Deck. The problem I am getting is that when I try and print the array out all I get is 52 lines of Card_Games.Card (the namespace is Card_Games). What did I do wrong in my Card class to not properly assign the value and suit of a card to that Card object?
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
Deck.FillDeck();
Deck.PrintDeck();
}
}
class Card
{
public int Value;
public static string SuitsArray = new string {"Hearts", "Diamonds", "Clubs", "Spades"};
public string Suit;
public Card(int value, string suit)
{
Value = value;
Suit = suit;
}
public Card(string input)
{
string tempValue = "";
string suitSentence = "";
switch (Value)
{
case 11:
tempValue = "Jack";
break;
case 12:
tempValue = "Queen";
break;
case 13:
tempValue = "King";
break;
case 14:
tempValue = "Ace";
break;
default:
tempValue = Value.ToString();
break;
}
switch (Suit)
{
case "Hearts":
suitSentence = " of Hearts";
break;
case "Diamonds":
suitSentence = " of Diamonds";
break;
case "Clubs":
suitSentence = " of Clubs";
break;
case "Spades":
suitSentence = " of Spades";
break;
}
input = tempValue + suitSentence;
}
}
class Deck
{
public static Object deck = new Object[52];
public static void FillDeck()
{
int index = 0;
foreach (string suit in Card.SuitsArray)
{
for (int value = 2; value <= 14; value++)
{
Card card = new Card(value, suit);
deck[index] = card;
index++;
}
}
}
public static void PrintDeck()
{
for (int i=0; i<52; i++)
{
System.Diagnostics.Debug.WriteLine(deck[i]);
}
}
}
c#
7
First and foremost, unless you implementCard.ToString
(i.e. override the default implementation), you won't get a meaningful output, because .NET doesn't know how to convert a "card" into astring
.
– Groo
Nov 19 '18 at 22:23
So I just need to do an override on the ToString method?
– MattCV27
Nov 19 '18 at 22:24
2
also, public Card(string input) doesn't actually appear to be doing anything
– Broom
Nov 19 '18 at 22:25
Yes, that's right.
– Adrian
Nov 19 '18 at 22:25
1
That's the first thing to do if you want to get a textual representation, yes. Other remarks would be: 1) if you are usingstatic
methods, you are usually doing something wrong. In this case, it means that only a single deck can exist in your app. 2) type ofdeck
should beCard
orList<Card>
, notObject
. 3)public Card(string input)
is a constructor, which doesn't do anything.
– Groo
Nov 19 '18 at 22:26
|
show 6 more comments
So I'm trying to create a deck of cards for one of my programming classes. I've never really done anything like this so sorry if I made some stupid mistake. I'm coding this in Visual Studio (per class rules). I am trying to create an array of Card objects for my Deck. The problem I am getting is that when I try and print the array out all I get is 52 lines of Card_Games.Card (the namespace is Card_Games). What did I do wrong in my Card class to not properly assign the value and suit of a card to that Card object?
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
Deck.FillDeck();
Deck.PrintDeck();
}
}
class Card
{
public int Value;
public static string SuitsArray = new string {"Hearts", "Diamonds", "Clubs", "Spades"};
public string Suit;
public Card(int value, string suit)
{
Value = value;
Suit = suit;
}
public Card(string input)
{
string tempValue = "";
string suitSentence = "";
switch (Value)
{
case 11:
tempValue = "Jack";
break;
case 12:
tempValue = "Queen";
break;
case 13:
tempValue = "King";
break;
case 14:
tempValue = "Ace";
break;
default:
tempValue = Value.ToString();
break;
}
switch (Suit)
{
case "Hearts":
suitSentence = " of Hearts";
break;
case "Diamonds":
suitSentence = " of Diamonds";
break;
case "Clubs":
suitSentence = " of Clubs";
break;
case "Spades":
suitSentence = " of Spades";
break;
}
input = tempValue + suitSentence;
}
}
class Deck
{
public static Object deck = new Object[52];
public static void FillDeck()
{
int index = 0;
foreach (string suit in Card.SuitsArray)
{
for (int value = 2; value <= 14; value++)
{
Card card = new Card(value, suit);
deck[index] = card;
index++;
}
}
}
public static void PrintDeck()
{
for (int i=0; i<52; i++)
{
System.Diagnostics.Debug.WriteLine(deck[i]);
}
}
}
c#
So I'm trying to create a deck of cards for one of my programming classes. I've never really done anything like this so sorry if I made some stupid mistake. I'm coding this in Visual Studio (per class rules). I am trying to create an array of Card objects for my Deck. The problem I am getting is that when I try and print the array out all I get is 52 lines of Card_Games.Card (the namespace is Card_Games). What did I do wrong in my Card class to not properly assign the value and suit of a card to that Card object?
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
Deck.FillDeck();
Deck.PrintDeck();
}
}
class Card
{
public int Value;
public static string SuitsArray = new string {"Hearts", "Diamonds", "Clubs", "Spades"};
public string Suit;
public Card(int value, string suit)
{
Value = value;
Suit = suit;
}
public Card(string input)
{
string tempValue = "";
string suitSentence = "";
switch (Value)
{
case 11:
tempValue = "Jack";
break;
case 12:
tempValue = "Queen";
break;
case 13:
tempValue = "King";
break;
case 14:
tempValue = "Ace";
break;
default:
tempValue = Value.ToString();
break;
}
switch (Suit)
{
case "Hearts":
suitSentence = " of Hearts";
break;
case "Diamonds":
suitSentence = " of Diamonds";
break;
case "Clubs":
suitSentence = " of Clubs";
break;
case "Spades":
suitSentence = " of Spades";
break;
}
input = tempValue + suitSentence;
}
}
class Deck
{
public static Object deck = new Object[52];
public static void FillDeck()
{
int index = 0;
foreach (string suit in Card.SuitsArray)
{
for (int value = 2; value <= 14; value++)
{
Card card = new Card(value, suit);
deck[index] = card;
index++;
}
}
}
public static void PrintDeck()
{
for (int i=0; i<52; i++)
{
System.Diagnostics.Debug.WriteLine(deck[i]);
}
}
}
c#
c#
edited Nov 20 '18 at 0:34
Ahmed Abdelhameed
5,61382146
5,61382146
asked Nov 19 '18 at 22:19
MattCV27MattCV27
192
192
7
First and foremost, unless you implementCard.ToString
(i.e. override the default implementation), you won't get a meaningful output, because .NET doesn't know how to convert a "card" into astring
.
– Groo
Nov 19 '18 at 22:23
So I just need to do an override on the ToString method?
– MattCV27
Nov 19 '18 at 22:24
2
also, public Card(string input) doesn't actually appear to be doing anything
– Broom
Nov 19 '18 at 22:25
Yes, that's right.
– Adrian
Nov 19 '18 at 22:25
1
That's the first thing to do if you want to get a textual representation, yes. Other remarks would be: 1) if you are usingstatic
methods, you are usually doing something wrong. In this case, it means that only a single deck can exist in your app. 2) type ofdeck
should beCard
orList<Card>
, notObject
. 3)public Card(string input)
is a constructor, which doesn't do anything.
– Groo
Nov 19 '18 at 22:26
|
show 6 more comments
7
First and foremost, unless you implementCard.ToString
(i.e. override the default implementation), you won't get a meaningful output, because .NET doesn't know how to convert a "card" into astring
.
– Groo
Nov 19 '18 at 22:23
So I just need to do an override on the ToString method?
– MattCV27
Nov 19 '18 at 22:24
2
also, public Card(string input) doesn't actually appear to be doing anything
– Broom
Nov 19 '18 at 22:25
Yes, that's right.
– Adrian
Nov 19 '18 at 22:25
1
That's the first thing to do if you want to get a textual representation, yes. Other remarks would be: 1) if you are usingstatic
methods, you are usually doing something wrong. In this case, it means that only a single deck can exist in your app. 2) type ofdeck
should beCard
orList<Card>
, notObject
. 3)public Card(string input)
is a constructor, which doesn't do anything.
– Groo
Nov 19 '18 at 22:26
7
7
First and foremost, unless you implement
Card.ToString
(i.e. override the default implementation), you won't get a meaningful output, because .NET doesn't know how to convert a "card" into a string
.– Groo
Nov 19 '18 at 22:23
First and foremost, unless you implement
Card.ToString
(i.e. override the default implementation), you won't get a meaningful output, because .NET doesn't know how to convert a "card" into a string
.– Groo
Nov 19 '18 at 22:23
So I just need to do an override on the ToString method?
– MattCV27
Nov 19 '18 at 22:24
So I just need to do an override on the ToString method?
– MattCV27
Nov 19 '18 at 22:24
2
2
also, public Card(string input) doesn't actually appear to be doing anything
– Broom
Nov 19 '18 at 22:25
also, public Card(string input) doesn't actually appear to be doing anything
– Broom
Nov 19 '18 at 22:25
Yes, that's right.
– Adrian
Nov 19 '18 at 22:25
Yes, that's right.
– Adrian
Nov 19 '18 at 22:25
1
1
That's the first thing to do if you want to get a textual representation, yes. Other remarks would be: 1) if you are using
static
methods, you are usually doing something wrong. In this case, it means that only a single deck can exist in your app. 2) type of deck
should be Card
or List<Card>
, not Object
. 3) public Card(string input)
is a constructor, which doesn't do anything.– Groo
Nov 19 '18 at 22:26
That's the first thing to do if you want to get a textual representation, yes. Other remarks would be: 1) if you are using
static
methods, you are usually doing something wrong. In this case, it means that only a single deck can exist in your app. 2) type of deck
should be Card
or List<Card>
, not Object
. 3) public Card(string input)
is a constructor, which doesn't do anything.– Groo
Nov 19 '18 at 22:26
|
show 6 more comments
2 Answers
2
active
oldest
votes
Change object
to Card
. Then in your print method you can print deck[i].Value
and deck[i].Suit
. Here it is as a console app:
class Program
{
static void Main(string args)
{
Deck.FillDeck();
Deck.PrintDeck();
Console.ReadLine();
}
}
class Card
{
public int Value;
public static string SuitsArray = new string { "Hearts", "Diamonds", "Clubs", "Spades" };
public string Suit;
public Card(int value, string suit)
{
Value = value;
Suit = suit;
}
public Card(string input)
{
string tempValue = "";
string suitSentence = "";
switch (Value)
{
case 11:
tempValue = "Jack";
break;
case 12:
tempValue = "Queen";
break;
case 13:
tempValue = "King";
break;
case 14:
tempValue = "Ace";
break;
default:
tempValue = Value.ToString();
break;
}
switch (Suit)
{
case "Hearts":
suitSentence = " of Hearts";
break;
case "Diamonds":
suitSentence = " of Diamonds";
break;
case "Clubs":
suitSentence = " of Clubs";
break;
case "Spades":
suitSentence = " of Spades";
break;
}
input = tempValue + suitSentence;
}
}
class Deck
{
public static Card deck = new Card[52];
public static void FillDeck()
{
int index = 0;
foreach (string suit in Card.SuitsArray)
{
for (int value = 2; value <= 14; value++)
{
Card card = new Card(value, suit);
deck[index] = card;
index++;
}
}
}
public static void PrintDeck()
{
for (int i = 0; i < 52; i++)
{
Console.WriteLine($"{deck[i].Value} {deck[i].Suit}");
}
}
}
I think you meant to sayCard
and notDeck
, right?
– Eric Lippert
Nov 19 '18 at 23:53
Whoops yes thanks for correcting me :).
– Jon Vote
Nov 20 '18 at 0:32
add a comment |
Here's a slightly optimized version.
I've added getters to pull a Named Value and a full Name, e.g "Ace Of Spades". And used an Enum
for suites.
Then I've used a single loop with the modulo operator (%
) and Math.Floor
to populate the deck. This is where the enum comes into play as it is trivial to work with enums and ints.
public class Card
{
public enum Suites
{
Hearts = 0,
Diamonds,
Clubs,
Spades
}
public int Value
{
get;
set;
}
public Suites Suite
{
get;
set;
}
//Used to get full name, also useful
//if you want to just get the named value
public string NamedValue
{
get
{
string name = string.Empty;
switch (Value)
{
case (14):
name = "Ace";
break;
case (13):
name = "King";
break;
case (12):
name = "Queen";
break;
case (11):
name = "Jack";
break;
default:
name = Value.ToString();
break;
}
return name;
}
}
public string Name
{
get
{
return NamedValue + " of " + Suite.ToString();
}
}
public Card(int Value, Suites Suite)
{
this.Value = Value;
this.Suite = Suite;
}
}
public class Deck
{
public List<Card> Cards = new List<Card>();
public void FillDeck()
{
public void FillDeck()
{
//Can use a single loop utilising the mod operator % and Math.Floor
//Using divition based on 13 cards in a suited
for (int i = 0; i < 52; i++)
{
Card.Suites suite = (Card.Suites)(Math.Floor((decimal)i/13));
//Add 2 to value as a cards start a 2
int val = i%13 + 2;
Cards.Add(new Card(val, suite));
}
}
}
public void PrintDeck()
{
foreach(Card card in this.Cards)
{
Console.WriteLine(card.Name);
}
}
}
Demo: https://dotnetfiddle.net/Xuj7b6
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53383468%2fcreating-a-deck-of-cards-in-c-sharp%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Change object
to Card
. Then in your print method you can print deck[i].Value
and deck[i].Suit
. Here it is as a console app:
class Program
{
static void Main(string args)
{
Deck.FillDeck();
Deck.PrintDeck();
Console.ReadLine();
}
}
class Card
{
public int Value;
public static string SuitsArray = new string { "Hearts", "Diamonds", "Clubs", "Spades" };
public string Suit;
public Card(int value, string suit)
{
Value = value;
Suit = suit;
}
public Card(string input)
{
string tempValue = "";
string suitSentence = "";
switch (Value)
{
case 11:
tempValue = "Jack";
break;
case 12:
tempValue = "Queen";
break;
case 13:
tempValue = "King";
break;
case 14:
tempValue = "Ace";
break;
default:
tempValue = Value.ToString();
break;
}
switch (Suit)
{
case "Hearts":
suitSentence = " of Hearts";
break;
case "Diamonds":
suitSentence = " of Diamonds";
break;
case "Clubs":
suitSentence = " of Clubs";
break;
case "Spades":
suitSentence = " of Spades";
break;
}
input = tempValue + suitSentence;
}
}
class Deck
{
public static Card deck = new Card[52];
public static void FillDeck()
{
int index = 0;
foreach (string suit in Card.SuitsArray)
{
for (int value = 2; value <= 14; value++)
{
Card card = new Card(value, suit);
deck[index] = card;
index++;
}
}
}
public static void PrintDeck()
{
for (int i = 0; i < 52; i++)
{
Console.WriteLine($"{deck[i].Value} {deck[i].Suit}");
}
}
}
I think you meant to sayCard
and notDeck
, right?
– Eric Lippert
Nov 19 '18 at 23:53
Whoops yes thanks for correcting me :).
– Jon Vote
Nov 20 '18 at 0:32
add a comment |
Change object
to Card
. Then in your print method you can print deck[i].Value
and deck[i].Suit
. Here it is as a console app:
class Program
{
static void Main(string args)
{
Deck.FillDeck();
Deck.PrintDeck();
Console.ReadLine();
}
}
class Card
{
public int Value;
public static string SuitsArray = new string { "Hearts", "Diamonds", "Clubs", "Spades" };
public string Suit;
public Card(int value, string suit)
{
Value = value;
Suit = suit;
}
public Card(string input)
{
string tempValue = "";
string suitSentence = "";
switch (Value)
{
case 11:
tempValue = "Jack";
break;
case 12:
tempValue = "Queen";
break;
case 13:
tempValue = "King";
break;
case 14:
tempValue = "Ace";
break;
default:
tempValue = Value.ToString();
break;
}
switch (Suit)
{
case "Hearts":
suitSentence = " of Hearts";
break;
case "Diamonds":
suitSentence = " of Diamonds";
break;
case "Clubs":
suitSentence = " of Clubs";
break;
case "Spades":
suitSentence = " of Spades";
break;
}
input = tempValue + suitSentence;
}
}
class Deck
{
public static Card deck = new Card[52];
public static void FillDeck()
{
int index = 0;
foreach (string suit in Card.SuitsArray)
{
for (int value = 2; value <= 14; value++)
{
Card card = new Card(value, suit);
deck[index] = card;
index++;
}
}
}
public static void PrintDeck()
{
for (int i = 0; i < 52; i++)
{
Console.WriteLine($"{deck[i].Value} {deck[i].Suit}");
}
}
}
I think you meant to sayCard
and notDeck
, right?
– Eric Lippert
Nov 19 '18 at 23:53
Whoops yes thanks for correcting me :).
– Jon Vote
Nov 20 '18 at 0:32
add a comment |
Change object
to Card
. Then in your print method you can print deck[i].Value
and deck[i].Suit
. Here it is as a console app:
class Program
{
static void Main(string args)
{
Deck.FillDeck();
Deck.PrintDeck();
Console.ReadLine();
}
}
class Card
{
public int Value;
public static string SuitsArray = new string { "Hearts", "Diamonds", "Clubs", "Spades" };
public string Suit;
public Card(int value, string suit)
{
Value = value;
Suit = suit;
}
public Card(string input)
{
string tempValue = "";
string suitSentence = "";
switch (Value)
{
case 11:
tempValue = "Jack";
break;
case 12:
tempValue = "Queen";
break;
case 13:
tempValue = "King";
break;
case 14:
tempValue = "Ace";
break;
default:
tempValue = Value.ToString();
break;
}
switch (Suit)
{
case "Hearts":
suitSentence = " of Hearts";
break;
case "Diamonds":
suitSentence = " of Diamonds";
break;
case "Clubs":
suitSentence = " of Clubs";
break;
case "Spades":
suitSentence = " of Spades";
break;
}
input = tempValue + suitSentence;
}
}
class Deck
{
public static Card deck = new Card[52];
public static void FillDeck()
{
int index = 0;
foreach (string suit in Card.SuitsArray)
{
for (int value = 2; value <= 14; value++)
{
Card card = new Card(value, suit);
deck[index] = card;
index++;
}
}
}
public static void PrintDeck()
{
for (int i = 0; i < 52; i++)
{
Console.WriteLine($"{deck[i].Value} {deck[i].Suit}");
}
}
}
Change object
to Card
. Then in your print method you can print deck[i].Value
and deck[i].Suit
. Here it is as a console app:
class Program
{
static void Main(string args)
{
Deck.FillDeck();
Deck.PrintDeck();
Console.ReadLine();
}
}
class Card
{
public int Value;
public static string SuitsArray = new string { "Hearts", "Diamonds", "Clubs", "Spades" };
public string Suit;
public Card(int value, string suit)
{
Value = value;
Suit = suit;
}
public Card(string input)
{
string tempValue = "";
string suitSentence = "";
switch (Value)
{
case 11:
tempValue = "Jack";
break;
case 12:
tempValue = "Queen";
break;
case 13:
tempValue = "King";
break;
case 14:
tempValue = "Ace";
break;
default:
tempValue = Value.ToString();
break;
}
switch (Suit)
{
case "Hearts":
suitSentence = " of Hearts";
break;
case "Diamonds":
suitSentence = " of Diamonds";
break;
case "Clubs":
suitSentence = " of Clubs";
break;
case "Spades":
suitSentence = " of Spades";
break;
}
input = tempValue + suitSentence;
}
}
class Deck
{
public static Card deck = new Card[52];
public static void FillDeck()
{
int index = 0;
foreach (string suit in Card.SuitsArray)
{
for (int value = 2; value <= 14; value++)
{
Card card = new Card(value, suit);
deck[index] = card;
index++;
}
}
}
public static void PrintDeck()
{
for (int i = 0; i < 52; i++)
{
Console.WriteLine($"{deck[i].Value} {deck[i].Suit}");
}
}
}
edited Nov 20 '18 at 0:31
Ahmed Abdelhameed
5,61382146
5,61382146
answered Nov 19 '18 at 23:42
Jon VoteJon Vote
3808
3808
I think you meant to sayCard
and notDeck
, right?
– Eric Lippert
Nov 19 '18 at 23:53
Whoops yes thanks for correcting me :).
– Jon Vote
Nov 20 '18 at 0:32
add a comment |
I think you meant to sayCard
and notDeck
, right?
– Eric Lippert
Nov 19 '18 at 23:53
Whoops yes thanks for correcting me :).
– Jon Vote
Nov 20 '18 at 0:32
I think you meant to say
Card
and not Deck
, right?– Eric Lippert
Nov 19 '18 at 23:53
I think you meant to say
Card
and not Deck
, right?– Eric Lippert
Nov 19 '18 at 23:53
Whoops yes thanks for correcting me :).
– Jon Vote
Nov 20 '18 at 0:32
Whoops yes thanks for correcting me :).
– Jon Vote
Nov 20 '18 at 0:32
add a comment |
Here's a slightly optimized version.
I've added getters to pull a Named Value and a full Name, e.g "Ace Of Spades". And used an Enum
for suites.
Then I've used a single loop with the modulo operator (%
) and Math.Floor
to populate the deck. This is where the enum comes into play as it is trivial to work with enums and ints.
public class Card
{
public enum Suites
{
Hearts = 0,
Diamonds,
Clubs,
Spades
}
public int Value
{
get;
set;
}
public Suites Suite
{
get;
set;
}
//Used to get full name, also useful
//if you want to just get the named value
public string NamedValue
{
get
{
string name = string.Empty;
switch (Value)
{
case (14):
name = "Ace";
break;
case (13):
name = "King";
break;
case (12):
name = "Queen";
break;
case (11):
name = "Jack";
break;
default:
name = Value.ToString();
break;
}
return name;
}
}
public string Name
{
get
{
return NamedValue + " of " + Suite.ToString();
}
}
public Card(int Value, Suites Suite)
{
this.Value = Value;
this.Suite = Suite;
}
}
public class Deck
{
public List<Card> Cards = new List<Card>();
public void FillDeck()
{
public void FillDeck()
{
//Can use a single loop utilising the mod operator % and Math.Floor
//Using divition based on 13 cards in a suited
for (int i = 0; i < 52; i++)
{
Card.Suites suite = (Card.Suites)(Math.Floor((decimal)i/13));
//Add 2 to value as a cards start a 2
int val = i%13 + 2;
Cards.Add(new Card(val, suite));
}
}
}
public void PrintDeck()
{
foreach(Card card in this.Cards)
{
Console.WriteLine(card.Name);
}
}
}
Demo: https://dotnetfiddle.net/Xuj7b6
add a comment |
Here's a slightly optimized version.
I've added getters to pull a Named Value and a full Name, e.g "Ace Of Spades". And used an Enum
for suites.
Then I've used a single loop with the modulo operator (%
) and Math.Floor
to populate the deck. This is where the enum comes into play as it is trivial to work with enums and ints.
public class Card
{
public enum Suites
{
Hearts = 0,
Diamonds,
Clubs,
Spades
}
public int Value
{
get;
set;
}
public Suites Suite
{
get;
set;
}
//Used to get full name, also useful
//if you want to just get the named value
public string NamedValue
{
get
{
string name = string.Empty;
switch (Value)
{
case (14):
name = "Ace";
break;
case (13):
name = "King";
break;
case (12):
name = "Queen";
break;
case (11):
name = "Jack";
break;
default:
name = Value.ToString();
break;
}
return name;
}
}
public string Name
{
get
{
return NamedValue + " of " + Suite.ToString();
}
}
public Card(int Value, Suites Suite)
{
this.Value = Value;
this.Suite = Suite;
}
}
public class Deck
{
public List<Card> Cards = new List<Card>();
public void FillDeck()
{
public void FillDeck()
{
//Can use a single loop utilising the mod operator % and Math.Floor
//Using divition based on 13 cards in a suited
for (int i = 0; i < 52; i++)
{
Card.Suites suite = (Card.Suites)(Math.Floor((decimal)i/13));
//Add 2 to value as a cards start a 2
int val = i%13 + 2;
Cards.Add(new Card(val, suite));
}
}
}
public void PrintDeck()
{
foreach(Card card in this.Cards)
{
Console.WriteLine(card.Name);
}
}
}
Demo: https://dotnetfiddle.net/Xuj7b6
add a comment |
Here's a slightly optimized version.
I've added getters to pull a Named Value and a full Name, e.g "Ace Of Spades". And used an Enum
for suites.
Then I've used a single loop with the modulo operator (%
) and Math.Floor
to populate the deck. This is where the enum comes into play as it is trivial to work with enums and ints.
public class Card
{
public enum Suites
{
Hearts = 0,
Diamonds,
Clubs,
Spades
}
public int Value
{
get;
set;
}
public Suites Suite
{
get;
set;
}
//Used to get full name, also useful
//if you want to just get the named value
public string NamedValue
{
get
{
string name = string.Empty;
switch (Value)
{
case (14):
name = "Ace";
break;
case (13):
name = "King";
break;
case (12):
name = "Queen";
break;
case (11):
name = "Jack";
break;
default:
name = Value.ToString();
break;
}
return name;
}
}
public string Name
{
get
{
return NamedValue + " of " + Suite.ToString();
}
}
public Card(int Value, Suites Suite)
{
this.Value = Value;
this.Suite = Suite;
}
}
public class Deck
{
public List<Card> Cards = new List<Card>();
public void FillDeck()
{
public void FillDeck()
{
//Can use a single loop utilising the mod operator % and Math.Floor
//Using divition based on 13 cards in a suited
for (int i = 0; i < 52; i++)
{
Card.Suites suite = (Card.Suites)(Math.Floor((decimal)i/13));
//Add 2 to value as a cards start a 2
int val = i%13 + 2;
Cards.Add(new Card(val, suite));
}
}
}
public void PrintDeck()
{
foreach(Card card in this.Cards)
{
Console.WriteLine(card.Name);
}
}
}
Demo: https://dotnetfiddle.net/Xuj7b6
Here's a slightly optimized version.
I've added getters to pull a Named Value and a full Name, e.g "Ace Of Spades". And used an Enum
for suites.
Then I've used a single loop with the modulo operator (%
) and Math.Floor
to populate the deck. This is where the enum comes into play as it is trivial to work with enums and ints.
public class Card
{
public enum Suites
{
Hearts = 0,
Diamonds,
Clubs,
Spades
}
public int Value
{
get;
set;
}
public Suites Suite
{
get;
set;
}
//Used to get full name, also useful
//if you want to just get the named value
public string NamedValue
{
get
{
string name = string.Empty;
switch (Value)
{
case (14):
name = "Ace";
break;
case (13):
name = "King";
break;
case (12):
name = "Queen";
break;
case (11):
name = "Jack";
break;
default:
name = Value.ToString();
break;
}
return name;
}
}
public string Name
{
get
{
return NamedValue + " of " + Suite.ToString();
}
}
public Card(int Value, Suites Suite)
{
this.Value = Value;
this.Suite = Suite;
}
}
public class Deck
{
public List<Card> Cards = new List<Card>();
public void FillDeck()
{
public void FillDeck()
{
//Can use a single loop utilising the mod operator % and Math.Floor
//Using divition based on 13 cards in a suited
for (int i = 0; i < 52; i++)
{
Card.Suites suite = (Card.Suites)(Math.Floor((decimal)i/13));
//Add 2 to value as a cards start a 2
int val = i%13 + 2;
Cards.Add(new Card(val, suite));
}
}
}
public void PrintDeck()
{
foreach(Card card in this.Cards)
{
Console.WriteLine(card.Name);
}
}
}
Demo: https://dotnetfiddle.net/Xuj7b6
edited Nov 20 '18 at 0:47
answered Nov 20 '18 at 0:20
Jon PJon P
11.8k73459
11.8k73459
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53383468%2fcreating-a-deck-of-cards-in-c-sharp%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
7
First and foremost, unless you implement
Card.ToString
(i.e. override the default implementation), you won't get a meaningful output, because .NET doesn't know how to convert a "card" into astring
.– Groo
Nov 19 '18 at 22:23
So I just need to do an override on the ToString method?
– MattCV27
Nov 19 '18 at 22:24
2
also, public Card(string input) doesn't actually appear to be doing anything
– Broom
Nov 19 '18 at 22:25
Yes, that's right.
– Adrian
Nov 19 '18 at 22:25
1
That's the first thing to do if you want to get a textual representation, yes. Other remarks would be: 1) if you are using
static
methods, you are usually doing something wrong. In this case, it means that only a single deck can exist in your app. 2) type ofdeck
should beCard
orList<Card>
, notObject
. 3)public Card(string input)
is a constructor, which doesn't do anything.– Groo
Nov 19 '18 at 22:26