In: Computer Science
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]); } }
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: