Results 1 to 8 of 8

Thread: Card Game

  1. #1
    HarleyRowland is offline Member
    Join Date
    Feb 2013
    Posts
    26
    Rep Power
    0

    Default Card Game

    I have to make a card game for my university assignment. I need to create two enum classes (one called suit and one called number) and a card class which creates an instance of one of each of the enum classes, which i have done. I have made a returnSuit and a returnNumber class. What i am confused about is the next step, I must create a class which will form a deck of the 52 possible cards and I am unsure of how to implement this. I thought that I would have to create an array of 52 cards but I don't really know how to do this. Is there some kind of loop I can make to do this? Or do i have to explicitly state all 52 possible cards?

    Thanks for any help.

  2. #2
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default Re: Card Game

    You can loop over the suit enum entires (4 times) and within that loop you loop over the number enum ( 13 times) entries.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  3. #3
    HarleyRowland is offline Member
    Join Date
    Feb 2013
    Posts
    26
    Rep Power
    0

    Default Re: Card Game

    Okay thank you, how would i iterate through the enum?

    like

    for(Suit suit : Suit) {
    ...
    }

    ? Thanks. Is there anywhere I can learn about enum types in more detail?

  4. #4
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default Re: Card Game

    Always have a look at the Oracle tutorials.
    Enum Types (The Java™ Tutorials > Learning the Java Language > Classes and Objects)

    And google also finds the one or the other tutorial ;)
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  5. #5
    HarleyRowland is offline Member
    Join Date
    Feb 2013
    Posts
    26
    Rep Power
    0

    Default Re: Card Game

    Okay I will have a look at that now :) Thanks a lot for your help!

  6. #6
    HarleyRowland is offline Member
    Join Date
    Feb 2013
    Posts
    26
    Rep Power
    0

    Default Re: Card Game

    Wow, thank you! There is the exact code in that tutorial lol, I want to understand it though. Just one more question about enum types. Do you treat a type in an enum as a number? So if I have HEART, SPADE, CLUB, DIAMOND they would be 1, 2, 3, 4? So i can say suit.HEART++ within the loop and that would move onto the SPADE?

  7. #7
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,945
    Rep Power
    19

    Default Re: Card Game

    Nope.
    This isn't C++ where enums are simply names overlaid over a number.
    Enums are full classes, with a limited number of instances.

    Now, you could provide a next() method to your Suit class, which would have the same effect of ordering.
    Java Code:
    public enum Suit {
       HEART(SPADE), SPADE(CLUB), CLUB(DIAMOND), DIAMOND(null); 
       private Suit next;
    
       private Suit(Suit next) {
          this.next = next;
       }
    
       public Suit next() {
          return next;
       }
    }
    Something like that (note, this was done in the forum and not in an IDE, so may well not compile).

    That's if you want an order.

    Of course you can simply iterate:
    Java Code:
    for (Suit suit : Suit.values()) { // You'll need to check the method call.
    }
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  8. #8
    HarleyRowland is offline Member
    Join Date
    Feb 2013
    Posts
    26
    Rep Power
    0

    Default Re: Card Game

    Okay thank you for all of your help! I think i might be able to do this assignment now! :)

Similar Threads

  1. Need a Basis for Card Game
    By haydenholligan in forum New To Java
    Replies: 1
    Last Post: 12-11-2012, 06:48 PM
  2. Networked Card Game.
    By T-Prime in forum Networking
    Replies: 9
    Last Post: 07-26-2011, 12:22 AM
  3. Card Game
    By abby0910 in forum New To Java
    Replies: 1
    Last Post: 07-24-2010, 12:38 AM
  4. please help me with this card game
    By noobinoo in forum New To Java
    Replies: 13
    Last Post: 03-28-2010, 02:07 PM
  5. card game Rummy
    By javafox in forum New To Java
    Replies: 4
    Last Post: 03-14-2009, 03:53 PM

Posting Permissions

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