Part B: Building the HighLow Game 1. Now that we have our classes, we can build a game to demonstrate how they can be used. 2. The flow of this game is fairly straightforward. First, a card is drawn from the deck and shown to the player. The player then makes a guess as to whether or not they think the next card will have a higher or lower card value compared to the one they were shown. If they guess correctly, they win. 3. Inside the main method of HighLowGame.java, add the following lines of code. We need the Deck object to generate cards, and the Scanner to capture the user choices. Deck deck = new Deck(); Scanner sc= new Scanner(System. in); sc.close(); Remember to add the import statement to be able to use the Scanner class. 4. With those two essential elements in place, add the next section (above the scnr.close() line). Here, we start the game by drawing our first card, communicating that result with the user, and prompting them to make a guess. Card card 1= deck drawCard () ; System. out. println ("The first card is " + card1. declareCard()); System. out. println("Will the next card be higher or lower?"); System. out. print("Enter 1 for lower, 2 for higher: "); int choice =5cnr. nextint(); 5. Once we've captured the user's guess, we can draw the second card and inform the user of what it is:
6. Now that both cards have been drawn, we need to determine if the player won, lost, or tied (in the event the cards had the same value). Now there are many, many ways of accomplishing this but consider the following: boolean higher = card2.getvalue() ) card1.getvalue(); if (card1.getvalue() -- card2,getvalue()) System.out.println ("Card values were the same, no winner or loser this round."); \} else if ((higher \& choice--2) II (!higher \&\& choice -- 1)) \{ 5ystem.out.println("Winner!"); \} else \{ 5ystem.out.println("Sorry, your guess was incorrect :("); \} 7. In this setup, we first generate a boolean to establish the relationship between the two cards. The first IF statement needs to cover the situation where the two cards have the same value, because our boolean would be false in the situation where the cards have the same value, which might lead us to make the wrong assumptions about the game result. Next, we cover our two win conditions inside a compound conditional (card2 is higher and player chose higher OR card2 is lower and player chose lower). Finally, we can conclude that if the game did not result in a win or a tie, the only thing left is that the player lost, so we cover that in our final else block. 8. Run this code and test your game. When you are confident your game runs correctly, show your work to an instructor or a TA to receive credit for this section. Part C: Blackjack! 1. In this portion of the assignment, we're going to be repurposing our existing Card and Desk classes to create a more complicated game: Blackjack. For those of you who don't know how Blackjack is played, you can review an in-depth summary of the rules here. 2. The TLDR version of these rules is that the Player and the House play against one another. The winner is the one who ends up with the higher value set of cards, up to 21. Anything over 21 is a loss for the Player or House. 3. Start by creating a new file, Blackjack.java. Give it a main method. 4. Just like with the previous game, create a Deck object and a Scanner object. As the last line of your main method, close the scanner. Remember to place all new code in the main method above this statement 5. Before we can start coding the game, we have one important rule change that affects how the cards work. In Blackjack, a Jack, Queen, or King all have a value of 10, and Ace cards are worth either 1 or 11 . For simplicity's sake, we're going to set the value of
d.java public class Card \{ //FIELDS private int value; private String suit; // CONSTRUCTOR public Card(int value, String suit) \{ this value = value; this.suit = suit; \})// end constructor // ACCESSOR METHODS public string getsuit()\{ return this.suit; \} public int getValue()\{ return this.value; \} // declareCard public string declareCard() \{ String result = null; if(this.value ==11){ result = "the Jack of "+this.suit: 3 else if(this. value ==12){ result = "the Queen of "+this.suit; 3 else if(this. value ==13){ result = "the King of "+this.suit; 3 else if(this.value ==1){ result = "the Ace of "+this.suit; \} else result = "the "+this.value + " of "+this.suit; 3 return result; \}
Deck.java