# JAVA Program to Implement BlackJack

This is the Java Program to Implement Black Jack

Blackjack is a very well known gambling card game played againsta dealer in a casino. In this card game, each player is trying to beat the dealer, by obtaining a sum of card values not more than 21 which is greater than the sum of dealer card values. Player is initially given 2 cards, but he could choose to hit(ask for 3rd card) or stand (no more cards). If he chooses to hit for 3rd card and total score crosses 21 he get busted (loses irrespective of the total score of dealer cards). Face cards (Jacks, Queensand Kings) are worth 10 points. Acesare worth 1 or 11, whichever is preferable. Other cards are represented by their number.

Here, you have to implement a conservative player strategy of playing blackjack. This conservative player does not want to get busted and hit only when its safe to do so. He follows following strategy:

hitif (score<=11) or (an ACE is held) standotherwise.

Write a program to implement the above strategy given the initial 2 cards of the player.

Program Problem Statement

•The Prototype of the Function is : •public inthitOrStand(charfirstcard, char secondcard) Where the function hitOrStandtakes 2 character input firstcard, secondcardand return interger output.

•Constraints 1) Color of the card does not affects the score of the card. 2) If the value of card is not valid then return -1. 3) Don’t consider the card with value 10. 4) Input for Face cards must be capital letters(‘A’,’Q’,’J’,’K’) else return -1

•Example 1 –Input : firstcard= ‘J’secondcard= ‘5’ –Output The function returns 0. –Explanation:Here the value of J (JACK) is 10. so the sum of card values is 15, so he chooses to stand. •Example 2 –Input : firstcard= 4 secondcard= 3 –Output The function returns 1. –Explanation:Here the sum of values of card 4 and 3 is 7. It is less than 11, so it makes sense for him to draw another card.

•Example 3 –Input : firstcard= ‘A’secondcard= ‘F’. –Output The function returns -1 –Explanation:Here F is an invalid card name

```
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package blackjack;
/**
*
* @author Jithin
*/
public class BlackJack {
public int hitorstand(char card1,char card2)
{
int val1=valueof(card1);
int val2=valueof(card2);
System.out.println(""+val1+" "+val2);
if(val1==-1||val2==-1){return -1;}
int total=val1+val2;
if (total <= 11||card1=='A'||card2=='A') return 1;
else return 0;
}
public int valueof(char card)
{
if(card>=49&&card<=57)
{
return card-'0';
}
else if(card=='A') return 11;
else if(card=='K'|| card=='Q'|| card=='J') return 10;
else return -1;
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
BlackJack bj=new BlackJack();
System.out.println(" "+bj.hitorstand('J','5'));
}
}
```

## Recent Comments