Search code examples
javaarraysplaying-cards

Cannot get card/deck object program to work


this is my first time posting on the forum and I'm not entirely sure if my question is valid, but I will try to be specific and follow the guideline. In following the guidelines, this is a question based around a class assignment. This assignment is to take code that creates a 'deck' object that represents a deck of cards and add several features. I am currently stuck in the process.

My issue lies within this code:

    public class SilasAlmgrenS6L1CardCreate {
public static void main(String args[]) {
    Deck d = new Deck();
    d.shuffle();

   Hand f = new Hand(d); //Returns error 'Hand cannot be resolved to a type'

}

public static class Deck {
    Card[] cardArray = new Card[52];
    Deck() { //constructor
        int suits = 4;
        int cardType = 13;
        int cardCount = 0;
        for (int i = 1; i <= suits; i++)
            for (int j = 1; j <= cardType; j++) {
                cardArray[cardCount] = new Card(i, j);
                cardCount++;
            } //End loop
    } //End deck() constructor


    ////////////////////////////////////////////////////////
    //My code starts here

    public class Hand {
        Hand(Deck a) {
            Card[] Hand = {a.cardArray[0], a.cardArray[1], a.cardArray[2], a.cardArray[3], a.cardArray[4]};
            Card[] playerHand = {Hand[0], Hand[1]};
            System.out.println("You have " + playerHand[0] + " and " + playerHand[1] );

        } //End hand constructor
    } //End hand class

    public void shuffle() {
        //Runs loop for the length of the deck
        for(int i = 0; i < cardArray.length; i++) {
            int num = (int) (Math.random() * (51 - 0)) + 0; //Creates a random number between 0 and 51; used to shuffle
            Card placeHolder = cardArray[i]; //Picks a place holder card from the deck
            cardArray[i] = cardArray[num]; //Picks two random cards and make them equal
            cardArray[num] = placeHolder; //Assigns one of the duplicate cards to the placeholder value 
        } //End for
    } //End shuffle


    //And ends here
    /////////////////////////////////////////////////


    public void print() {
        for (int i = 0; i < cardArray.length; i++)
            System.out.println(cardArray[i]);
    } //End print loop
} //End print class



public static class Card {
    String suit, name;
    int points;
    Card(int n1, int n2) {
        suit = getSuit(n1);
        name = getName(n2);
        points = getPoints(name);
    } //End card class

    public String toString() {
        return "The " + name + " of " + suit;
    } //End toString

    public String getName(int i) {
        if (i == 1) return "Ace";
        if (i == 2) return "Two";
        if (i == 3) return "Three";
        if (i == 4) return "Four";
        if (i == 5) return "Five";
        if (i == 6) return "Six";
        if (i == 7) return "Seven";
        if (i == 8) return "Eight";
        if (i == 9) return "Nine";
        if (i == 10) return "Ten";
        if (i == 11) return "Jack";
        if (i == 12) return "Queen";
        if (i == 13) return "King";
        return "error";
    } //End getName String

    public int getPoints(String n) {
        if (n == "Jack" || n == "Queen" || n == "King" || n == "Ten")
            return 10;
        if (n == "Two")
            return 2;
        if (n == "Three")
            return 3;
        if (n == "Four")
            return 4;
        if (n == "Five")
            return 5;
        if (n == "Six")
            return 6;
        if (n == "Seven")
            return 7;
        if (n == "Eight")
            return 8;
        if (n == "Nine")
            return 9;
        if (n == "Ace")
            return 11;
        return -1;
    } //End int getPoints

    public String getSuit(int i) {
        if (i == 1) return "Diamonds";
        if (i == 2) return "Clubs";
        if (i == 3) return "Spades";
        if (i == 4) return "Hearts";
        return "error";
    } //End getSuit String        
} //End Deck class

}

The majority of this program was already provided with the assignment, but we are to add several features. The first of these features was a shuffle method, which I was able to do. In the next feature we are to create a 'Hand' class that deals a hand of cards to the user and calculate how many points they have, as if it were a game of blackjack. This is the exact wording of this step is:

'Add a Hand Class that contains an array of 5 Card references. Have the program Deal the Hand two cards and display them for the user. Tell the user how many points they have and ask them if they would like another card or not. Continue to allow the player to add cards until they reach 5 cards or the total is greater than 21.'

I have ran through several ways that I felt I could create this class, but none seem to be working. This current iteration is as close as I've gotten. I am currently stumped, however. My issues are; I don't know why I'm getting the type error when I try to use the Hand class and I have no idea how to implement the getPoints() method. There are several steps following the creation of the Hand class, but I am sure I can get through them if I can figure out how to make this class work. I'm on the brink of punching a hole in my wall, so any help with fixing this code would be absolutely appreciated.


