librejack

Blackjack clone written in java
git clone https://github.com/michaelneuper/librejack.git
README | Files | Log | Refs | LICENSE

commit aace6103747924cd50d2fcfac14b8e6adeb6f23f
parent d06e12ece93556475f9581b5653dafc1737bceae
Author: Michael Neuper <73108749+michaelneuper@users.noreply.github.com>
Date:   Fri,  7 Oct 2022 19:17:05 +0200

Change formatting

Diffstat:
Msrc/controller/Hand.java | 33++++++++++++++++++---------------
Msrc/model/Card.java | 117++++++++++++++++++++++++++++++++++++++++++-------------------------------------
Msrc/model/Deck.java | 130++++++++++++++++++++++++++++++++++++++++----------------------------------------
3 files changed, 145 insertions(+), 135 deletions(-)

diff --git a/src/controller/Hand.java b/src/controller/Hand.java @@ -6,21 +6,24 @@ import model.Card; import model.Deck; /** - * Contains methods to calculate the value of a hand, take cards from a deck and discard the hand to a deck. - * @author michael + * Contains methods to calculate the value of a hand, + * take cards from a deck and + * discard the hand to a deck. + * + * @author Michael */ public class Hand { - -// private ArrayList<Card> hand; -// -// /** -// * Hand constructor -// */ -// public Hand() { -// hand = new ArrayList<Card>(); -// } -// -// public void takeCardFromDeck(Deck deck) { -// hand.add(deck.takeCard()); -// } + + private ArrayList<Card> hand; + + /** + * Hand constructor + */ + public Hand() { + hand = new ArrayList<Card>(); + } + + public void takeCardFromDeck(Deck deck) { + hand.add(deck.takeCard()); + } } diff --git a/src/model/Card.java b/src/model/Card.java @@ -2,10 +2,11 @@ package model; /** * Represents a card and handles card operations + * * @author Michael */ public class Card { - + // TODO change name to image public enum Suit { @@ -24,70 +25,76 @@ public class Card { return suitName; } } - + public enum Rank { - + TWO("Two", 2), THREE("Three", 3), - FOUR("Four",4), - FIVE("Five",5), - SIX("Six",6), - SEVEN("Seven",7), - EIGHT("Eight",8), - NINE("Nine",9), - TEN("Ten",10), - JACK("Jack",10), - QUEEN("Queen",10), - KING("King",10), + FOUR("Four", 4), + FIVE("Five", 5), + SIX("Six", 6), + SEVEN("Seven", 7), + EIGHT("Eight", 8), + NINE("Nine", 9), + TEN("Ten", 10), + JACK("Jack", 10), + QUEEN("Queen", 10), + KING("King", 10), ACE("Ace", 11); - + String rankName; public int rankValue; - - Rank(String rankName, int rankValue){ + + Rank(String rankName, int rankValue) { this.rankName = rankName; this.rankValue = rankValue; } - - public String toString(){ + + public String toString() { return rankName; } } - - private Suit suit; - private Rank rank; - - /** - * Card constructor: - * Creates a card given a suit and rank - * @param suit Card suit - * @param rank Card rank - */ - public Card(Suit suit, Rank rank) { - this.suit = suit; - this.rank = rank; - } - - - /** - * Rank value getter method - * @return card rank value - */ - public int getValue() { - return this.rank.rankValue; - } - - public Suit getSuit() { - return suit; - } - - public Rank getRank() { - return rank; - } - - @Override - public String toString() { - return "[" + rank + " of " + suit + "](" - + this.getValue() + ")"; - } + + private Suit suit; + private Rank rank; + + /** + * Card constructor: + * Creates a card given a suit and rank + * + * @param suit Card suit + * @param rank Card rank + */ + public Card(Suit suit, Rank rank) { + this.suit = suit; + this.rank = rank; + } + + public Card(Card card) { + this.suit = card.getSuit(); + this.rank = card.getRank(); + } + + /** + * Rank value getter method + * + * @return card rank value + */ + public int getValue() { + return this.rank.rankValue; + } + + public Suit getSuit() { + return suit; + } + + public Rank getRank() { + return rank; + } + + @Override + public String toString() { + return "[" + rank + " of " + suit + "](" + + this.getValue() + ")"; + } } \ No newline at end of file diff --git a/src/model/Deck.java b/src/model/Deck.java @@ -7,50 +7,48 @@ import model.Card.Rank; import model.Card.Suit; /** - * Holds multiple cards and + * Holds multiple cards and * performs operations such as shuffling the deck + * * @author Michael */ public class Deck { - + private ArrayList<Card> deck; // creates a "deck" of card objects - - /** - * Deck constructor - */ + + /** + * Deck constructor + */ public Deck() { this.deck = new ArrayList<Card>(); } - - /** - * Method that populates an empty deck with 52 cards - * by looping over each rank and value - */ + + /** + * Method that populates an empty deck with 52 cards + * by looping over each rank and value + */ public void populate() { - for(Suit cardSuit : Suit.values()) { + + for (Suit cardSuit : Suit.values()) { for (Rank cardRank : Rank.values()) { // adds a new card to the deck this.deck.add(new Card(cardSuit, cardRank)); } } } - + /** - * Method that shuffles the deck - */ + * Method that shuffles the deck + */ public void shuffle() { + ArrayList<Card> shufDeck = new ArrayList<Card>(); // temporary deck for storing cards in random order Random random = new Random(); int randomCardIndex = 0; - int originalSize = this.deck.size(); - for(int i = 0; i < originalSize; i++) { + + while (deck.size() > 0) { // Generate random index - /* - Formula for generating random number: - = random.nextInt((max - min) + 1) + min - = random.nextInt(((deckSize - 1) - 0) + 1) + 0 - */ randomCardIndex = random.nextInt(this.deck.size()); shufDeck.add(this.deck.get(randomCardIndex)); // add card to random index of temporary deck this.deck.remove(randomCardIndex); // remove card from original deck @@ -59,57 +57,59 @@ public class Deck { this.deck = shufDeck; } -// /** -// * Method to draw card from one deck to another -// * @param comingFromDeck deck the card comes from -// */ -// public void draw(Deck comingFromDeck) { -// this.deck.add(comingFromDeck.getCard(0)); -// comingFromDeck.removeCard(0); -// } -// -// /** -// * Method to remove a card from the deck -// * @param i index of card to be removed -// */ -// public void removeCard(int i) { -// this.deck.remove(i); -// } -// -// /** -// * Method to return the value of a card at a paticular index -// * @param i index of card to be returned -// * @return card at index i -// */ -// public Card getCard(int i) { -// return this.deck.get(i); -// } - - /** - * Method to add a card to the deck - * @param inCard the card the be added - */ + // /** + // * Method to draw card from one deck to another + // * @param comingFromDeck deck the card comes from + // */ + // public void draw(Deck comingFromDeck) { + // this.deck.add(comingFromDeck.getCard(0)); + // comingFromDeck.removeCard(0); + // } + // + // /** + // * Method to remove a card from the deck + // * @param i index of card to be removed + // */ + // public void removeCard(int i) { + // this.deck.remove(i); + // } + // + // /** + // * Method to return the value of a card at a paticular index + // * @param i index of card to be returned + // * @return card at index i + // */ + // public Card getCard(int i) { + // return this.deck.get(i); + // } + + /** + * Method to add a card to the deck + * + * @param inCard the card the be added + */ public void addCard(Card inCard) { this.deck.add(inCard); } -// /** -// * Method to take a copy of the first card from the deck, -// * remove the card from the deck -// * and return the card -// * @return card -// */ -// public Card takeCard() { -// Card cardToTake = new Card(deck.get(0)); -// deck.remove(0); -// return cardToTake; -// } + /** + * Method to take a copy of the first card from the deck, + * remove the card from the deck + * and return the card + * + * @return + */ + public Card takeCard() { + Card cardToTake = new Card(deck.get(0)); + deck.remove(0); + return cardToTake; + } public String toString() { - + String ret = ""; // holds the string to return - for(Card card: deck){ + for (Card card : deck) { ret += card; ret += "\n"; }