From 5df7836de41543d7fd2d8837dbafc24e8b0f5a0f Mon Sep 17 00:00:00 2001 From: sgoudham Date: Fri, 16 Oct 2020 23:27:46 +0100 Subject: [PATCH] Deck Class for storing Cards --- Personal/Cards/src/Deck.java | 46 ++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 Personal/Cards/src/Deck.java diff --git a/Personal/Cards/src/Deck.java b/Personal/Cards/src/Deck.java new file mode 100644 index 0000000..988f7be --- /dev/null +++ b/Personal/Cards/src/Deck.java @@ -0,0 +1,46 @@ +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +public abstract class Deck { + private List deck = new ArrayList(); + + // Generate deck at the beginning of the game + public Deck() { + for (Suits suit : Suits.values()) { + for (Faces face : Faces.values()) + deck.add(new Card(suit.getSuit(), face.getFace())); + } + } + + // Deal cards to players + public List dealCards(int numOfPlayers) { + int noOfCards = (deck.size() / numOfPlayers) + 1; + List allPlayers = new ArrayList(); + + for (int i = 0; i < numOfPlayers; i++) { + Hand playerHand = new Hand(); + while (playerHand.getListOfCards().size() < noOfCards && deck.size() > 0) { + Card indexCard = deck.get(0); + playerHand.addCard(indexCard); + dealACard(indexCard); + } + allPlayers.add(new Player("Player " + i, playerHand)); + } + return allPlayers; + } + + // Deal a card to the hand + public void dealACard(Card card) { + deck.remove(card); + } + + // Shuffle the entire deck + public void shuffle() { + Collections.shuffle(deck); + } + + public List getDeck() { + return deck; + } +}