I need help checking and adding code to create a functioning card points system for blackjack. Each numeric card is wort

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899603
Joined: Mon Aug 02, 2021 8:13 am

I need help checking and adding code to create a functioning card points system for blackjack. Each numeric card is wort

Post by answerhappygod »

I need help checking and adding code to create a
functioning card points system for blackjack.
Each numeric card is worth the same as the card's rank. For
example, a 2 of hearts is worth 2 points, a 7 of diamonds is worth
7 points, etc.
Each of the face cards Jack, Queen and King are worth 10
points.
The ace is worth either 1 point or 11 points (the player is free
to choose).
public class Runner {

public static void main(String args[]) {
Deck deck = new Deck() ;
deck.shuffle() ;
BlackjackDealer dealer = new
BlackjackDealer() ;
while (!dealer.isStaying()
&& !dealer.getHand().isBust()) {
Card card =
deck.takeFromTop() ;

System.out.println("Dealer has been dealt the " + card) ;
dealer.take(card)
;
if
(dealer.getHand().isBust()) {

System.out.println("Dealer is bust with a score of " +
dealer.getHand().getScore()) ;
} else if
(dealer.isStaying()) {

System.out.println("Dealer is staying with a score of " +
dealer.getHand().getScore()) ;
} else {

System.out.println("Dealer is still in with a score of " +
dealer.getHand().getScore()) ;
}
}
}
}
public class BlackjackDealer {
private boolean isStaying;
private BlackjackHand hand;
public BlackjackDealer(){
this.hand = new BlackjackHand();
}
public boolean isStaying(){return
this.isStaying;}
public BlackjackHand getHand(){return
this.hand;}
public boolean take(Card card){
if(isStaying || hand.isBust()){
return false;
}
else {
hand.add(card);
if(hand.getScore()<17)
isStaying = false;
else isStaying =
true;
return true;
}
}
public void reset(){
this.hand = new BlackjackHand();
this.isStaying = false;
}
}
import java.util.ArrayList;
public class BlackjackHand {
private int size;
private int score;
private ArrayList<Card> list;
public BlackjackHand(){
this.score = 0;
this.size = 0;
this.list = new
ArrayList<Card>();
}
public void add(Card card){
list.add(card);
}
public int getSize(){return this.size;}
public Card get(int index){
return list.get(index);
}
public int getScore(){return this.score;}
public boolean isBust(){
return score > 21;
}
}
import java.io.*;
import java.util.ArrayList;
import java.util.Collections;
public class Deck {
ArrayList<Card> cardDeck;
public Deck() {
cardDeck = new ArrayList<Card>();
for (char suit : Card.SUITS) {
for (int rank = 1; rank <= 13; rank++) {
cardDeck.add(new Card(suit, rank));
}
}
}
public int getSize() {
return cardDeck.size();
}
public Card takeFromTop() {
return cardDeck.remove(0);
}
public boolean putOnBottom(Card card) {
for (int i = 0; i < cardDeck.size(); i++) {
if (cardDeck.get(i).getRank() == card.getRank()
&& cardDeck.get(i).getSuit() == card.getSuit())
return false;
}
cardDeck.add(card);
return true;
}
public void shuffle() {
Collections.shuffle(cardDeck);
}
}
public class Card {
public static final char[] SUITS =
{'♣','♠','♦','❤'};
public static final String[] RANKS =
{"A","2","3","4","5","6","7","8","9","10","J","Q","K"} ;
//Your code here
private char suit;
private int rank;
public Card(char suit, int value) {
this.suit = suit;
this.rank = value;
}
public char getSuit() {
return this.suit;
}
public int getRank() {
return this.rank;
}
public String toString() {
return RANKS[this.rank - 1] + suit;
}
}
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply