So for this project, we’re going to make a game. This game will make use of a se
ID: 3696933 • Letter: S
Question
So for this project, we’re going to make a game. This game will make use of a server to join two clients together for a modified game of Blackjack. I’ll explain how this modified game works below.
Here’s your task: You’re going to create exactly two classes. One class will act as the functional server. The main method of this server will setup the connection on a localhost, find the two clients, let them know which one is Player 1 and which one is Player 2 (depending on the order they connect), and then manage the game as it’s being played.
This is important: For each player’s turn they should start by displaying their current score to the user. Then, the user should be prompted to select whether they want to “Hit” or “Stand”. This option selection can be done any way you choose, but invalid entries must be accounted for*. If the player chooses to “Hit”, the server should deal a “card” to the player, which will consist of choosing a random number between 1 and 13. The server sends this “card” to the player, and the player receives the card and adds it to their score. If the card is above a 10, then add a 10 to the player’s score instead of the original value (In a standard Blackjack game, Jacks, Queens, and Kings still count as 10). After they receive the card or if they choose to “Stand”, the next player should take their turn. If either player takes a “Hit”, and the card they’re dealt raises their score total above 21, then the game should immediately end and the other player is the winner (this is known as a “Bust”). Likewise, if both players choose to “Stand”, then both players should be shown the final scores and the game is then over.
*for example, if you choose to say “Enter (1) For a Hit or enter (2) to Stand”, then any entry other than 1 or 2 should be counted as invalid and should loop to allow the player another entry. If you choose something like “Enter (H)it or (S)tand”, then valid entries should be “H”, “S”, and ideally “h” and “s”.
You May: Get user input in any way you choose, though you must check for invalid entries and prompt a re-entry if invalid data is entered; you may choose to implement an additional feature that allows a 1 (Ace) to be worth either 1 or 11, however this is not necessary; you may choose to deal two cards to each player to begin the game, however this is not necessary; you may choose to display the first card drawn to each player to both players, however this is not necessary; you may use driver classes and additional methods other than the main method in each class, however this is not necessary to complete this program.
You May Not: Exceed the scope of a console-based application – this means no GUIs, etc; you may not allow for more than two players by any means; you may not use two different client classes – one client must function as either player.
Can someone please tell me how to do this program?
Explanation / Answer
import java.util.Random; import java.util.Scanner; public class SeventySix { public static void main(String[] args) { Scanner keyboard = new Scanner(System.in); //52 Cards, Aces = 11, Picture cards = 10, Ace's cannot be reduced to 1. int[] newCard = {2,2,2,2,3,3,3,3,4,4,4,4,5,5,5,5,6,6,6,6,7,7,7,7,8,8,8,8,9,9,9,9,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,11,11,11,11}; //Shuffle. Once per game. shuffleArray(newCard); //Start BlackJack. System.out.println("Welcome to BlackJack!"); System.out.println(); System.out.println("You get a " + newCard[0] + " and a " + newCard[1] + "."); int playerTotal = newCard[0] + newCard[1]; System.out.println("Your total is " + playerTotal + "."); System.out.println(); //Player can get blackjack/bust in the 1st deal. - awaiting betting system (enhanced bets for blackjack in first round) if (playerTotal == 21){ System.out.println("Blackjack, you win."); System.exit(0); } if (playerTotal > 21){ System.out.println("Bust, You lose."); System.exit(0); } // Dealer cards System.out.println("The dealer has a " + newCard[2] + " showing, and a hidden card."); int dealerTotal = newCard[2] + newCard[3]; if (dealerTotal > 21){ //Dealer bust check. System.out.println(); System.out.println("Dealers total is " + dealerTotal + "."); System.out.println("Dealer is bust, you win!"); System.exit(0); } if (dealerTotal == 21){ //Dealer blackjack check. System.out.println(); System.out.println("Dealer reveals his second card: " + newCard[3] + "."); System.out.println("Dealers total is " + dealerTotal + "."); System.out.println(); System.out.println("Dealer has BlackJack, you lose."); System.exit(0); } System.out.println("His total is hidden."); System.out.println(); // Hit or Stay for player. System.out.print("Would you like to "hit" or "stay"? "); String hitStay = keyboard.next(); System.out.println(); //cc = card count int cc = 4; if (hitStay.equalsIgnoreCase("hit")){ // While loop to ensure different cards & multiple "hits". while (playerTotal < 21 && hitStay.equalsIgnoreCase("hit")){ if (hitStay.equalsIgnoreCase("hit")){ System.out.println("You drew a " + newCard[cc] + "."); playerTotal = playerTotal + newCard[cc]; System.out.println("Your total is " + playerTotal + "."); System.out.println(); cc++; //Adds 1 to ensure next card is different. // Bust & Blackjack check. if (playerTotal > 21){ System.out.println("You are bust, You lose."); System.exit(0); } if (playerTotal == 21){ System.out.println("Blackjack, you win."); System.exit(0); } System.out.print("Would you like to "hit" or "stay"? "); hitStay = keyboard.next(); System.out.println(); } } } // Dealers turn, only if Round 1 didn't end in bust/blackjack. keyboard.close(); System.out.println("Ok dealers turn."); System.out.println("His hidden card was a " + newCard[3] + "."); // reveal hidden from round one. cc++; // Pretty sure its not needed. while (dealerTotal < 16){ // Dealer will stay on 16+ and hit if below. System.out.println(); System.out.println("Dealer chooses to hit."); System.out.println("He draws a " + newCard[cc] + "."); cc++; dealerTotal = dealerTotal + newCard[cc]; System.out.println(); System.out.println("His total is " + dealerTotal); // bust check - no need for blackjack check due to final win sequence if (dealerTotal > 21){ System.out.println(); System.out.println("Dealer is bust, YOU WIN!"); System.exit(0); } // stay condition. if (dealerTotal < 21 && dealerTotal > 16){ System.out.println(); System.out.println("Dealer Stays."); } } // final win sequence. System.out.println(); System.out.println("Dealer total is " + dealerTotal); System.out.println("Your total is " + playerTotal); System.out.println(); if (dealerTotal > playerTotal){ System.out.println("Dealer wins."); } if (dealerTotal == playerTotal){ System.out.println("You both draw."); } if (dealerTotalRelated Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.