Results 1 to 8 of 8
Thread: Card Game
- 02-04-2013, 01:13 PM #1
Member
- Join Date
- Feb 2013
- Posts
- 26
- Rep Power
- 0
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, 01:52 PM #2
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
- 02-04-2013, 04:21 PM #3
Member
- Join Date
- Feb 2013
- Posts
- 26
- Rep Power
- 0
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, 04:23 PM #4
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
- 02-04-2013, 04:30 PM #5
Member
- Join Date
- Feb 2013
- Posts
- 26
- Rep Power
- 0
Re: Card Game
Okay I will have a look at that now :) Thanks a lot for your help!
- 02-04-2013, 04:42 PM #6
Member
- Join Date
- Feb 2013
- Posts
- 26
- Rep Power
- 0
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, 04:57 PM #7
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
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.
Something like that (note, this was done in the forum and not in an IDE, so may well not compile).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; } }
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.
- 02-05-2013, 12:44 AM #8
Member
- Join Date
- Feb 2013
- Posts
- 26
- Rep Power
- 0
Similar Threads
-
Need a Basis for Card Game
By haydenholligan in forum New To JavaReplies: 1Last Post: 12-11-2012, 06:48 PM -
Networked Card Game.
By T-Prime in forum NetworkingReplies: 9Last Post: 07-26-2011, 12:22 AM -
Card Game
By abby0910 in forum New To JavaReplies: 1Last Post: 07-24-2010, 12:38 AM -
please help me with this card game
By noobinoo in forum New To JavaReplies: 13Last Post: 03-28-2010, 02:07 PM -
card game Rummy
By javafox in forum New To JavaReplies: 4Last Post: 03-14-2009, 03:53 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks