Hello, I need help in my 21 card game. I can\'t figure out how to read my player
ID: 3571522 • Letter: H
Question
Hello, I need help in my 21 card game. I can't figure out how to read my player and dealer deck array until it hits a space and from there add up the score.
For example
Jack of Hearts => Jack
8 of Spades =>8
import java.util.Random;
import java.util.Scanner;
public class Gamble
{
static Random generator = new Random();
static int userChoice;
static final int deckSize = 52, suitNum = 4, rankNum = 13;
public static String[] shuffledDeck = new String[deckSize];
public static String[] playerDeck = new String[10];
public static String[] dealerDeck = new String[10];
public static void main(String[] args)
{
info();
if (userChoice == 1)
{
game();
}
else if (userChoice == 2)
{
gameInfo();
}
else
{
//endGame;
}
}
//info method gives menu of game
public static int info()
{
Scanner keyboard = new Scanner (System.in);
System.out.println("Welcome to Blackjack! Today is your lucky day" +
"becuase you get a free starting balance of $100." + " (1) Start (2)" +
" Info (3) Quit");
userChoice = keyboard.nextInt();
return userChoice;
}
public static void game()
{
gameDeck();
Deck();
}
public static void gameDeck()
{
String[] suits = {"Clubs", "Diamonds", "Hearts", "Spades"};
String[] ranks = {"2", "3", "4", "5", "6", "7", "8", "9",
"10", "Jack", "Queen", "King", "Ace"};
// initialize deck
String[] deck = new String[deckSize];
for (int initializeRank = 0; initializeRank < rankNum; initializeRank++)
{
for (int initializeSuit = 0; initializeSuit < suitNum; initializeSuit++)
{
deck[suitNum * initializeRank + initializeSuit] = ranks[initializeRank] +
" of " + suits[initializeSuit];
}
}
shuffle(deck);
}
public static String[] shuffle(String deck[])
{
for (int shuffle = 0; shuffle < shuffledDeck.length; shuffle++)
{
int r = generator.nextInt(deckSize);
if(!deck[r].equals(""))
{
shuffledDeck[shuffle] = deck[r];
deck[r] = "";
}
else
{
shuffle--;
}
}
return shuffledDeck;
}
public static void Deck()
{
for (int i = 0; i < 3; i++)
{
deal(playerDeck, dealerDeck);
}
System.out.println(" Player:");
for (int i = 0; i < 2; i++)
{
System.out.println(playerDeck[i]);
}
System.out.println(" Dealer:");
for (int i = 0; i < 2; i++)
{
System.out.println(dealerDeck[i]);
}
}
public static int deal(String[] playerDeck, String[] dealerDeck)
{
int cardNum = 0;
for (int d = 0; d < 2; d++)
{
playerDeck[d] = shuffledDeck[cardNum];
cardNum++;
dealerDeck[d] = shuffledDeck[cardNum];
cardNum++;
}
return cardNum;
}
public static void gameInfo()
{
System.out.println(" Blackjack, also known as twenty-one, is the most" +
" widely played casino banking game in the world. Blackjack is a" +
" comparing card game between a player and dealer, meaning players" +
" compete against the dealer but not against other players. It is" +
" played with one or more decks of 52 cards. The objective of the game" +
"is to beat the dealer in one of the following ways:" +" •Get 21" +
" points on the player's first two cards (called a "blackjack" or" +
" "natural"), without a dealer blackjack •Reach a final score higher" +
"than the dealer without exceeding 21; or •Let the dealer draw additional" +
"cards until his or her hand exceeds 21.");
}
/*public static void winnerCheck()
{
if(dealersValue == 17 ) {
System.out.println("Dealer Has 17 - Dealer Sticks ");
y = 1;
if(usersValue < 17) {
System.out.println("You Have: " + usersValue + " You Lost");
} else if(usersValue == dealersValue) {
System.out.println("You Have: " + usersValue + " You Drew");
} else {
System.out.println("You Have: " + usersValue + " You Won!");
}
}
if(dealersValue > 17 && dealersValue < 21) {
System.out.println("Dealer Has: " + dealersValue + " Dealer Sticks " );
y = 1;
if(usersValue < 18) {
System.out.println("You Have: " + usersValue + " You Lost");
} else if(usersValue == dealersValue) {
System.out.println("You Have: " + usersValue + " You Drew");
} else {
System.out.println("You Have: " + usersValue + " You Won!");
}
}
}
Explanation / Answer
Add this code in your code for displaying user info and deck value
//for display player and deck
public static void display()
{
If(strcmp(“Playername”,” ”)==0)
{
If(deckSize[]!=empty)
{
For(int i=0;i<deckSize[].legnth;i++)
{
System.out.println(“ +username of +uservalue => + playerDeck[i]);
System.out.println(“ + dealersValue of +suit[]=> +ranks[]);
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.