class Card {
private String point, type;
private int facevalue;
public String getPoint(){
return point;
}
public String getType(){
return type;
}
public int getFacevalue(){
return facevalue;
}
public Card(String inPoint, String inType, int inFacevalue){
point=inPoint; type=inType; facevalue=inFacevalue;
}
public String toString(){
return point +" of " + type;
}
};
class Deck {
private Card[] deckcards;
private int topdeck;
public void genCard(){
for(int i=0; i < 52; i++) {
Random gen = new Random();
Card temp;
int number = gen.nextInt(52);
temp = deckcards[i];
deckcards[i] = deckcards[number];
deckcards[number] = temp;
}
}
public Deck(){
deckcards = new Card[52];
String[] types = {"Spades", "Hearts", "Clubs", "Diamonds"};
String[] points = {"Ace", "King", "Queen", "Jack", "Ten", "Nine", "Eight", "Seven", "Six", "Five", "Four", "Three", "Two"};
int[] facevalues = {11, 10, 10, 10, 10, 9, 8, 7, 6, 5, 4, 3, 2};
for(int i = 0; i < 52; i++){
deckcards[i] = new Card(points[i%13], types[i%4], facevalues[i%13]);
// System.out.println(points[i%13]+" of "+types[i%4]+", Facevalue: "+facevalues[i%13]);
}
topdeck = 0;
}
public Card getCard(){
return deckcards[topdeck++];
}
};
class NewPlayer {
private String name;
private ArrayList<Card> hand;
private int aces=0, total=0, cards=0;
public String getName() {
return name;
}
public int getTotal(){
return total;
}
public void setName(String name){
this.name=name;
}
public void hit(Card a){
hand.add(a);
cards++;
if(a.getPoint().equals("Ace"))
aces++;
if(aces >= 2)
total = total + 1;
else
total = total + a.getFacevalue();}
public void getHand(){
for(int i = 0; i < hand.size(); i++){
Card b = hand.get(i);
System.out.println(b);
}
}
public NewPlayer(){
hand = new ArrayList<Card>();
}
};
public class Blackjack {
public static void main(String[] args){
{
Scanner inputans = new Scanner(System.in);
String answer;
Deck myDeck = new Deck();
myDeck.genCard();
NewPlayer me = new NewPlayer();
System.out.print("Please enter your name: ");
me.setName(inputans.nextLine());
NewPlayer dealer = new NewPlayer();
dealer.setName("Dealer");
System.out.println("Hi...... Welcome " + me.getName() + "\n");
Card a = myDeck.getCard();
me.hit(a);
a = myDeck.getCard();
dealer.hit(a);
System.out.println("The Dealer is showing :");
System.out.println(a + "\n");
a = myDeck.getCard();
me.hit(a);
System.out.println("Your hand is: ");
me.getHand();
System.out.println("Your total is: " + me.getTotal() + "\n");
a = myDeck.getCard();
dealer.hit(a);
if(me.getTotal()==(21)){
System.out.println("Dealer's hand is: ");
dealer.getHand();
System.out.println("Dealer's total is: " + dealer.getTotal());
if(me.getTotal() > dealer.getTotal())
System.out.println("Congratulations " + me.getName() + "! You win!!!");
else
System.out.println("It's a draw! Play again!");}
while(me.getTotal() < 21){
System.out.print("Do you want another card? Please enter YES or NO: ");
answer = inputans.nextLine();
if(answer.equalsIgnoreCase("No")){
System.out.println("Dealer's hand is: ");
dealer.getHand();
System.out.println("Dealer's total is: " + dealer.getTotal());
if(me.getTotal() > dealer.getTotal())
System.out.println("Congratulations " + me.getName()+"! You win!!!");
else if(me.getTotal() < dealer.getTotal())
System.out.println("Sorry " + me.getName() + "... You lose. Try again!");
else
System.out.println("It's a draw! Play again!");}
else if(answer.equalsIgnoreCase("Yes")){
a = myDeck.getCard();
me.hit(a);
System.out.println("Your hand is: ");
me.getHand();
System.out.println("Your total is: " + me.getTotal());
System.out.println();
{
if(me.getTotal() > 21)
System.out.println("Sorry " + me.getName()+"... You bust. Try again!");
else if(me.getTotal()==(21))
{
if(me.getTotal()>dealer.getTotal())
{
System.out.println("Dealer's hand is: ");
dealer.getHand();
System.out.println("Dealer's total is: "+dealer.getTotal());
System.out.println("Congratulations "+me.getName()+"! You win!!");}
else
System.out.println("It's a draw! Play again!");}}}}
System.out.println();
}
}
}