123
-=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- (c) WidthPadding Industries 1987 0|429|0 -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=-
Socoder -> C/C++/C#/Other -> Self Teaching Help!

Sun, 17 Apr 2011, 21:21
killa57
Hi im in need of some help i have a programming project from a book and cant figure it out? it asks to make a class named Card that represents like a standard playing card. And every card have a suit and a card number. and the program deals five random cards...... So im new to this how would i go about starting this ? I've created a class called Card i feel like im not even going about this right it has me confused even when i read the chapter more im like umm what? :S So does it look right so far? how would i define the card name and suits? in the program or the Class?

This is the Class that i have so far



This is the program i have so far




Mon, 18 Apr 2011, 03:19
Jayenkai
When I used to deal with card things, the whole Suit/Value thing used to annoy me like MAD. I'm sure there's an easy way to deal with Suit/Value, but at some point you just have to give up!

Instead, don't think of 4 small decks of cards.
When you're playing with cards, you instead clump them all together, to make one big deck.

Try it like that..

Instead of each card having 2 values (Suit, Value), give each card only one (card number).
Stick with this single number, right up until the point that you need to know the Suit/Value, at which point it's just a case of division to work them out.

Assuming Card = 0->51

Suit = floor(Card/13); // Will return a value 0->3
Value = (Card % 13); // Will return a value 0->13

Then simply draw/write whichever is associated at the time.

By sticking with a full deck, as opposed to having 4 groups, you'll find it much easier to do card-like things with them. Shuffling is WAY easier like this!


As for whether it's Class or Program, I'm not the world's greatest OOP user, but if Class is the theme of this lesson, then try to do as much as you possibly can within the class.
The more that happens in the class, the better you'll learn to do things that way.

-=-=-
''Load, Next List!''
Mon, 18 Apr 2011, 04:00
Afr0
There's nothing wrong with your program so far, at least. Except you should be consistent when naming variables, and there's no need to be using 'System.Linq'.
Also don't call your argument 'suits'. Call it 'suit'. You're passing a string, not an array of strings! Be consistent, don't confuse yourself and anyone else reading your code!

Edit: I also noticed you haven't defined an access level for your 'generator' variable. C# defaults to 'private' when you have not set an access level, but you should get used to setting access levels. It makes your code easier to understand and read, and will help you in the future (IIRC C++ (and other OOP languages) requires you to explicity set your access levels).

-=-=-
Afr0 Games

Project Dollhouse on Github - Please fork!
Mon, 18 Apr 2011, 06:09
Evil Roy Ferguso
The lovely thing about coding is that you'll ask 10 people a question and get 23 different answers.

For what it's worth:

- Cards shouldn't randomly set their suit / value in the constructor -- this is kind of like if at a playing card factory, each card off the printing press had a random suit and value. Instead, you'll want to tell each card what suit / value it has when you create it.

- After you've created all the cards, shuffle the list. A Knuth-Fischer-Yates shuffle is both effective and easy to implement. Creating the whole deck and shuffling is necessary because otherwise you could easily end up with more than one of the same card.

- For the moment, I'd just go with an array lookup for the number ( static readonly string[] _valueNames = new[] { "Ace", "Deuce", ... "Queen", "King" } -- might get fancy later on. If your Suit is an Enum, just a ToString() will work for now.

This is about what I arrived at: