2

I am trying to make a simplified version of Solitaire known as Elevens. I successfully made the Card class but I am having trouble creating a Deck class. The goal is to create a deck with unique cards. Sorry if this explanation isn't the best but hopefully the code samples will help:

The Card class constructor:

package ElevensLab;

public class Card {
    private String suit;
    private String rank;
    private int pointValue;

public Card(String cardRank, String cardSuit, int cardPointValue) {
    rank=cardRank;
    suit=cardSuit;
    pointValue=cardPointValue;
}

The Deck class constructor:

package ElevensLab;
import java.util.List;
import java.util.ArrayList;

public class Deck {
    private List<Card> cards;
    private int size;
    public Deck(String[] suits, String[] ranks, int[] values){
        ArrayList<Card> cards=new ArrayList<Card>();
        for(String suit: suits){
            for(String rank: ranks){
                for(int value: values){
                    cards.add(suit);
                }
            }
        }
        size=cards.size();
    }
3
  • 1
    So what's the problem? Commented May 1, 2017 at 18:23
  • You seem to be adding suits to your list of cards, for some reason instead of constructing cards and adding them to your list of cards. Commented May 1, 2017 at 18:25
  • Are you asking for design help? Commented May 1, 2017 at 18:26

1 Answer 1

1

Alright, here goes:

So, you're making an ArrayList of Card. To add an item to that ArrayList, it must be of type Card, correct? So on the line in your Deck class cards.add(suit) you are adding a String to an ArrayList of type Card. This is giving you a compiler error, I would assume. Instead, what I would do, is actually put a Card constructor, inside of the cards.add() call. I'll show you using your code:

package ElevensLab;
import java.util.List;
import java.util.ArrayList;

public class Deck {
    private List<Card> cards;
    private int size;
    public Deck(String[] suits, String[] ranks, int[] values){
        ArrayList<Card> cards=new ArrayList<Card>();
        for(String suit: suits){
            for(String rank: ranks){
                for(int value: values){
                    cards.add(new Card(rank, suit, value)); //This is the line I changed
                }
            }
        }
        size=cards.size();
    }

This code adds a new Card to the Deck with the desired values. I hope that you find this helpful, and wish you luck!

Sign up to request clarification or add additional context in comments.

1 Comment

i'm gonna break a guideline and say THANK YOU! i didn't think about actually creating a new instance of Card

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.