can someone please update this code that is in java and help me get it to compile, showing the
fixed code?
import java.util.*;
import java.util.Scanner;
import java.util.Random;
public class BlackJack {
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
/**
* This is the deck of cards, do not edit it unless you want to change the variable name
*/
ArrayList<String> deck = new ArrayList<String>(Arrays.asList(
"AH", "2H", "3H", "4H", "5H", "6H", "7H", "8H", "9H", "10H", "JH", "QH", "KH",
"AD", "2D", "3D", "4D", "5D", "6D", "7D", "8D", "9D", "10D", "JD", "QD", "KD",
"AC", "2C", "3C", "4C", "5C", "6C", "7C", "8C", "9C", "10C", "JC", "QC", "KC",
"AS", "2S", "3S", "4S", "5S", "6S", "7S", "8S", "9S", "10S", "JS", "QS", "KS"
));
System.out.println("Give me a seed.");
int seed = scnr.nextInt();
// Set the seed for the random number generator
Random random = new Random(seed);
System.out.println("How many humans would you like to play with?");
int numPlayers = scnr.nextInt();
// Create an array to hold the player scores
int[] playerScores = new int[numPlayers];
// Create an array to hold the player decks
ArrayList<String>[] playerDecks = new ArrayList[numPlayers];
for (int i = 0; i < numPlayers; i++) {
playerDecks[i] = new ArrayList<String>();
}
// Deal two cards to each player
for (int i = 0; i < numPlayers; i++) {
ArrayList<String> myDeck = playerDecks[i];
myDeck.add(dealCard(deck, random));
myDeck.add(dealCard(deck, random));
// Display the player's cards
System.out.println("Player " + (i + 1) + "'s cards:");
displayCards(myDeck);
// Wait for player to acknowledge their cards
System.out.println("Acknowledge that you have seen your cards player " + (i + 1) + " by entering
any key.");
scnr.next();
// Calculate and display the player's score
int score = calculateScore(myDeck);
playerScores[i] = score;
System.out.println("Player " + (i + 1) + "'s score: " + score);
}
// Play the game
boolean[] isBusted = new boolean[numPlayers];
boolean isAllStick = false;
while (!isAllStick) {
isAllStick = true;
for (int i = 0; i < numPlayers; i++) {
ArrayList<String> myDeck = playerDecks[i];
if (!isBusted[i]) {
// Ask player whether to hit or stick
System.out.println("Player " + (i + 1) + "'s cards: ");
displayCards(myDeck);
System.out.println("Player " + (i + 1) + " would you like to hit or stick?");
String response = scnr.next();
while (response.equalsIgnoreCase("hit")) {
myDeck.add(deck.remove(random.nextInt(deck.size())));
int score = calculateScore(myDeck);
if (score > 21) {
isBusted[i] = true;
System.out.println("Player " + (i + 1) + " has busted. Enter any key to acknowledge this.");
scnr.next();
break;
}
System.out.println("Player " + (i + 1) + "'s cards: ");
displayCards(myDeck);
System.out.println("Player " + (i + 1) + " would you like to hit or stick?");
response = scnr.next();
}
while (response.equalsIgnoreCase("hit")) {
myDeck.add(dealCard(random));
// If the player busts, mark them as busted and stop asking for more cards
if (s.
can someone please update this code that is in java and help.pdf
1. can someone please update this code that is in java and help me get it to compile, showing the
fixed code?
import java.util.*;
import java.util.Scanner;
import java.util.Random;
public class BlackJack {
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
/**
* This is the deck of cards, do not edit it unless you want to change the variable name
*/
ArrayList<String> deck = new ArrayList<String>(Arrays.asList(
"AH", "2H", "3H", "4H", "5H", "6H", "7H", "8H", "9H", "10H", "JH", "QH", "KH",
"AD", "2D", "3D", "4D", "5D", "6D", "7D", "8D", "9D", "10D", "JD", "QD", "KD",
"AC", "2C", "3C", "4C", "5C", "6C", "7C", "8C", "9C", "10C", "JC", "QC", "KC",
"AS", "2S", "3S", "4S", "5S", "6S", "7S", "8S", "9S", "10S", "JS", "QS", "KS"
));
System.out.println("Give me a seed.");
int seed = scnr.nextInt();
// Set the seed for the random number generator
Random random = new Random(seed);
System.out.println("How many humans would you like to play with?");
int numPlayers = scnr.nextInt();
// Create an array to hold the player scores
int[] playerScores = new int[numPlayers];
// Create an array to hold the player decks
ArrayList<String>[] playerDecks = new ArrayList[numPlayers];
for (int i = 0; i < numPlayers; i++) {
playerDecks[i] = new ArrayList<String>();
}
// Deal two cards to each player
for (int i = 0; i < numPlayers; i++) {
ArrayList<String> myDeck = playerDecks[i];
myDeck.add(dealCard(deck, random));
myDeck.add(dealCard(deck, random));
// Display the player's cards
System.out.println("Player " + (i + 1) + "'s cards:");
displayCards(myDeck);
// Wait for player to acknowledge their cards
System.out.println("Acknowledge that you have seen your cards player " + (i + 1) + " by entering
any key.");
scnr.next();
2. // Calculate and display the player's score
int score = calculateScore(myDeck);
playerScores[i] = score;
System.out.println("Player " + (i + 1) + "'s score: " + score);
}
// Play the game
boolean[] isBusted = new boolean[numPlayers];
boolean isAllStick = false;
while (!isAllStick) {
isAllStick = true;
for (int i = 0; i < numPlayers; i++) {
ArrayList<String> myDeck = playerDecks[i];
if (!isBusted[i]) {
// Ask player whether to hit or stick
System.out.println("Player " + (i + 1) + "'s cards: ");
displayCards(myDeck);
System.out.println("Player " + (i + 1) + " would you like to hit or stick?");
String response = scnr.next();
while (response.equalsIgnoreCase("hit")) {
myDeck.add(deck.remove(random.nextInt(deck.size())));
int score = calculateScore(myDeck);
if (score > 21) {
isBusted[i] = true;
System.out.println("Player " + (i + 1) + " has busted. Enter any key to acknowledge this.");
scnr.next();
break;
}
System.out.println("Player " + (i + 1) + "'s cards: ");
displayCards(myDeck);
System.out.println("Player " + (i + 1) + " would you like to hit or stick?");
response = scnr.next();
}
while (response.equalsIgnoreCase("hit")) {
myDeck.add(dealCard(random));
// If the player busts, mark them as busted and stop asking for more cards
if (score > 21) {
isBusted[i] = true;
System.out.println("Player " + (i + 1) + " has busted. Enter any key to acknowledge this.");
scnr.next();
break;
}
// Ask the player again whether to hit or stick
3. System.out.println("Player " + (i + 1) + " would you like to hit or stick?");
response = scnr.next();
}
while (response.equalsIgnoreCase("hit")) {
myDeck.add(deck.remove(random.nextInt(deck.size())));
int score = calculateScore(myDeck);
if (score > 21) {
System.out.println("Player " + (i + 1) + "'s cards: " + myDeck);
}
}
}
// Determine the maximum score that is not a bust
int maxScore = -1;
for (int i = 0; i < numPlayers; i++) {
int score = playerScores[i];
if (score <= 21 && score > maxScore) {
maxScore = score;
}
}
// Find the indices of the players with the maximum score
ArrayList<Integer> maxScoreIndices = new ArrayList<Integer>();
for (int i = 0; i < numPlayers; i++) {
if (playerScores[i] == maxScore) {
maxScoreIndices.add(i + 1);
}
}
// Print the winner or winners, or declare a tie
if (maxScoreIndices.size() == 1) {
System.out.println("Player " + maxScoreIndices.get(0) + " got the highest score of " + maxScore +
".");
} else if (maxScoreIndices.size() == numPlayers) {
System.out.println("Everybody busted.");
} else if (maxScoreIndices.size() == 2) {
System.out.print("Players ");
System.out.print(maxScoreIndices.get(0) + " ");
System.out.print("and " + maxScoreIndices.get(1));
System.out.println(" tied for the highest score of " + maxScore);
} else {
System.out.print("Players ");
for (int i = 0; i < maxScoreIndices.size() - 1; i++) {
System.out.print(maxScoreIndices.get(i) + ", ");
}
4. System.out.print("and " + maxScoreIndices.get(maxScoreIndices.size() - 1));
System.out.println(" tied for the highest score of " + maxScore);
}
System.out.println("Nobody won.");
}
}
}
}