Steller
Steller

Reputation: 308

Java's Card Class example - printing enum values

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 am taking in the number of hands and the number of cards per hands as arguments

like: java -jar a02.jar 4 3

I have added values to the ranks and suits. I am printing the DECK then the HAND...

What i am trying to do is print the deck first with their values times together. for example: Ten of Hearts

Ten = 10 Hearts = 4

So, i print out Ten of Hearts(40)

When i print the deck, I am having a problem getting them to print on one line by the number of cards per hand. like:

eight of spades(8), ten of hearts(40), deuce of hearts(8)

What can i do to fix this?

My other issue is that is there a better way of retrieving the values of the cards than what i am doing? could i use some type of compare?

because when i print out the hand after the deck i am having a hard time getting the values and the hand right ( so i commented out the code )

I also commented the part of code in main() where i am retrieving the values. how could i do this differently?

Keep in mind i want the values printed also with the Hand like i am doing with deck

I will shorted the output..

here is the output:

C:\Java\a02>java -jar a1.jar 3 4
THREE of SPADES(3),      // i would like this to print out like hand does below
FIVE of HEARTS(20),
DEUCE of DIAMONDS(6),
KING of CLUBS(20),
DEUCE of HEARTS(8),
TEN of HEARTS(40),
JACK of HEARTS(40),
ACE of HEARTS(44),
NINE of CLUBS(18),
EIGHT of SPADES(8),
QUEEN of SPADES(10),
KING of HEARTS(40),
NINE of SPADES(9),
SEVEN of SPADES(7),
SEVEN of HEARTS(28),
EIGHT of HEARTS(32),
TEN of SPADES(10),
DEUCE of CLUBS(4),
EIGHT of DIAMONDS(24),

// here is the hand - which i also want the values

[SIX of CLUBS, SEVEN of DIAMONDS, TEN of DIAMONDS, TEN of CLUBS]
[SIX of HEARTS, THREE of HEARTS, SIX of SPADES, DEUCE of SPADES]
[JACK of SPADES, NINE of DIAMONDS, SEVEN of CLUBS, THREE of DIAMONDS]

C:\Java\a02>

here is the code:

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;


private Rank(int points)
{
this.Rankpoints = points;  
}

public int getRankpoints() 
{
return Rankpoints;
}


}

public enum Suit { CLUBS(2), DIAMONDS(3), HEARTS(4), SPADES(1);

private int Suitpoints;

Suit(int points)
{

Suitpoints = points;  

}



public int getSuitpoints()
{
return 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 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);
    int total = 0;
    // ArrayList<Cards> hand = new ArrayList<Cards>();



  // this is how i am handling the values.. which is not good.
 for (Cards card : deck)
 { 

 for(Cards.Suit a : Cards.Suit.values())
 {


 for(Cards.Rank b : Cards.Rank.values())
 {
    if(card.rank() == b)
     {


 if(card.suit() == a)  // as you can see this works. theres got to be a better way??
 {
   total = b.getRankpoints();
  // hand.add(card);
   System.out.println(card.rank() + " of " +  card.suit() + "(" + a.getSuitpoints() * total + ")" + ",");


 }

}
}
   }
}


    for (int i=0; i < numHands; i++)
        System.out.println(deal(deck, cardsPerHand));   // prints the hand
       //deal(deck,cardsPerHand,numHands);
}


// hand
 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();
    // Map<String, Map<String, Integer>> inputWords = new HashMap<String, Map<String, Integer>>();
      int total = 0;

      return hand;
    }

   }
 //for (Cards card : hand)
 //{ 

 // for(Cards.Suit a : Cards.Suit.values())
 // {
 //for(Cards.Rank b : Cards.Rank.values())
 // {
  //  if(card.rank() == b)
   //  {

  // if(card.suit() == a)
 //{
  // total = b.getRankpoints();


    // System.out.println("card.rank() + " of " +  card.suit() + "(" + a.getSuitpoints() * total + ")" + ",");
    // return hand;
  //}

 //}
  //}
 //}

   //}
  //return hand;
   //}

   //}

Any help is much appreciated. code snippets or hints are very welcome =) Keep in mind i am pretty new to Java..

Thank you

Upvotes: 0

Views: 6262

Answers (2)

objects
objects

Reputation: 8677

Try changing your Card classes toString() method to something like this:

public String toString() 
{ 
    return rank + " of " + suit+ "(" + suit.getSuitpoints() * rank.getRankpoints() + ")" + ","); 
}

Then this:

   System.out.println(card.rank() + " of " +  card.suit() + "(" + a.getSuitpoints() * total + ")" + ",");

Can become:

System.out.println(card);

Upvotes: 2

Brad
Brad

Reputation: 572

In regard to getting it all to print on one line you're looking for System.out.print() rather than System.out.println().

Upvotes: 1

Related Questions