Question: This is my java code so far for a game of blackjack. Need help adding this to my code : Make your program determine uniqueness
This is my java code so far for a game of blackjack. Need help adding this to my code :
Make your program determine uniqueness in cards while drawing it. That means, that you cannot draw a card again.
package project;
import java.util.Random;
import java.util.Scanner;
public class blackjack {
static void displayInstuction() { //to display instruction
System.out.println("Instructions:"+
" First, the dealer and player each start with two cards drawn."+
" The goal of the game is to get as close to 21 card points as possible."+
" Having a card total of 21 points results in a win (unless the dealer also has a total of 21 points, in which case, it is a tie)."+
" If anyone goes over 21 points, they automatically lose."
+ " If both the dealer and player get over 21 card points, it is a tie. Otherwise, whoever is closest to 21 is deemed winner.");
}
static int drawCard() { //to draw Card
Random rand=new Random();
return rand.nextInt(13)+1;
}
static String determineWinner(int userScore,int dealerScore) //to determine who wins
{
if(userScore==21 && dealerScore==21||userScore>21 && dealerScore>21) //if its a tie
return "Tie";
else if(userScore<=21&&userScore>dealerScore||dealerScore>21) //if user wins
return "user";
else if(dealerScore<=21&&dealerScore>userScore||userScore>21) //if dealer wins
return "dealer";
return "";
}
public static void main(String[] args)
{
displayInstuction(); //displaying instruction
Scanner input=new Scanner(System.in);
String n="y"; //for user input
int userCard,dealerCard; //to store user card number and dealer card number
int userScore=0,dealerScore=0; //to store user and dealer score
//Drawing two cards for first time
userCard=drawCard();
dealerCard=drawCard();
userScore+=userCard;
dealerScore+=dealerCard;
System.out.println(" Card Drawn User got: "+userCard+
" Dealer got: "+dealerCard);
System.out.println(" User Score: "+userScore+" Dealer Score: "+dealerScore);
System.out.println(" Want to draw another Card: y or n");
n=input.next();
while(n.equalsIgnoreCase("y")) //user chance to draw card
{
userCard=drawCard();
userScore+=userCard;
System.out.println(" Card Drawn User got: "+userCard+
" Dealer got: "+dealerCard);
System.out.println(" User Score: "+userScore+" Dealer Score: "+dealerScore);
System.out.println(" Want to draw another Card: y or n");
n=input.next();
}
while(!(dealerScore>=17)) //dealer chance to draw card
{
dealerCard=drawCard();
dealerScore+=dealerCard;
System.out.println(" Card Drawn User got: "+userCard+
" Dealer got: "+dealerCard);
System.out.println(" User Score: "+userScore+" Dealer Score: "+dealerScore);
}
int m=0;
while(true) //for ace
{
System.out.println("Pick Ace Value 1 or 11 : ");
m=input.nextInt();
if(m==1||m==11)
break;
else
System.out.println("Please pick only 1 or 11...");
}
userScore+=m;
//print results
System.out.println(" User Score: "+userScore+" Dealer Score: "+dealerScore);
if(determineWinner(userScore,dealerScore).equalsIgnoreCase("user"))
System.out.println("User Wins");
else if(determineWinner(userScore,dealerScore).equalsIgnoreCase("dealer"))
System.out.println("Dealer Wins");
else
System.out.println("Its a tie");
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
