# Card Game

• 02-04-2013, 02:13 PM
HarleyRowland
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.
• 02-04-2013, 02:52 PM
PhHein
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.
• 02-04-2013, 05:21 PM
HarleyRowland
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?
• 02-04-2013, 05:23 PM
PhHein
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 ;)
• 02-04-2013, 05:30 PM
HarleyRowland
Re: Card Game
Okay I will have a look at that now :) Thanks a lot for your help!
• 02-04-2013, 05:42 PM
HarleyRowland
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?
• 02-04-2013, 05:57 PM
Tolls
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.
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:
Code:

```for (Suit suit : Suit.values()) { // You'll need to check the method call. }```
• 02-05-2013, 01:44 AM
HarleyRowland
Re: Card Game
Okay thank you for all of your help! I think i might be able to do this assignment now! :)