Below are two classes Deck and Card. these are being used for a blackjack game and therefore i am trying to get the values of the cards. the values are currently stored in an enum, however i cannot seem to work out how to get the values from the enum.
Here are the steps I have tried: 1. Create a hand class which deals two cards from the deck by creating an instance of the class and then using the method deck.deal. 2. Attempt to get the rank values from the dealt cards. Here is where my approach is failing. I have tried to get the values from the Enum but i simply cannot seem to access them. for Example if deck.deal gives me the 5 of clubs and the 6 of hearts, i cannot seem to get those cards and run the method of Rank.getvalues.
whats the best approach here?
public class Deck {
private final Stack<Card> deckCards;
public Deck() {
this.deckCards = initDeck();
}
private Stack<Card> initDeck() {
final Stack<Card> deckCards = new Stack<>();
for (final Card.Suit suit : Card.Suit.values()) {
for (final Card.Rank rank : Card.Rank.values()) {
deckCards.push(Card.getCard(rank, suit));
}
}
Collections.shuffle(deckCards);
return deckCards;
}
public Optional<Card> deal() {
return this.deckCards.empty() ? Optional.empty() :
Optional.of(this.deckCards.pop());
}
}
public class Card implements Comparable <Card> {
private final Rank rank;
private final Suit suit;
private final static Map<String, Card> Card_Cache = initCache();
public static Map<String, Card> initCache(){
final Map<String, Card> cache = new HashMap<>();
for(Suit suit: Suit.values()){
for(Rank rank: Rank.values()){
cache.put(cardKey(rank, suit), new Card(rank,suit));
}
}
return Collections.unmodifiableMap(cache);
}
public static Card getCard (final Rank rank, final Suit suit){
final Card card = Card_Cache.get(cardKey(rank, suit));
if(card != null){
return card;
}
throw new RuntimeException("Invalid card!");
}
private static String cardKey(final Rank rank, final Suit suit) {
return rank + " of " + suit;
}
public Card(Rank rank, Suit suit) {
this.rank = rank;
this.suit = suit;
}
@Override
public String toString() {
return String.format("%s of %s", this.rank, this.suit);
}
@Override
public int compareTo(Card o) {
final int rankComparison = Integer.compare(this.rank.getRankValue(), o.rank.getRankValue());
if(rankComparison != 0){
}
return Integer.compare(this.suit.getSuitValue(), this.suit.getSuitValue());
}
enum Suit{
diamonds(1),
clubs(2),
hearts(3),
spades(4);
private final int suitValue;
Suit(final int suitValue){
this.suitValue = suitValue;
}
public int getSuitValue(){
return this.suitValue;
}
}
enum Rank{two(2), three(3), four(4), five(5), six(6),
seven(7), eight(8), nine(9), ten(10), jack(11),
queen(12), king(13), ace(14);
private final int rankValue;
Rank(final int rankValue){
this.rankValue = rankValue;
}
public int getRankValue(){
return this.rankValue;
}
}
public class Hand {
private Optional<Card> card1;
private Optional<Card> card2;
Deck mynewdeck = new Deck();
public Hand() {
this.card1 = mynewdeck.deal();
this.card2 = mynewdeck.deal();
}
// public String getvalue(){
// get
// }
public Optional<Card> getCard1() {
return card1;
}
public void setCard1(Optional<Card> card1) {
this.card1 = card1;
}
public Optional<Card> getCard2() {
return card2;
}
public void setCard2(Optional<Card> card2) {
this.card2 = card2;
}
}
below is my Main class. which consist of a series of attempts ( none of which seem to work ) in order to access the rank value of the cards given to me by the Hand class. hope this makes my attempts and approach more clear
public class Main {
public static void main(String[] args) {
Hand myhand = new Hand();
System.out.println(myhand.getCard1());
System.out.println(myhand.getCard2());
System.out.println(myhand.getCard1(Card.Rank.valueOf();// incorrect
Card mytestcard = new Card(Card.Rank.seven, Card.Suit.spades);
System.out.println("this is the card value " + Card.Rank.valueOf(Card.Rank.seven.toString()));
// incorrect
String myfirstcard = (myhand.getCard1().toString()); // incorrect
System.out.println(Card.Suit.valueOf(myfirstcard)); // incorrect
}
}