I am attempting to create an OOP friendly Java BlackJack game to progress my knowledge.
I've hit a wall and I just don't know enough to see the problem. Was wondering if anyone could point out my problems.
Additionally, after googling relevant topics about this I've found people time and time again saying using enums would be more beneficial, as a beginner would this be advised? Or should I stick with String arrays for the time being.
Thank you.
my code:
public class BlackJack{
BlackJack() {
Deck deck = new Deck();
deck.createDeck();
System.out.println(deck.deckList);
}
public static void main(String[] args) {
new BlackJack();
}
}
public class Card{
private String valueCard;
private String suitCard;
public Card(String value, String suit) {
this.valueCard = value;
this.suitCard = suit;
}
public String getValue() {
return valueCard;
}
public String getSuit() {
return suitCard;
}
}
import java.util.ArrayList;
public class Deck {
ArrayList<Card> deckList = new ArrayList<Card>();
String[] value = {"Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten",
"Jack", "Queen", "King", "Ace"};
String[] suit = {"Hearts", "Clubs", "Spades", "Diamonds"};
Deck() {
deckList = new ArrayList<Card>();
}
public void createDeck() {
for (int i = 0; i < value.length; i++) {
for (int x = 0; x < suit.length; x++) {
Card card = new Card(value[i], suit[x]);
deckList.add(card);
}
}
}
}
edit: at the moment my out-put from my println is: [Card@addbf1, Card@42e816, Card@9304b1, ... etc] What does this mean?
Thank you for your time.
EDIT: Anyone who also needs an answer to this in the future:
added:
@Override
public String toString(){
return valueCard + " of " + suitCard;
}
}
to my Card class and then used it in the Deck class:
import java.util.ArrayList;
public class Deck {
ArrayList<Card> deckList = new ArrayList<Card>();
String[] value = {"Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten",
"Jack", "Queen", "King", "Ace"};
String[] suit = {"Hearts", "Clubs", "Spades", "Diamonds"};
Deck() {
deckList = new ArrayList<Card>();
}
public void createDeck() {
for (int i = 0; i < value.length; i++) {
for (int x = 0; x < suit.length; x++) {
Card card = new Card(value[i], suit[x]);
deckList.add(card);
card.toString();
}
}
}
}
ENUM: public class CardEnum {
public enum Rank { DEUCE, THREE, FOUR, FIVE, SIX,
SEVEN, EIGHT, NINE, TEN, JACK, QUEEN, KING, ACE }
public enum Suit { CLUBS, DIAMONDS, HEARTS, SPADES }
}
public class Card{
private CardEnum.Rank rank;
private CardEnum.Suit suit;
public Card(CardEnum.Rank rank, CardEnum.Suit suit) {
this.rank = rank;
this.suit = suit;
}
public Rank getRank() {
return rank;
}
public Suit getSuit() {
return suit;
}
@Override
public String toString(){
return rank + " of " + suit;
}
}