Table-like (database-like) object

S

Snaggy

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
 
J

Jeroen

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
 
Z

zacks

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

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.
 
S

Snaggy

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

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.- Hide quoted text -

- 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
 
J

j1mb0jay

Snaggy said:
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
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.- Hide quoted text -

- 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

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top