Question

In: Computer Science

Play a blackjack. Write a Java program that starts from a deck of 52 cards and...

Play a blackjack. Write a Java program that starts from a deck of 52 cards and one player plays again a dealer. Use a simple rule explained here. Ace can be counted as 1 only for simplicity. Jack, Queen, King are counted as 10. At the beginning, player receives two cards. Dealer receives two cards, but shows one and hides one. Program asks player if player wants to receive another card or not. (player can continue to receive new cards until the sum does not exceed 21) Anyone who gets the total of 21 or closer to 21 wins. But when anyone exceeds 21, one loses. Dealer shows the hidden card when players stops receiving a new card. Dealer should continue receiving a new card if the dealer’s total is equal or less than 16. If the dealer’s total is above 16, thendealer can not receive any more new card. Then program can decide who wins.

The following code is an incomplete code

import java.util.Scanner;

public class BlackJackIncomplete {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int []  aDeck = new int[52];  // 13 x 4

        // create a deck of card represented by integer 0 to 51
        for (int i = 0; i < aDeck.length; i++) {
            aDeck[i] = i;
            // initial card is all sorted, not shuffled
        }
        shuffle(aDeck);
        int curP = 0;
        int[] dealer = new int[10];
        int[] player = new int[10];

        dealer[0] = aDeck[curP++];
        player[0] = aDeck[curP++];
        dealer[1] = aDeck[curP++];
        player[1] = aDeck[curP++];
        int numOfCardsPerPlayer = 2;
        boolean gameover = false;
        printTable(dealer, player, numOfCardsPerPlayer);
        System.out.print("Do you want a new card?, then say 'hit'. If not, say 'stand'. It ends your turn. (hit/stand)");

        while (!gameover) {
            String playerChoice = input.next();
            if(playerChoice.equals("stand")) {
                System.out.println("Players stops at" + cards(player, numOfCardsPerPlayer));
                int playerTotal = 0;
                for (int x = 0; x <numOfCardsPerPlayer; x++)
                    playerTotal += cardNum(player[x]);
                if (playerTotal > 21) {
                    System.out.println("Dealer won!");
                    gameover = true;
                    break;
                }
                do {
                    System.out.println("Dealer's hand " + cards(dealer, numOfCardsPerPlayer));
                    int dealerTotal = 0;
                    for (int x = 0; x <numOfCardsPerPlayer; x++)
                        dealerTotal += cardNum(dealer[x]);
                    // Dealer will hit until his/her cards total 17 or higher. BlackJack Rule
                    if (dealerTotal <= 16) {
                        dealer[numOfCardsPerPlayer] = aDeck[curP++];
                        numOfCardsPerPlayer++;
                    }
                    else {
                        if (dealerTotal > 21) {
                            System.out.println("You won!");
                        }
                        if (dealerTotal > playerTotal) {
                            System.out.println("Dealer won!");
                        }
                        else {
                            System.out.println("You won!");
                        }
                        gameover = true;
                        break;
                    }
                } while (true);
            }
            else {  // player says "hit". 
                    //Fill the complete code here

            }
        }

    }
    // index 0 -> Ace (1)
    // index 1 .. 9 -> card (2 - 10)
    // index 10: Jack, index 11: Queen, Index 12: King

    public static int cardNum(int n) {
        int remainderN = n % 13;
        remainderN += 1;   // shifting as explained above

        if (remainderN > 10)
            remainderN = 10;
        return remainderN;
    }

    public static String cards (int[] d, int numOfCardsPerPlayer) {
        String cardsInHands = "";
        for(int i =0; i<numOfCardsPerPlayer; i++ )
            cardsInHands = cardsInHands.concat("[" + cardNum(d[i]) + "]");
        return cardsInHands;
    }

    public static void printTable(int[] d, int[] p, int numOfCardsPerPlayer) {
        System.out.print("Dealer's hand ");
        for(int i =0; i<numOfCardsPerPlayer - 1; i++ )
            System.out.print("[" + cardNum(d[i]) + "]");
        System.out.println("[X]");

        System.out.println("Player's hand " + cards(p, numOfCardsPerPlayer));
    }

    public static void shuffle(int [] aDeck) {
        for (int i = 0; i < aDeck.length; i++) {
            int j = (int) (Math.random() * aDeck.length);  // j is an integer range from 0 to 51
            swap(aDeck, i, j);
        }
    }

    public static void swap(int a[], int i, int j) {
        int tmp = a[i];
        //System.out.println("n:" + a[0] + " m:" + a[1]);
        a[i] = a[j];
        a[j] = tmp;
        //System.out.println("n:" + a[0] + " m:" + a[1]);
    }
}

