CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 14 of 14

Thread: loops

  1. #1
    Join Date
    Oct 2010
    Posts
    11

    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.

  2. #2
    Join Date
    Jun 2008
    Posts
    2,477

    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.

  3. #3
    Join Date
    Oct 2010
    Posts
    11

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

  4. #4
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: loops

    There are 52 cards in a Deck. 13 x 4

    How do you get that?
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  5. #5
    Join Date
    Oct 2010
    Posts
    11

    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.

  6. #6
    Join Date
    Apr 2004
    Location
    England, Europe
    Posts
    2,492

    Re: loops

    Define 'does not work'.
    My hobby projects:
    www.rclsoftware.org.uk

  7. #7
    Join Date
    Oct 2010
    Posts
    11

    Re: loops

    foreach (char suit in {'c','s','h','d'}) gives a compiler error.

  8. #8
    Join Date
    Jan 2010
    Posts
    1,133

    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.

  9. #9
    Join Date
    Oct 2010
    Posts
    11

    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.

  10. #10
    Join Date
    Jun 2008
    Posts
    2,477

    Re: loops

    Quote Originally Posted by elishac View Post
    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.

  11. #11
    Join Date
    Jan 2010
    Posts
    1,133

    Re: loops

    Quote Originally Posted by elishac View Post
    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?
    Quote Originally Posted by BigEd781 View Post
    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.

    Quote Originally Posted by elishac View Post
    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.

  12. #12
    Join Date
    Jan 2010
    Posts
    1,133

    Re: loops

    Quote Originally Posted by TheGreatCthulhu View Post
    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.

  13. #13
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    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
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  14. #14
    Join Date
    Oct 2010
    Posts
    11

    Re: loops

    Thanks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured