+1(978)310-4246 credencewriters@gmail.com
  

Description

Write a Java program that will allow a user to play one hand of the card game Blackjack, or
Twenty-One, as its sometimes known. The rules, for the purpose of this assignment, are as
follows:
Blackjack rules:
ï‚· The game is played with a deck of standard paying cards.
 A player is dealt 2 cards to start the “hand”
 A player’s score is the sum of the value of all cards in their hand, where:
o Cards numbered 2 through 10 are worth their numerical value
o Picture cards (Jack, Queen, King) are worth 10 points
o Aces are worth either 1 or 11 points
 The player then has the choice to “hit” or “stand”
o If the player “stands”, their turn is finished
o If the player “hits”, they are dealt another card and the score is updated
 Play continues until the player “stands”, gets 21, or “busts” (see below)
o The current score is then final
ï‚· The object is to get 21, or as close as possible, without going over
 If the final score is over 21, the player “busts”, i.e. loses
o Initially, an ace is valued at 11, but…
o if an 11 would lead to a “bust”, it is re-valued at 1
ï‚· If the player gets 21 with the first 2 cards (i.e. an Ace and a card worth 10), they have
“Blackjack”
Normally, Blackjack is played competitively, where hands are dealt to both players and the
dealer in a casino. Bets are placed and the players attempt to outscore the dealer, thereby
winning their bet. In this assignment, we will only deal one player’s hand (no dealer, no bets).
Assignment Description:
For this assignment, we will associate each card in a standard playing deck with an integer “id”
code having a value ranging from 0 to 51. Each card will also be represented with a 2 character
string of the form “RS” where R is the rank and “S” is the suit. The rank and the suit can be
determined using the following arrays and formulas:
String[] ranks = {“A”, “2”, “3”, “4” ,”5″, “6”, “7”, “8”, “9”, “10”, “J”, “Q”, “K”};
String[] suits = {“S”, “H”, “C”, “D”};
R= id % 13; // the one character string containing the card rank
S= id / 13 // the one character string containing the card suit
Thus for an id == 31, the 2 character card string is “6C”, representing the six of clubs
Create a Java class called Assignment1. Include a main() method in which one hand of
Blackjack will be played. The program should, in main(), deal the first two cards and display
both the cards in the hand (a string) and the score. Then, ask the player to hit or stand,
continually displaying the result with each hit until the player stands, gets 21 (or blackjack), or
busts. Display the final result and exit.
The main routine should contain two arrays as follows:
int deck[52]; // contains the integer id codes for all cards in the deck
int hand[12]; // contains the integer id codes for the cards in the player’s hand
Note that, before starting play, the deck should be populated with the 52 card id’s and shuffled
(the cards randomly arranged) using the shuffling routine provided below. Cards are then “dealt”
in order from the array and added to the player’s hand. The number of cards in the player’s
hand will need to be tracked and updated as the game progresses.
Write the following methods for the Assignment1 class and use them to play the game in main().

CSC243 – Assignment 1
Objective:
Write a Java program that will allow a user to play one hand of the card game Blackjack, or
Twenty-One, as its sometimes known. The rules, for the purpose of this assignment, are as
follows:
Blackjack rules:
ï‚·
ï‚·
ï‚·
ï‚·
ï‚·
ï‚·
ï‚·
ï‚·
The game is played with a deck of standard paying cards.
A player is dealt 2 cards to start the “hand”
A player’s score is the sum of the value of all cards in their hand, where:
o Cards numbered 2 through 10 are worth their numerical value
o Picture cards (Jack, Queen, King) are worth 10 points
o Aces are worth either 1 or 11 points
The player then has the choice to “hit” or “stand”
o If the player “stands”, their turn is finished
o If the player “hits”, they are dealt another card and the score is updated
Play continues until the player “stands”, gets 21, or “busts” (see below)
o The current score is then final
The object is to get 21, or as close as possible, without going over
If the final score is over 21, the player “busts”, i.e. loses
o Initially, an ace is valued at 11, but…
o if an 11 would lead to a “bust”, it is re-valued at 1
If the player gets 21 with the first 2 cards (i.e. an Ace and a card worth 10), they have
“Blackjack”
Normally, Blackjack is played competitively, where hands are dealt to both players and the
dealer in a casino. Bets are placed and the players attempt to outscore the dealer, thereby
winning their bet. In this assignment, we will only deal one player’s hand (no dealer, no bets).
Assignment Description:
For this assignment, we will associate each card in a standard playing deck with an integer “id”
code having a value ranging from 0 to 51. Each card will also be represented with a 2 character
string of the form “RS” where R is the rank and “S” is the suit. The rank and the suit can be
determined using the following arrays and formulas:
String[] ranks = {“A”, “2”, “3”, “4” ,”5″, “6”, “7”, “8”, “9”, “10”, “J”, “Q”, “K”};
String[] suits = {“S”, “H”, “C”, “D”};
R= id % 13; // the one character string containing the card rank
S= id / 13
// the one character string containing the card suit
Thus for an id == 31, the 2 character card string is “6C”, representing the six of clubs
Create a Java class called Assignment1. Include a main() method in which one hand of
Blackjack will be played. The program should, in main(), deal the first two cards and display
both the cards in the hand (a string) and the score. Then, ask the player to hit or stand,
continually displaying the result with each hit until the player stands, gets 21 (or blackjack), or
busts. Display the final result and exit.
The main routine should contain two arrays as follows:
int deck[52]; // contains the integer id codes for all cards in the deck
int hand[12]; // contains the integer id codes for the cards in the player’s hand
Note that, before starting play, the deck should be populated with the 52 card id’s and shuffled
(the cards randomly arranged) using the shuffling routine provided below. Cards are then “dealt”
in order from the array and added to the player’s hand. The number of cards in the player’s
hand will need to be tracked and updated as the game progresses.
Write the following methods for the Assignment1 class and use them to play the game in main().
Assignment1 methods:
private static String getCardName(int id)
ï‚· Given a card id, return the 2 character string for the card
private static int idValue(int id)
ï‚· Given a card id, return the point value for the card (assume aces are worth 11)
private static int getScore(int[] hand, int num)
 Given a player’s hand, return the score
ï‚· num is the number of cards in the hand
 Note: start by valuing an ace as 11 points, but reduce it to 1 if needed to avoid a “bust”
ï‚· Hint: use idValue() for each card to get the initial score.
private static void hitHand(int[] hand, int id, int num)
ï‚· Add the given card id to the hand
ï‚· num is the number of cards in the hand prior to adding the card
private static void printHand(int[] hand, int num)
ï‚· Return a string containing a space delimited listing of the 2-character card strings in the hand
Finally, add the following method to the class. Use it to shuffle the deck before starting the game.
// Implementation of the Fisher-Yates shuffling algorithm
// randomly shuffles (rearranges the ordering) of an array of integers
private static void shuffleFY(int[] array) {
for (int i=0; i < array.length-1; i++) { int rindx = i + (int)(Math.random()*(array.length - i)); int temp = array[i]; array[i] = array[rindx]; array[rindx] = temp; } } Use turnin to submit the Assignment1.java file (only). Purchase answer to see full attachment

error: Content is protected !!