Solution

  • The first problem is that your Hand class is a subclass of your static Deck class. This way you can not use it in your main method, because that is not the enclosing class.

    EDIT

    So, I went a little overboard here. Hope it makes sense to you.

    First of all, we have your main class. I renamed it for the sake of not wanting to type that long name. I added your Handand Deck objects as variables, since neither the Hand belongs to the Deck or the other way around, but they both are part of the Main class, let's refer to it as the game.

    You start by shuffling the deck. When you have the deck, you can get a random card. This is provided by the deck. (I will come to that method shortly).

    As soon as you have a hand, you can show the user the amount of points they have and ask them if they would like an extra card (if numberOfPoints < 22 and numberOfCards < 5). If they do want an extra card, ask the deck for a random card and add it to the hand. Go on until any of the boundaries are met.

    import java.util.ArrayList;
    import java.util.List;
    import java.util.Scanner;
    
    public class CardExample {
    
    private Hand playHand;
    private Deck playDeck;
    private Scanner reader = new Scanner(System.in);  // Reading from System.in
    
    
    public static void main(String args[]) {
        new CardExample().play();
    }
    
    public void play(){
        playDeck = new Deck();
        playDeck.shuffle();
    
        Card firstCard = playDeck.getRandomCard();
        Card secondCard = playDeck.getRandomCard();
    
        List<Card> startCards = new ArrayList<>();
        startCards.add(firstCard);
        startCards.add(secondCard);
    
        playHand = new Hand(startCards);
        requestInput();
    }
    
    private void requestInput(){
        System.out.println("You have " + playHand.getPoints() + " points");
        System.out.println("New card? (Y/N)");
        String input = reader.next();
        if(input.equalsIgnoreCase("y")){
            Card newCard = playDeck.getRandomCard();
            playHand.addCard(newCard);
    
            if(playHand.getNumberOfCards() < 5 && playHand.getPoints() < 22) {
                requestInput();
            }else if(playHand.getPoints() >= 22){
                System.out.println("You have " + playHand.getPoints() + "points. You're dead, sorry.");
                reader.close();
            } else{
                System.out.println("You have 5 cards, that's the max");
                reader.close();
            }
        }else{
            System.out.println("Your score is " + playHand.getPoints() + " points");
            reader.close();
    
        }
    }
    }
    

    Deck class

     public class Deck {
    
    private Card[] cardArray = new Card[52];
    private int currentIndex = 0;
    
    public Deck() { //constructor
        int suits = 4;
        int cardType = 13;
        int cardCount = 0;
        for (int i = 1; i <= suits; i++)
            for (int j = 1; j <= cardType; j++) {
                cardArray[cardCount] = new Card(i, j);
                cardCount++;
            } //End loop
    }
    
    public void shuffle() {
        //Runs loop for the length of the deck
        for(int i = 0; i < cardArray.length; i++) {
            int num = (int) (Math.random() * (51 - 0)) + 0; //Creates a random number between 0 and 51; used to shuffle
            Card placeHolder = cardArray[i]; //Picks a place holder card from the deck
            cardArray[i] = cardArray[num]; //Picks two random cards and make them equal
            cardArray[num] = placeHolder; //Assigns one of the duplicate cards to the placeholder value
        } //End for
    } //End shuffle
    
    public Card[] getCardArray() {
        return cardArray;
    }
    
    public Card getRandomCard(){
        Card nextCard = cardArray[currentIndex];
        currentIndex += 1;
        return nextCard;
    }
    
    
    //And ends here
    /////////////////////////////////////////////////
    
    
    public void print() {
        for (int i = 0; i < cardArray.length; i++)
            System.out.println(cardArray[i]);
    } //End print loop
    }
    

    Hand class addCard updates points so that whenever getting numberOfCards or points, right value is returned

    import java.util.ArrayList;
    import java.util.List;
    
    public class Hand {
    private int points = 0;
    private int numberOfCards = 0;
    private List<Card> hand = new ArrayList<>();
    
    
    public Hand(List<Card> cards) {
        hand.addAll(cards);
        numberOfCards += cards.size();
        for(Card card: cards){
            points += card.points;
        }
    } //End hand constructor
    
    public void addCard(Card card){
        hand.add(card);
        points += card.points;
        numberOfCards += 1;
    }
    
    public int getNumberOfCards() {
        return numberOfCards;
    }
    
    public int getPoints() {
        return points;
    }
    
    
    }
    

    Card class (unchanged, but seperate file)

    public class Card {
    String suit, name;
    int points;
    Card(int n1, int n2) {
        suit = getSuit(n1);
        name = getName(n2);
        points = getPoints(name);
    } //End card class
    
    public String toString() {
        return "The " + name + " of " + suit;
    } //End toString
    
    public String getName(int i) {
        if (i == 1) return "Ace";
        if (i == 2) return "Two";
        if (i == 3) return "Three";
        if (i == 4) return "Four";
        if (i == 5) return "Five";
        if (i == 6) return "Six";
        if (i == 7) return "Seven";
        if (i == 8) return "Eight";
        if (i == 9) return "Nine";
        if (i == 10) return "Ten";
        if (i == 11) return "Jack";
        if (i == 12) return "Queen";
        if (i == 13) return "King";
        return "error";
    } //End getName String
    
    public int getPoints(String n) {
        if (n == "Jack" || n == "Queen" || n == "King" || n == "Ten")
            return 10;
        if (n == "Two")
            return 2;
        if (n == "Three")
            return 3;
        if (n == "Four")
            return 4;
        if (n == "Five")
            return 5;
        if (n == "Six")
            return 6;
        if (n == "Seven")
            return 7;
        if (n == "Eight")
            return 8;
        if (n == "Nine")
            return 9;
        if (n == "Ace")
            return 11;
        return -1;
    } //End int getPoints
    
    public String getSuit(int i) {
        if (i == 1) return "Diamonds";
        if (i == 2) return "Clubs";
        if (i == 3) return "Spades";
        if (i == 4) return "Hearts";
        return "error";
    } //End getSuit String
    } //End Deck class