-
October 20th, 2010, 01:49 PM
#1
loops
hello,
in your opinion what's the easiest, fastest and most elegant way in c# of doing the same procedure from character '1' to character '10'.
Same question for the following set of letters : 'h', 'e', 'l', 'o'.
Thanks.
-
October 20th, 2010, 01:52 PM
#2
Re: loops
Code:
void DoSomething( List<char> chars )
{
foreach( char c in chars )
{
// do stuff with 'c'
}
}
Without more information as to what you are actually trying to accomplish that is the best I can offer.
-
October 20th, 2010, 02:08 PM
#3
Re: loops
I'm just a beginner so the solution is probably quite simple.
I just want to generate a deck of cards, from 2 to ace and for each suit, without having to write them all down.
Something like (with card being a class previously created)
List<card> deck = new List<card>();
foreach (char suit in {'c','s','h','d'}){
for (char number = '1' ; c < '10' ; c++){
deck.Add(card(number,suit);
}
foreach (char faceCard in {'j','q','k'}){
deck.Add(card(faceCard ,suit);
}
}
Except that this doesn't work in C#.
-
October 20th, 2010, 10:05 PM
#4
Re: loops
There are 52 cards in a Deck. 13 x 4
How do you get that?
-
October 21st, 2010, 12:10 AM
#5
Re: loops
Well, 52 = 4* (10+3), right?
that's what I did.
The question is about the foreach loop, it doesn't seem to work.
-
October 21st, 2010, 07:06 AM
#6
Re: loops
Define 'does not work'.
-
October 21st, 2010, 10:10 AM
#7
Re: loops
foreach (char suit in {'c','s','h','d'}) gives a compiler error.
-
October 21st, 2010, 11:26 AM
#8
Re: loops
In C#, you have to write something like foreach (char suit in new char[]{'c','s','h','d'})
However, the char type is not actually an integral type like it is in C++, so you can't treat them as numbers in your loop - at least not without some explicit casts.
But why not use integers in the first place?
I also wouldn't use the char type to represent a suit - an enum would do much better.
BTW, unless you made a few typos, the compiler should also complain about the few ')' that are missing.
And, you can use the [code][/code] tags to preserve formatting.
-
October 21st, 2010, 02:22 PM
#9
Re: loops
Thanks for your advices. Sorry about the typos.
I didn't understand how you plan to use integers to represent the card values.
I mean, for all the cards between 1 and 10, no problem, but what about jack, queen, king?
An enum wouldn't work either because an identifier can't be a number.
-
October 21st, 2010, 02:53 PM
#10
Re: loops
Originally Posted by elishac
An enum wouldn't work either because an identifier can't be a number.
Uhhh, what? Of course it can
Code:
enum Example
{
Example1 = 0,
Example2 = 0x1,
Example3 = 0x2,
Example4 = 0x4,
Example5 = 0x8
}
Of course, the problem you will run into is that all of the face cards should be 10. To solve that, a card can be a class which holds an enum value specifying which card it is, and which exposes a property giving its value. Either way, using char's is a bad idea.
-
October 21st, 2010, 05:20 PM
#11
Re: loops
Originally Posted by elishac
I didn't understand how you plan to use integers to represent the card values.
I mean, for all the cards between 1 and 10, no problem, but what about jack, queen, king?
Originally Posted by BigEd781
Of course, the problem you will run into is that all of the face cards should be 10. To solve that, a card can be a class which holds an enum value specifying which card it is, and which exposes a property giving its value. Either way, using char's is a bad idea.
Hmm... I just realized there's an interesting cultural difference I was not aware of; and incidentally, thanks to that, this is a great chance to talk about how we abstract the real world when we program.
There are some card games that don't treat the jack, the queen and the king card as valued 10. Where I'm from, it is universally accepted that the cards in a deck generally have the following values:
- A: 1 or 11
- 2-10: 2-10
- (A: 1 or 11)
- J: 12
- Q: 13
- K: 14
It is, of course, recognized that these values can be modified by the game rules. The point is, if you said "14", everyone would know you meant "K".
Why is this interesting to a C# coder? Well, this way the whole thing is abstracted to numbers, and the whole suit is treated uniformly - every card is treated the same way.
However, I understand that, where you're from, something like this might be considered non-standard.
I would go with what BigEd781 suggested - create a class that would represent a card. This way you can standardize and hide the implementation details from the user, and expose whatever properties and methods are suitable. The internal representation can be converted to proper presentation (string? image?) when required.
Originally Posted by elishac
An enum wouldn't work either because an identifier can't be a number.
My original idea was to use an enum to describe the suit.
Code:
enum Suit
{
Clubs,
Spades,
Hearts,
Diamonds
}
However, you if the integer based approach above is inconvenient, you can also use a second enum for the values - which is just another approach that enables you to threat all the cards in a suit in a same fashion.
Then you can provide a value property that would return the actual value of the card. An OO approach can utilize this to make the value returned by the property be dependent on the actual game being played (or on the rules of the game, to be more precise), but this is slightly more advanced.
You can even go berserk and represent the whole deck using an enumeration. Depending on what you need to achieve, this could be even more convenient; for example, if the underlying type is long, you can use flags to represent every card in a deck, and use the remaining bits for special data, and have all the card related variables with the fixed size of a long, which might be convenient if you, say, want to send the card data over a network.
These are some of the variants, and as you can see, there are several factors that can influence your choice - but the most important one is: how is the card class going to be used (both by your class, and by client classes)?
Last edited by TheGreatCthulhu; October 21st, 2010 at 05:24 PM.
-
October 21st, 2010, 05:30 PM
#12
Re: loops
Originally Posted by TheGreatCthulhu
However, the char type is not actually an integral type like it is in C++, so you can't treat them as numbers in your loop - at least not without some explicit casts.
I need to correct myself: I said "the char type is not actually an integral type like it is in C++" - actually, it is. I was wrong.
The point is, unlike C++, C# treats its char as a full-fledged type, static and instance methods and all. It allows implicit conversions from, but not to the char type.
It may seem strange to you, but this is actually a good thing.
To learn more see this.
-
October 21st, 2010, 06:58 PM
#13
Re: loops
Well, for most games here,
Code:
# J: 10
# Q: 10
# K: 10
# A : 10 OR 1
as they are all FACE CARDS, except that the ACE can be high or low
-
October 22nd, 2010, 04:50 AM
#14
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|