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

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 !!