Please help correct my existing java code. I need help adding code to my existing code to create a blackjack points syst

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

Please help correct my existing java code. I need help adding code to my existing code to create a blackjack points syst

Post by answerhappygod »

Please help correct my existing java code.
I need help adding code to my existing code to create a
blackjack points system for the cards, I have added my code
below.
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;
}
}
Output
Dealer is still in with a score of 0
Dealer has been dealt the A♦
Dealer is still in with a score of 0
Dealer has been dealt the J♣
Dealer is still in with a score of 0
Dealer has been dealt the J❤
Dealer is still in with a score of 0
Dealer has been dealt the 2♠
Dealer is still in with a score of 0
Dealer has been dealt the 3♠
Dealer is still in with a score of 0
Dealer has been dealt the K♠
Dealer is still in with a score of 0
Dealer has been dealt the 3♦
Dealer is still in with a score of 0
Dealer has been dealt the K♦
Dealer is still in with a score of 0
Dealer has been dealt the 7♣
Dealer is still in with a score of 0
Dealer has been dealt the 6♦
Dealer is still in with a score of 0
Dealer has been dealt the 5❤
Dealer is still in with a score of 0
Dealer has been dealt the J♦
Dealer is still in with a score of 0
Dealer has been dealt the 9♣
Dealer is still in with a score of 0
Dealer has been dealt the A♣
Dealer is still in with a score of 0
Dealer has been dealt the J♠
Dealer is still in with a score of 0
Dealer has been dealt the 4♣
Dealer is still in with a score of 0
Dealer has been dealt the K❤
Dealer is still in with a score of 0
Dealer has been dealt the 7♠
Dealer is still in with a score of 0
Dealer has been dealt the 10♦
Dealer is still in with a score of 0
Dealer has been dealt the 3♣
Dealer is still in with a score of 0
Dealer has been dealt the 7♦
Dealer is still in with a score of 0
Dealer has been dealt the 9♠
Dealer is still in with a score of 0
Dealer has been dealt the 10♠
Dealer is still in with a score of 0
Dealer has been dealt the 2♦
Dealer is still in with a score of 0
Dealer has been dealt the 6❤
Dealer is still in with a score of 0
Dealer has been dealt the 9♦
Dealer is still in with a score of 0
Dealer has been dealt the 6♣
Dealer is still in with a score of 0
Dealer has been dealt the 9❤
Dealer is still in with a score of 0
Dealer has been dealt the 2❤
Dealer is still in with a score of 0
Dealer has been dealt the 5♠
Dealer is still in with a score of 0
Dealer has been dealt the K♣
Dealer is still in with a score of 0
Dealer has been dealt the Q♣
Dealer is still in with a score of 0
Dealer has been dealt the 10♣
Dealer is still in with a score of 0
Dealer has been dealt the Q♠
Dealer is still in with a score of 0
Dealer has been dealt the 4❤
Dealer is still in with a score of 0
Dealer has been dealt the A❤
Dealer is still in with a score of 0
Dealer has been dealt the 7❤
Dealer is still in with a score of 0
Dealer has been dealt the Q❤
Dealer is still in with a score of 0
Dealer has been dealt the 8❤
Dealer is still in with a score of 0
Dealer has been dealt the 4♦
Dealer is still in with a score of 0
Dealer has been dealt the 8♦
Dealer is still in with a score of 0
Dealer has been dealt the A♠
Dealer is still in with a score of 0
Dealer has been dealt the 6♠
Dealer is still in with a score of 0
Dealer has been dealt the Q♦
Dealer is still in with a score of 0
Dealer has been dealt the 8♠
Dealer is still in with a score of 0
Dealer has been dealt the 4♠
Dealer is still in with a score of 0
Dealer has been dealt the 5♦
Dealer is still in with a score of 0
Exception in thread "main"
java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply