This week's giveaway is in the EJB and other Java EE Technologies forum.
We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes cards Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "cards" Watch "cards" New topic
Author

cards

jesse johnson
Greenhorn

Joined: Jun 14, 2001
Posts: 6
i'm trying to define a deck of cards, and getting more confused by the minute. here is what little i have so far:

i would assume i still need to set the suit and value, but like i said i'm getting very confused - can someone point me in the right direction?
(edited by Cindy to format code)

[This message has been edited by Cindy Glass (edited June 26, 2001).]
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
Perhaps a static array containing the cards that are valid?


"JavaRanch, where the deer and the Certified play" - David O'Meara
Dave Vick
Ranch Hand

Joined: May 10, 2001
Posts: 3244
Jesse
To elaborate on what Cindy said how about a Deck class that has as a member a static array of Card objects. You could use a static initalizer to initialize the array to fill in all of the cards. You could also have a set of static final variables for each suit: Deck.SPADES=0, Deck.HEARTS=1, and so on. Then some methods like shuffle() and deal().
That should gie you a good starting point, if you have any questions let us know. Or is there something specific that is giving you trouble?
Dave


Dave
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4419
    
    5

See http://developer.java.sun.com/developer/Books/shiftintojava/page1.html
This was written by Joshua Bloch, author of "Effective Java". Bloch writes about the typesafe enumeration pattern and uses cards as an example.
Below is some code that shows a way to "shuffle" a deck of cards.
<pre>
Card[] deck = new Card[52]; // standard deck

... // populate deck with instances of Card

// shuffle the deck
for (int i = deck.length - 1; i > 0; i--)
{
int nextCard = (int)(Math.random() * i);
Card temp = deck[i];
deck[i] = deck[nextCard];
deck[nextCard] = temp;
}
</pre>

[This message has been edited by JUNILU LACAR (edited June 26, 2001).]


Junilu - [How to Ask Questions] [How to Answer Questions]
jesse johnson
Greenhorn

Joined: Jun 14, 2001
Posts: 6
things like shuffling the deck are a little too advanced for a newbie like me (for now ). i'm simply trying to write a class Card to define a card for a game, then put a few objects in main to show it works. i THINK i have the constructor figured, but i'm struggling with setting the suit and setting the number.
Guy Reynolds
Ranch Hand

Joined: Oct 27, 2000
Posts: 61
Jesse, assuming the code snippet you posted is contained within a class declaration, the only problem I see is what I'm thinking is a typo just before the getSuit method. The posted snippet doesn't close the constructor block...
sb
public Card(int theValue, String theSuit) {
value = theValue;
suit = theSuit;
} <----- this is the missing curly brace that closes the block
public String getSuit() {
return suit;
}
public int getValue() {
return value;
}
public String display() {
return getValue() + " of " + getSuit();
}
of course, this should all be contained within a class's block, something like:
public class Card {
private int value;
private String suit;
public Card(int theValue, String theSuit) {
value = theValue;
suit = theSuit;
}
public String getSuit() {
return suit;
}
public int getValue() {
return value;
}
public String display() {
return getValue() + " of " + getSuit();
}
}

with this class, you should be able to construct different card objects in your main method and display them:
something like:
public class DeckOfCards {
public static void main( String[] args ) {
Card aceOfSpades = new Card( 0, "Spade" );
Card oneOfSpades = new Card( 1, "Spade" );
System.out.println( aceOfSpades.display() );
System.out.println( oneOfSpades.display() );
}
}
p.s. these would be two seperate files, Card.java and DeckOfCards.java. You could put them both in one file by removing the "public" from the Card class ex.
public class DeckOfCards{
.
.
.
}
class Card{
.
.
.
}
save as file DeckOfCards.java

[This message has been edited by Guy Reynolds (edited June 26, 2001).]
jesse johnson
Greenhorn

Joined: Jun 14, 2001
Posts: 6
thanks for all your help, i think its making sense now... one last question, can i modify this so it will display "Three of Clubs"?
Guy Reynolds
Ranch Hand

Joined: Oct 27, 2000
Posts: 61
Yes you can. How depends on a few things. You could just change the variable "value" to a String, but then you wouldn't be able to compare them for equality as easily. You could put a switch in the body of the display method that would return different strings depending on the value returned from getValue. Or you could write another function that returned a string based upon the int that was passed to it and use this function inside the display function. And I'm sure there are tons of other solutions. The best (object oriented-wise) of the ones I've listed, is the latter.
Something like:
private String getName( int value ) {
switch( value ) {
case 0:
return "Ace";
case 1:
return "One";
.
.
.
}
}
public String display() {
return getName( getValue() ) + " of " + getSuit();
}
p.s. actually an even better idea is no display method at all... instead override the toString method, using the same body as the display method. Then you don't have to call
System.out.println( aceOfSpades.display() );
you can call
System.out.println( aceOfSpades );
and get the same results.
[This message has been edited by Guy Reynolds (edited June 26, 2001).]
Mikael Jonasson
Ranch Hand

Joined: May 16, 2001
Posts: 158
Maybe it would be a good idea to put in a range-check in the constructor, that is making sure that you create a valid card (make sure that the suite excists, and that the value is between 1 and 13).
Have you considered putting in wild-cards?
/Mike
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: cards
 
Similar Threads
Error
How to extend ImageIcon class effectively
Need help writing constructor
class question
How to test my code for proper function?