0

Hello i am trying to create a game of Video Poker. This is part of what I have so far (I did not include enums, imports, variables, toStrings, etc. in order to save space):

My Card class defines our cards:

public Card(Value value, Suit suit) {

    this.value = value;
    this.suit = suit;
}

My Deck class creates a 52 card deck and returns a 5 card hand in the draw() method:

private ArrayList<Card> deck = new ArrayList<Card>();
private ArrayList<Card> hand = new ArrayList<Card>();
private final int HANDSIZE = 5;

public Deck() {

    for(Suit suit : Suit.values()) {
        for(Value value : Value.values()){
            Card card = new Card(value, suit);
            deck.add(card);
        }
    }
}

public ArrayList<Card> draw() {

    Random rng = new Random();

    for(int i = 0; i < HANDSIZE; i++){
        int getCard = rng.nextInt(deck.size());
        Card addCard = deck.get(getCard);
        hand.add(addCard);
        deck.remove(getCard);
    }

    return hand;
}

My Player class lets gives information on our players.

public class Player extends Deck {

    private String name;
    private int chips;

    public Player(String name, int chips) {

        this.name = name;
        this.chips = chips;
    }

The problem is that every player uses their own deck, therefore sometimes two or more different players may have the same card. My question is how do I set up my program to have each player use the same deck? Thanks.

Example of an output:

Name: John Doe, Cash: 500 [Jack of Hearts, Ten of Hearts, Two of Diamonds, Ace of Clubs, Nine of Diamonds]

Name: Jane Doe, Cash: 500 [King of Spades, Two of Diamonds, Jack of Hearts, Ace of Spades, Seven of Diamonds]

Halvor Holsten Strand
  • 19,829
  • 17
  • 83
  • 99
t3rrh42d2
  • 134
  • 1
  • 2
  • 10
  • 1
    Player shouldn't extend from `Deck`, but should instead, get a reference to an instance of `Deck`, which should passed to the `Player` via it's constructor... – MadProgrammer Jul 23 '14 at 01:41
  • 1
    Your "Game" class should keep a `Deck` and each `Player` in the game shouldn't extend from `Deck` but should perhaps have a `Hand` that is populated from the `Deck` contained in the Game class, whatever that is. – Ryan J Jul 23 '14 at 01:43

3 Answers3

2

Don't make the Player class extend Deck. That is basically making Players use themselves (in a way) and you're making them "have" a deck themselves. Therefore, you need to use the Deck class separately.

Pass through an instance of Deck in the Player constructor, like so:

public class Player {

    private String name;
    private int chips;
    private Deck deck;

    public Player(String name, int chips, Deck deck) {
        this.name = name;
        this.chips = chips;
        this.deck = deck;
    }
}
1

You could use a singleton pattern if there is no other convenient place to hold your Deck:

private ArrayList<Card> deck = new ArrayList<Card>();
private final int HANDSIZE = 5;

private static Deck deck = new Deck();

public static Deck getInstance() {
  return deck;
}

// Private Constructor
private Deck() {

    for(Suit suit : Suit.values()) {
        for(Value value : Value.values()){
            Card card = new Card(value, suit);
            deck.add(card);
        }
    }
}

public ArrayList<Card> draw() {

    Random rng = new Random();
    ArrayList<Card> hand = new ArrayList<Card>();
    for(int i = 0; i < HANDSIZE; i++){
    int getCard = rng.nextInt(deck.size());
    Card addCard = deck.get(getCard);
    hand.add(addCard);
    deck.remove(getCard);
    }

    return hand;
}

}

And Player draws from the deck:

public class Player {

    private String name;
    private int chips;

    public Player(String name, int chips) {

        this.name = name;
        this.chips = chips;
    }

    public ArrayList<Card> draw() {
        Deck.getInstance().draw()
    }
Khary Mendez
  • 1,818
  • 1
  • 14
  • 18
  • This seems to be working fine except player 2 has an output of the first 5 cards plus an additional 5: Name: John Doe, Cash: 500 [Eight of Diamonds, Two of Diamonds, Two of Spades, Seven of Hearts, Jack of Clubs] Name: Jane Doe, Cash: 500 [Eight of Diamonds, Two of Diamonds, Two of Spades, Seven of Hearts, Jack of Clubs, Ten of Diamonds, Five of Spades, Six of Diamonds, Nine of Diamonds, Jack of Diamonds] – t3rrh42d2 Jul 23 '14 at 03:40
  • hand should be a local variable within the draw method, not an attribute of the classs. Updated above. – Khary Mendez Jul 23 '14 at 09:53
0

So I would create 5*the number of players of random integers. Then split it in to groups of five and go in to the deck and look for those numbered cards. I.e a 0 is a 2 of spades a 13 is a 2 of clovers a 14 is a three of clovers e.t.c. Also you would have to repeat this process until you get a list without duplucates