Hi,
I' trying to write a program (to learn the language) that handles a
deck of cards. Each card has an ID, a string name, a position, an
order (for shuffling) and as many other optional properties we might
want to assign it.
To store the deck's data I'm using right now a string[ , ] array, but
this is proving difficult.
If I want to select the cards in a position I have to do a foreach,
and that's ok. But if I want to sort let's say by positon and subsort
by order, it gets difficult..
I thought I'd me nice to work on this data as I would on a database
table, doing things like select where, order by etc etc
I know it exists a LINQ thing that might serve the purpose, but the
example I found don't quite fit what I need..
any suggestions?
thanks
Luca
Jeroen - 11 Jan 2008 21:05 GMT
Luca,
I mainly work with .NET 2, so for LINQ advice you should count on the
guru's here.
However, concerning the approach your taking: start the same as if you
were architecting a database you'd start with a schema. Similarly for
your code, start with creating interfaces to show relationships
between your objects. For example:
/*****************************************/
interface ICard {
// jack, 2, 3, ace, etc
string name;
// clubs, spades, diamonds, hearts
MyCardColorEnum color;
// object reference to the deck
IDeck deck;
}
interface IDeck {
// cards in the deck, ordered
IDictionary<int, ICard> listOfCards;
// gets a random card
ICard getRandomCard();
// gets card from a specific location
ICard getCard(int location);
// gets the top card of the deck
ICard getCard();
}
/*****************************************/
Good luck with your study!
Regards,
Jeroen
zacks@construction-imaging.com - 11 Jan 2008 21:41 GMT
> Hi,
>
[quoted text clipped - 19 lines]
>
> Luca
Providing you are using .NET 2.0 or later, I would use a generic list
of a class object of a class that defines each card.
Snaggy - 11 Jan 2008 22:25 GMT
On Jan 11, 10:41 pm, za...@construction-imaging.com wrote:
> > Hi,
>
[quoted text clipped - 24 lines]
>
> - Show quoted text -
that's it!! I did it with a List<card> after having defined a card
class, and used linq queries, it works perfectly! It really is an
amazing feature this linq, and quite powerful
bye
j1mb0jay - 11 Jan 2008 22:38 GMT
> On Jan 11, 10:41 pm, za...@construction-imaging.com wrote:
>>
[quoted text clipped - 25 lines]
>
> bye
If you are learning the language I would try and build your own data
structure. A linked list that has pointers to the next and previous
nodes in the list would allow for card games that may contain more than
one pack of cards. Or even a binary tree which in turn when populated
would sort the cards into order for you.
j1mb0jay