1

*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(rank, 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?

Shashi
  • 12,487
  • 17
  • 65
  • 111
Steller
  • 308
  • 2
  • 8
  • 26
  • The O'Reilly head Start Java book has a good solution for this. – Andrew Sledge Jan 27 '10 at 23:46
  • do you have a link? is it based on what i want to accomplish? Head first java? – Steller Jan 27 '10 at 23:55
  • What is the purpose of the numerical value you are assigning to each card, e.g. Eight of spades(8), ten of hearts(40)? You realize that the way you're calculating this value, the five of hearts and the ten,jack,queen,king of clubs all have the same value, 20... – shoover Jan 28 '10 at 00:29

1 Answers1

3

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.

Shashi
  • 12,487
  • 17
  • 65
  • 111
danben
  • 80,905
  • 18
  • 123
  • 145
  • danben- i changed a few things.. check out original code.. i cant get value. – Steller Jan 28 '10 at 04:24
  • The problem is that `getSuitpoints()` belongs to `Suit` and `getRankpoints()` belongs to `Rank`. You can't invoke these methods on a `Cards` object. – danben Jan 28 '10 at 13:59
  • If i move this out of the enum i get a return type required private Rank(int points) Where do i put it? – Steller Jan 28 '10 at 20:01
  • No, leave those methods inside their enums. I am saying that you are trying to invoke the method on a different object than the one that declares it, and you can't do that. – danben Jan 28 '10 at 20:03
  • In main() how do i print the values of the enums? when i try card.getRankpoints() I get an error cant find method getRankpoints() – Steller Jan 28 '10 at 21:26
  • I see what your saying but how do i make an object to the enum so i can get the values? – Steller Jan 28 '10 at 21:35
  • It would be like `Rank.TEN.getRankpoints()` or `Suit.CLUBS.getSuitpoints()` – danben Jan 28 '10 at 21:38
  • Hey, i got it working... what i did was for(Cards.Suit a : Cards.Suit.values()) { System.out.println( a.getSuitpoints()); } – Steller Jan 28 '10 at 22:00
  • Exactly - `getSuitpoints()` belongs to the `Suit` class, not the `Cards` class. – danben Jan 28 '10 at 22:01