I am trying to achieve the best potential of OOP in Java by creating a blackjack game. I have 5 java files so far.
Suit.java
enum Suit {
/*
* Initialize suit values
*/
HEARTS,
SPADES,
CLUBS,
DIAMONDS;
}
Rank.java
enum Rank {
/*
* Initialize rank and card value
*/
TWO(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);
// Hold card value
private int cardValue;
/*
* Constructor set card value
* @param cardValue value of card
*/
private Rank(int cardValue) {
this.cardValue = cardValue;
}
/*
* This method obtains the card value
* @return This returns the value of card
*/
public int getCardValue() {
return cardValue;
}
}
Card.java
public class Card {
private Suit suit;
private Rank rank;
public Card(Rank rank, Suit suit) {
this.rank = rank;
this.suit = suit;
}
public Rank getCardValue() {
return rank;
}
public Suit getSuitValue() {
return suit;
}
}
deck.java
import java.util.ArrayList;
import java.util.Collections;
import java.util.Random;
public class Deck {
private ArrayList deck;
public Deck() {
this.deck = new ArrayList();
for(Suit suit : Suit.values()) {
for(Rank rank: Rank.values()) {
Card card = new Card(rank, suit);
this.deck.add(card);
}
}
Collections.shuffle(deck);
}
public ArrayList getDeck() {
return deck;
}
}
Game.java
import java.util.ArrayList;
import java.util.Iterator;
public class Game {
public static void main(String[] args) {
Deck deck = new Deck();
for(int i=0; i<deck.getDeck().size(); i++) {
System.out.println( ((Card) deck.getDeck().get(i)).getCardValue() + " of "
+ ((Card) deck.getDeck().get(i)).getSuitValue() );
}
}
}
Does my design make sense so far? Also how can I call (Rank.java) -> int getCardValue() instead of (Card.java) -> Rank getCardValue()? Do I need to do some polymorphism? extend? I want the int value not the text value.
Thanks!