Solutions

Expert Solution

import java.util.Scanner;

public class BlackJackIncomplete {

   public static void main(String[] args) {

   Scanner input = new Scanner(System.in);
int [] aDeck = new int[52]; // 13 x 4

// create a deck of card represented by integer 0 to 51
for (int i = 0; i < aDeck.length; i++) {
aDeck[i] = i;
// initial card is all sorted, not shuffled
}
shuffle(aDeck);
int curP = 0;
int[] dealer = new int[10];
int[] player = new int[10];

dealer[0] = aDeck[curP++];
player[0] = aDeck[curP++];
dealer[1] = aDeck[curP++];
player[1] = aDeck[curP++];
int numOfCardsPerPlayer = 2;
boolean gameover = false;
  
printTable(dealer, player, numOfCardsPerPlayer);
System.out.print("Do you want a new card?, then say 'hit'. If not, say 'stand'. It ends your turn. (hit/stand) ");

while (!gameover) {
String playerChoice = input.next();
if(playerChoice.equals("stand")) {
System.out.println("Players stops at" + cards(player, numOfCardsPerPlayer));
int playerTotal = 0;
for (int x = 0; x <numOfCardsPerPlayer; x++)
playerTotal += cardNum(player[x]);
if (playerTotal > 21) {
System.out.println("Dealer won!");
gameover = true;
break;
}
do {
System.out.println("Dealer's hand " + cards(dealer, numOfCardsPerPlayer));
int dealerTotal = 0;
for (int x = 0; x <numOfCardsPerPlayer; x++)
dealerTotal += cardNum(dealer[x]);
// Dealer will hit until his/her cards total 17 or higher. BlackJack Rule
if (dealerTotal <= 16) {
dealer[numOfCardsPerPlayer] = aDeck[curP++];
numOfCardsPerPlayer++;
}
else {
if (dealerTotal > 21) {
System.out.println("You won!");
}
else if (dealerTotal > playerTotal) {
System.out.println("Dealer won!");
}
else {
System.out.println("You won!");
}
gameover = true;
break;
}
} while (true);
}
else { // player says "hit".

   // add a card to player's hand and dealer's hand
   player[numOfCardsPerPlayer] = aDeck[curP++];
   numOfCardsPerPlayer++;
  
   dealer[numOfCardsPerPlayer-1] = aDeck[curP++];
  
   // display both the hands
   printTable(dealer, player, numOfCardsPerPlayer);
  
   // take user input
   System.out.print("Do you want a new card?, then say 'hit'. If not, say 'stand'. It ends your turn. (hit/stand) ");
}
}

}
  
   // index 0 -> Ace (1)
// index 1 .. 9 -> card (2 - 10)
// index 10: Jack, index 11: Queen, Index 12: King

public static int cardNum(int n) {
int remainderN = n % 13;
remainderN += 1; // shifting as explained above

if (remainderN > 10)
remainderN = 10;
return remainderN;
}

public static String cards (int[] d, int numOfCardsPerPlayer) {
String cardsInHands = "";
for(int i =0; i<numOfCardsPerPlayer; i++ )
cardsInHands = cardsInHands.concat("[" + cardNum(d[i]) + "]");
return cardsInHands;
}

public static void printTable(int[] d, int[] p, int numOfCardsPerPlayer) {
System.out.print("Dealer's hand ");
for(int i =0; i<numOfCardsPerPlayer - 1; i++ )
System.out.print("[" + cardNum(d[i]) + "]");
System.out.println("[X]");

System.out.println("Player's hand " + cards(p, numOfCardsPerPlayer));
}

public static void shuffle(int [] aDeck) {
for (int i = 0; i < aDeck.length; i++) {
int j = (int) (Math.random() * aDeck.length); // j is an integer range from 0 to 51
swap(aDeck, i, j);
}
}
  
public static void swap(int a[], int i, int j) {
int tmp = a[i];
//System.out.println("n:" + a[0] + " m:" + a[1]);
a[i] = a[j];
a[j] = tmp;
//System.out.println("n:" + a[0] + " m:" + a[1]);
}
}

