Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Rules of the game In Blackjack, the goal is to assemble the hand with the highest value without going over a value of 21. Cards

Rules of the game In Blackjack, the goal is to assemble the hand with the highest value without going over a value of 21. Cards numbered 2-10 are worth their stated value (i.e., their value is equal to their rank). Face cards (Jacks, Queens, and Kings) have a value of 10. Aces have a value of 11, unless doing so would make the hands value exceed 21, in which case they have a value of 1. The best outcome is a combination of an ace with either a 10 or a face card; this gives a total value of 21 from only two cards, which is known as Blackjack.

The dealer starts by dealing two cards to the user of the game and two cards to herself. The dealers first card is dealt face down, and the other cards are revealed.

The user then begins his turn. He repeatedly decides whethers to request another card (which is known as a hit) or to hold his current hand. The users turn continues until he chooses to hold, or until the value of his hand is 21 or more.

Once the users turn is completed, the dealer begins her turn. She first reveals her hidden card. Then, provided that the user has not gone over 21, the dealer gives herself hits as needed in an attempt to win the game. In our version of Blackjack, the dealer will employ the following rules:

  • If the users hand has a value that is less than 17, the dealer should take hits until the value of her hand exceeds the value of the users hand.
  • If the users hand has a value of 17 or more, the dealer should take hits until the value of her hand matches or exceeds the value of the users hand, unless the user has Blackjack (in which case the dealer should hold, regardless of the value of her own hand).

Note: If the user goes over 21, the dealer wont even be asked to decide whether to take a hit.

Sample runs Here are some sample runs of the game:

  • first sample run (random seed = 105)
  • second sample run (random seed = 110)

Notes about the sample runs:

  • See the section below entitled Testing your code for information about how to specify a random seed.
  • In each sample run, user inputs are underlined.

Structure of the program The code for your Blackjack program will be divided into a number of different classes. In particular, you will use the Card class that you wrote in Problem Set 2.

In addition, we are giving you complete or nearly complete implementations of the following classes:

  • Deck.java This class is a blueprint for objects that represent a deck of 52 playing cards. These objects have several methods, the most useful of which are the shuffle and dealCard methods. It uses a random-number generator when shuffling the cards to get a different ordering of the deck each time. When testing your code, its possible to control the numbers generated by the random-number generator so that you can get repeatable hands of cards. See the section entitled Testing your code for more details. You should not modify the code in this class.

  • Blackjack.java This class contains the main method of the program; you will run this class to start the program. The class also includes several static methods, two of which you will need to complete or modify. Note that this class is not a blueprint class. It has no fields, and all of its methods are static because they do not need a called object. You should not make any modifications to this class except for the changes specified in Tasks 3 and 4. In addition, this class will not compile until you complete Task 2 below.

Download these files, storing them in the same folder in which you stored your Card.java file.

Task 1: review the provided code Begin by reading over the code that we have given you. In particular, you should look at how the Blackjack class will make use of the types of objects that you will create below. You do not need to fully understand how the Deck class works, but we encourage you to look it over.

Task 2: create a blueprint class for a player Write a class named Player that serves as a blueprint for objects that represent a single Blackjack player. Save it in the same folder as the classes that you downloaded above. Import the java.util package at the start of the file.

Each Player object should have the following components:

  1. three fields:

    • one called name to keep track of the players name (a single string)
    • one called hand for an array to hold the cards in the players hand
    • one called numCards to keep track of how many cards are currently in the players hand Make sure that your field definitions prevent direct access by code from outside the class.
  2. a constructor that takes a single parameter for the name of the player. It should initialize all of the fields. Among other things, it should create the array that will store the cards. Make the collection big enough to store the maximum number of cards in a given hand (11). Use the class constant Blackjack.MAX_CARDS_PER_PLAYER to specify this value, rather than hard-coding the integer 11.

  3. an accessor named getName that returns the players name.

  4. an accessor named getNumCards that returns the current number of cards in the players hand.

  5. a mutator named addCard that takes a Card object as a parameter and adds the specified card to the players hand, filling the array from left to right. It should throw an IllegalArgumentException if the parameter is null, or if the player already has the maximum number of cards.

  6. an accessor named getCard that takes an integer index as a parameter and returns the Card at the specified position in the players hand, without actually removing the card from the hand. For example, if p is a Player, p.getCard(0) should return the card at position 0 in ps hand i.e., the first/leftmost card. If the specified index does not correspond to one of the cards in the hand, the method should throw an IllegalArgumentException.

  7. an accessor method named getHandValue that computes and returns the total value of the players current hand i.e., the sum of the values of the individual cards. Use the getValue method from the Card class to get each cards value. One tricky aspect of this method is handling any Aces that may be present in the hand. The getValue method will return 1 for an Ace, but you may need to change its value to 11 depending on the values of the rest of the cards in the hand. Hint: If there is more than one Ace in the hand, at most one of them can have a value of 11, and you should only give it a value of 11 if doing so wont cause the hands total value to exceed 21.

  8. an accessor method named printHand that prints the current contents of the players hand, followed by the value of the players hand. For example:

    4H 7C QD (value = 21) 

    Notes:

    • There should be two spaces between the strings for the individual cards, and two spaces between the last cards string and the left parenthesis at the start of the value.

    • This method should not print the player identifiers (dealer: or you: ) that come before the hands in the sample runs.

  9. an accessor method named hasBlackjack that returns true if the player has Blackjack (a two-card hand with a value of 21), and false otherwise. This method will be relatively simple to write if you take advantage of one or more of the other methods that youve already written.

  10. an accessor method called wantsHit that should return true if the player wants another hit, and false if the player does not want another hit. The method should take two parameters: a Scanner object that can be used to read from the console, and a Player object representing the players opponent (in that order). In this version of the method:

    • You should print the appropriate prompt asking if the user wants a hit (see the sample runs), and read a string from the console using the nextLine method of the Scanner passed in as a parameter. (Make sure that you do not use the Scanners next() method.) The method should return true if the user enters y or Y, and it should return false if the user enters any other input (even if its something other than n or N).
    • You should ignore the second parameter (the Player object for the opponent). The version of this method that you will write in Task 3 will use this second parameter, which is why we include it here as well.
  11. a mutator method called discardCards that should get rid of all of the cards in the players hand, to prepare for a new round of the game. There are different ways to accomplish this. The key thing is to ensure that immediately after this method is called, all other methods that depend on the number of cards in the players hand should behave as if the player has no cards.

After completing all of Task 2, you should be able to run the Blackjack class to play the game. At this point, the computer will be represented by a Player object, which means that you will be able to see all of its hand, and that its decisions about hits will be determined using the wantsHit method that you implemented above. In other words, you will need to decide on hits for both the user (i.e., you) and the dealer.

If the Blackjack class doesnt compile, that probably means that there are problems in the headers of one or more of your Player methods. Change your Player class as needed until the Blackjack class compiles. Remember that you are not allowed to change the Card or Deck classes in any way. In addition, you should not change the Blackjack class at this point in the process.

When running the program, you must highlight Blackjack.java in the list of files before using F5 to run the program.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database And Expert Systems Applications 22nd International Conference Dexa 2011 Toulouse France August/September 2011 Proceedings Part 1 Lncs 6860

Authors: Abdelkader Hameurlain ,Stephen W. Liddle ,Klaus-Dieter Schewe ,Xiaofang Zhou

2011th Edition

3642230873, 978-3642230875

More Books

Students also viewed these Databases questions