Home > Enterprise >  I am coding a card game in Java. The second object of the Hands class for player2 gives the player t
I am coding a card game in Java. The second object of the Hands class for player2 gives the player t

Time:02-24

I am a beginner in programming, and it is my first question here. I have to code a card game, Gin Rummy, in Java.

I created the Card class, Deck class, and Hands class. The deck class constructor initialize an Arraylist of 52 cards. At the start of the game each player has to draw 10 cards, so in the hands class constructor, the first 10 cards of the deck is drawn and added to an array list in the Hands class.

When I check the the first players cards, it is true. The cards left in the deck after player 1 draw 10 cards? true, cards are no longer there. Player 2 cards? Not true, player 2 cards are the first 10 cards of the 52 deck (the same as player 1). I spent hours trying to figure out what is wrong.

I printed the cards deck after player 2 take, and the first 20 cards are actually drawn. I do not know why, when I create a second object of the Hand class, it does not take the next 10 cards, but take the cards that the first object, player 1, already took.

This the CardsDeck class: it adds 52 card set to the array list, which works well.

     public class CardsDeck {

    private final int totalCardsNumber = 52;

 private static List<Card> deckOfCards;

    public CardsDeck(){
         deckOfCards = new ArrayList<>(totalCardsNumber);
         int index=0;
         for (int suit = 0; suit <Card.Suit.values().length; suit  ){
             for(int rank = 0; rank <Card.Rank.values().length; rank  ){
              deckOfCards.add(new Card(Card.Rank.values()[rank], Card.Suit.values()[suit]));
              index  ;
             }
         }
        // Collections.shuffle(deckOfCards);
        }


    public void print(){
        for (Card card: deckOfCards){ System.out.println(card);}
        }
    public void setDeckOfCards(List<Card> updatedDeckOfCards) { this.deckOfCards = new ArrayList<>(updatedDeckOfCards); }
    protected static List<Card> getDeckOfCards() {
        return deckOfCards;
    }

}

This is the Hands class:

public class CardsInHands extends CardsDeck{

 protected static List<Card> updatedCardList = new ArrayList<>(getDeckOfCards());
    protected List<Card> playerCards = new ArrayList<>();

    public CardsInHands(){ 
        for (int i=0; i<10; i  ){
           playerCards.add(getDeckOfCards().get(i)); 
           updatedCardList.remove(0); 
        }
        setDeckOfCards(updatedCardList); 

    }


    public void printPlayerCards() {
        for (Card card: playerCards){ System.out.print(card   " - ");}
    }


    public List<Card> getPlayerCards() {
        return playerCards;
    }
    public void setPlayerCards(List<Card> playerCards) { this.playerCards = playerCards;}
    public void removeCard(int cardIndex){ playerCards.remove(cardIndex);}
    public void addCard(Card card){playerCards.add(card);}

For sake of completion, this is the Card class:

  public class Card {
    public enum Rank {
        ACE(1),TWO(2), THREE(3), FOUR(4), FIVE(5),
        SIX(6), SEVEN(7), EIGHT(8), NINE(9),
        TEN(10),
        JACK(10), QUEEN(10), KING(10);
        int value;

        Rank(int value) { this.value = value; }
        public int getValue() { return value; }
    }

    public enum Suit {SPADES,HEARTS, CLUBS, DIAMONDS;}

    private final Rank rank;
    private final Suit suit;

    public Card(Rank rank, Suit suit){
        this.rank = rank;
        this.suit = suit;
    }


    public Rank getRank() { return rank; }
    public Suit getSuit() { return suit; }
    public int getValue(){ return rank.getValue(); }

    public String toString(){
        return this.rank  " of "  this.suit;
    }
}

This is the main method I test the code from:

 public class Runner {
    public static void main(String[] args){
    CardsDeck cards = new CardsDeck();
    //cards.print();


   CardsInHands playerOne = new CardsInHands();
   //playerOne.printPlayerCards(); //prints first 10 cards of the deck
   // cards.print(); //correct, first 10 cards are gone

   CardsInHands playerTwo = new CardsInHands();
   playerTwo.printPlayerCards(); //shows same 10 cards of player 1!
   //cards.print(); //correct, second set of 10 cards are gone

    }
}

Could anyone tell me what I am doing wrong that makes the second object of Hands class not work properly?

CodePudding user response:

This is happening because you have marked deckOfCards a static, which means it is a class level variable. Stop using static there and the problem will be solved.

More on static variable. https://beginnersbook.com/2013/04/java-static-class-block-methods-variables/#:~:text=Java Static Variables,the instances of the class.&text=Static variables are also known as Class Variables.

  • Related