File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

cards

 
jesse johnson
Greenhorn
Posts: 6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8521
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Perhaps a static array containing the cards that are valid?
 
Dave Vick
Ranch Hand
Posts: 3244
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Junilu Lacar
Bartender
Pie
Posts: 6529
21
Java Linux Mac Scala Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).]
 
jesse johnson
Greenhorn
Posts: 6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 61
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 61
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 158
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic