Java class card enum example. REVISED
*Any help is much appreciated *
I am using the class card example from java website to try to build a game.
http://java.sun.com/j2se/1.5.0/docs/guide/language/enums.html
I want to assign the suits and ranks values. I am not sure how to do that..
For suit, What i want to do is assign Heart = 4 diamond = 3, club =2, spade = 1
for rank, ace = 11 jack,queen,king = 10
2-10 the value of the card..the program accepts user input as arguments for number of hands and number of cards per hand.. like this: $ java Deal 4 5
so then I want it to print Eight of spades(8), ten of hearts(40)
based off from the values.. example spade = 1 * 8 hearts = 4 * 10
I can get it to print the hand just not the values...
Example of my Current output scaled down:
FIVE of CLUBS(0),
DEUCE of SPADES(0),
SEVEN of SPADES(0),
TEN of SPADES(0),
THREE of HEARTS(0),
FOUR of SPADES(0),
THREE of DIAMONDS(0),
[TEN of SPADES, THREE of HEARTS, FOUR of SPADES, THREE of DIAMONDS]
[FOUR of CLUBS, FIVE of CLUBS, DEUCE of SPADES, SEVEN of SPADES]
[QUEEN of HEARTS, SIX of HEARTS, FOUR of HEARTS, KING of DIAMONDS]
C:\Java\a02>
Here is the code for the program in two different classes
import java.util.*;
public class Cards {
public enum Rank {
DEUCE(2), THREE(3), FOUR(4), FIVE(5), SIX(6), SEVEN(7), EIGHT(8), NINE(
9), TEN(10), JACK(10), QUEEN(10), KING(10), ACE(11);
private int Rankpoints;
Rank(int points) {
this.Rankpoints = points;
}
public int getRankpoints() {
return this.Rankpoints;
}
}
public enum Suit {
CLUBS(2), DIAMONDS(3), HEARTS(4), SPADES(1);
private int Suitpoints;
Suit(int points) {
this.Suitpoints = points;
}
public int getSuitpoints() {
return this.Suitpoints;
}
}
private final Rank rank;
private final Suit suit;
private Cards(Rank rank, Suit suit) {
this.rank = rank;
this.suit = suit;
}
public Rank rank() {
return this.rank;
}
public Suit suit() {
return this.suit;
}
public String toString() {
return rank + " of " + suit;
}
private static final List<Cards> protoDeck = new ArrayList<Cards>();
// Initialize prototype deck
static {
for (Suit suit : Suit.values())
for (Rank rank : Rank.values())
protoDeck.add(new Cards(ra开发者_StackOverflownk, suit));
}
public static ArrayList<Cards> newDeck() {
return new ArrayList<Cards>(protoDeck); // Return copy of prototype deck
}
}
and here is the main
import java.util.*;
public class Deal {
public static void main(String args[]) {
int numHands = Integer.parseInt(args[0]);
int cardsPerHand = Integer.parseInt(args[1]);
List<Cards> deck = Cards.newDeck();
Collections.shuffle(deck);
for (Cards card : deck) {
System.out.println(card.rank() + " of " + card.suit() + "("
+ card.getSuitpoints() + ")" + ",");
}
for (int i = 0; i < numHands; i++)
System.out.println(deal(deck, cardsPerHand));
}
public static ArrayList<Cards> deal(List<Cards> deck, int n) {
int deckSize = deck.size();
List<Cards> handView = deck.subList(deckSize - n, deckSize);
ArrayList<Cards> hand = new ArrayList<Cards>(handView);
handView.clear();
return hand;
}
}
when i try to compile i get an error.. for
card.getSuitpoints()
error: cannot find symbol: method getSuitpoints()
i find that odd because
card.getRankpoints() compiles.. is it the way i am putting it into the enum?
getRankpoints() returns zero. whats wrong?
The reason you are seeing duplicates in your deck is because you are iterating over the cards twice.
for (Cards suit : deck) // ********* here is where i print the deck ********
{
for (Cards rank : deck) {
System.out.println(rank.rank() + " of " + suit.suit() + ",");
}
}
Assuming your deck generator works, it would be sufficient to iterate once:
for (Cards card : deck)
{
System.out.println(card.rank() + " of " + card.suit() + ",");
}
Also, Card
might be a better choice of name than Cards
for a class that represents a card.
To get the value, you need to add a method that returns it.
Rank(int points)
{
this.Rankpoints = points;
}
public int getRankpoints() {
return this.Rankpoints;
}
Then you can call that when you want to print the value.
精彩评论