//end of program

Output:


Related Solutions

Write a program in c++ that picks four cards from a deck of 52 cards and...
Write a program in c++ that picks four cards from a deck of 52 cards and computes the sum of the four cards. An Ace, King, Queen, and Jack represent 1, 13, 12, and 11, respectively. Your program should display the number of picks that yields the sum of 24. You are not allowed to use arrays. Declare four variables to hold the card values.
Design a c++ program to simulate the BlackJack game. Rules: 1) single deck, 52 cards -both...
Design a c++ program to simulate the BlackJack game. Rules: 1) single deck, 52 cards -both player and dealer are taking cards off the same deck (the same 52 values). 2) display BOTH cards of the players, and ONE card of the computer. 3) Check for blackjack (starting value of 21 for computer or player) -if either side has a blackjack end of the game, next hand of blackjack 4) Computer must hit if 16 or below. 5) Computer must...
write on eclipse java Write a program named lab5 that will play a game of Blackjack...
write on eclipse java Write a program named lab5 that will play a game of Blackjack between the user and the computer. Create a second class named Card with the following: Define the following private instance variables: cardValue (int) & cardSuite (String) Write a constructor with no parameters that will Set cardValue to a random number between 1 and 13 Generate a second random number between 0 and 3 and assign cardSuite a string based on its value (0 –...
A standard deck of cards has 52 cards, four each of 2,3,4,5,6,7,8,9,10,J,Q,K,A. In blackjack, a player...
A standard deck of cards has 52 cards, four each of 2,3,4,5,6,7,8,9,10,J,Q,K,A. In blackjack, a player gets two cards and adds their values. Cards count as their usual numbers, except Aces are 11 (or 1), while K, Q, J are all 10. “Blackjack” means getting an Ace and a value ten card. What is probability of getting a blackjack? What is probability of getting 19? (The probability that the sum of your cards is 19, using Ace as 11) Use...
Four cards are dealt from a deck of 52 cards. ​(a) What is the probability that...
Four cards are dealt from a deck of 52 cards. ​(a) What is the probability that the ace of spades is one of the 4 cards​? (b) Suppose one of the 4 cards is chosen at random and found not to be the ace of spades. What is the probability that none of the 4 cards is the ace of​ spades? ​(c) Suppose the experiment in part​ (b) is repeated a total of 10 times​ (replacing the card looked at...
1. Two cards are drawn from a deck of cards (consisting of 52 cards: 13 cards...
1. Two cards are drawn from a deck of cards (consisting of 52 cards: 13 cards for each of the four suits, Spades (S), Hearts (H), Diamonds (D) and Clubs (C)). Drawing is done with replacement, that is, the first card is drawn, recorded and put back in the deck, then the second card is drawn and recorded. Assuming an outcome is recorded in the order which two cards are drawn, what is the sample space for this (random) phenomenon?...
Two cards are randomly selected from a deck of 52 playing cards. (a) What is the...
Two cards are randomly selected from a deck of 52 playing cards. (a) What is the probability they constitute a pair (that is, that they are of the same denomination)? (b) What is the conditional probability they constitute a pair given that they are of different suits?
Four cards are drawn at random from a standard deck of 52 cards. a. What’s the...
Four cards are drawn at random from a standard deck of 52 cards. a. What’s the probability that at least one card is a 7? b. What’s the probability that 3 of the 4 cards are the same suit? c. What’s the probability that they are all the same suit?
Three cards are drawn from a deck of 52 cards without replacement. (a) What is the...
Three cards are drawn from a deck of 52 cards without replacement. (a) What is the probability that the third card is a spade (♠) given that the first card is a spade? (b) What is the probability that all cards are spades given that at least one of them is a spade? (c) Let Y be the number of black cards drawn. What is the probability that all 3 cards are black given that the first card is a...
Three cards are drawn from a deck of 52 cards without replacement. (a) What is the...
Three cards are drawn from a deck of 52 cards without replacement. (a) What is the probability that the third card is a spade (♠) given that the first card is a spade? (b) What is the probability that all cards are spades given that at least one of them is a spade? (c) Let Y be the number of black cards drawn. What is the probability that all 3 cards are black given that the first card is a...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT