shalini_goel14
shalini_goel14
Branch Unspecified
02 Feb 2009

Do you know how to code for a Black Jack

Hi All,

On behalf of our fellow CEan rohit330 , I am putting a question before all CEans over here:

"BlackJack is a very well known gambling card game played against a dealer in a casino. In his card game, each player is trying to beat the dealer, by obtaining a sum of card values notmore 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 (losess irresopective of the total score of dealer cards). Face cards(Jack, Queens and Kings) are worth 10 points. Aces are 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:

hit if(score<=11) or (an ACE is held)
stand otherwise

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

[ PS: Rohit, Please check if I have missed anything here. ]
rohit330

rohit330

Branch Unspecified
03 Feb 2009
yeah..everything fine
shalini_goel14

shalini_goel14

Branch Unspecified
04 Feb 2009
Hi rohit,

Please look at following Java implementation for this if it is fine for you.

/*
 * BlackJack.java
 *
 * Created on February 4, 2009, 11:32 AM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */
package myjava;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/**
 *
 * @author shalinig
 */
public class BlackJack {
    
    /** Creates a new instance of BlackJack */
    public BlackJack() {
    }
    
    public static void main(String args[]){
        
        BlackJack bObj= new BlackJack();
        
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        String firstCardVal="",secondCardVal="";
        Integer returnedVal=null;
        try {
            System.out.println("Enter your first card value.");
            firstCardVal = br.readLine();
            System.out.println("First Card Value is ::"+firstCardVal);
            
            System.out.println("Enter your second card value.");
            secondCardVal = br.readLine();
            System.out.println("Second Card Value is ::"+secondCardVal);
        } catch (IOException ex) {
            System.out.println("IO Exception occured");
        }
        
        if(firstCardVal !=null && secondCardVal !=null){
            returnedVal= bObj.retrunVal(firstCardVal.toUpperCase(),secondCardVal.toUpperCase());
        }
        
        if(returnedVal==0){
            System.out.println("He chooses to stand.");
        } else if(returnedVal==1){
            System.out.println("He asks for a hit.");
        } else
            System.out.println("Invalid card value");
    }
    
    public int retrunVal(String firstCardVal, String secondCardVal){
        Integer sum=0;
        System.out.println("[firstCardVal = "+firstCardVal+", secondCardVal = "+secondCardVal+"]");
        if(firstCardVal.equals("J")){
            firstCardVal="10";
        } else if(firstCardVal.equals("A")){
            firstCardVal="11";
        } else if(firstCardVal.equals("Q")){
            firstCardVal="12";
        } else if(firstCardVal.equals("K")){
            firstCardVal="13";
        } else if(secondCardVal.equals("J")){
            secondCardVal="10";
        } else if(secondCardVal.equals("A")){
            secondCardVal="11";
        } else if(secondCardVal.equals("Q")){
            secondCardVal="12";
        } else if(secondCardVal.equals("K")){
            secondCardVal="13";
        }
        try{
            sum = new Integer(Integer.parseInt(firstCardVal) + Integer.parseInt(secondCardVal));
            System.out.println("Sum is:"+sum);
        } catch (NumberFormatException ex) {
            System.out.println("Return val is -1");
            return -1;
        }
        if(sum<=11){
            System.out.println("Return val is 1");
            return 1;
        } else{
            System.out.println("Return val is 0");
            return 0;
        }
    } 
     
 }
Note: It is well compiled and executed. Feel free to ask any questions/doubts in it. 😀
shalini_goel14

shalini_goel14

Branch Unspecified
04 Feb 2009
Hi all C/C++ users,

Below find the prototype for this :

int testHit(char firstCard, char secondCard)
Where the function testHit takes 2 character input firstCard, secondCard and returns an Integer output.

Constraints:
1. Color of the card does not affect 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','J','Q','K') else return -1

Input specifications: In input you will be given 2 single characters representing the two cards.

Output specifications: The output will be 1 or 0. 1 means the player can hit and 0 means stand.

[PS: This is applicable only for C/C++ programs. Please try once.]
rohit330

rohit330

Branch Unspecified
04 Feb 2009
oops.. i dont know java!
shalini_goel14

shalini_goel14

Branch Unspecified
04 Feb 2009
rohit330
oops.. i dont know java!
You can ask me any question in that program rohit. Well, wait for sometime may be C/C++ users will post their program here.😀. I have given them logic idea also.
rohit330

rohit330

Branch Unspecified
10 Feb 2009
[B][U]Solution[/U][/B]

[B]#include<stdio.h>
#include<string.h>
#include<ctype.h>
int testHit(char,char);
int main()
{
clrscr();
//TestCase 1
	{
		char firstcard,secondcard;
		int result;
		firstcard= 'J';secondcard= 'A';
		result=testHit(firstcard,secondcard);
		printf("%d  ",result);

	}


	//TestCase 2
	{
		char firstcard,secondcard;
		int result;
		firstcard= '4';secondcard= '3';
		result=testHit(firstcard,secondcard);
		printf("%d  ",result);


	}
	//TestCase 3
	{
		char firstcard,secondcard;
		int result;
		firstcard= 'A';secondcard= 'F';
		result=testHit(firstcard,secondcard);
		printf("%d  ",result);
	}

return(0);
}
int testHit(char firstcard,char secondcard)
{
int temp,w;
char d,p;
char l,i;
if(((secondcard=='A')||(secondcard=='J')||(secondcard=='K')||(secondcard=='Q')||(secondcard=='2')||(secondcard=='3')||(secondcard=='4')||(secondcard=='5')||(secondcard=='6')||(secondcard=='7')||(secondcard=='8')||(secondcard=='9'))&&((firstcard=='A')||(firstcard=='J')||(firstcard=='K')||(firstcard=='2')||(firstcard=='3')||(firstcard=='4')||(firstcard=='5')||(firstcard=='6')||(firstcard=='7')||(firstcard=='8')||(firstcard=='9')||(firstcard=='Q')))
{
switch(firstcard)
{
case 'A':
w=firstcard-17;
firstcard=w+1;
break;
case 'J':
w=firstcard-17;
firstcard=w+1;
break;
case 'K':
w=firstcard-18;
firstcard=w+1;
break;
case 'Q':
w=firstcard-24;
firstcard=w+1;
break;
}
switch(secondcard)
{
case 'A':
w=secondcard-17;
secondcard=w+1;
break;
case 'J':
w=secondcard-17;
secondcard=w+1;
break;
case 'K':
w=secondcard-18;
secondcard=w+1;
break;
case 'Q':
w=secondcard-24;
secondcard=w+1;
break;
}
l=firstcard+secondcard;
//printf("%d",l);
p=toascii(l);
//printf("%d",p-96);
temp=p-97;
if ((temp<=11)||(firstcard=='A')||(secondcard=='A'))
return(1);
else
return(0);
}
else
return(-1);
}[/B]

rohit330

rohit330

Branch Unspecified
15 Feb 2009
Now the thread can be closed....

Share this content on your social channels -

Only logged in users can reply.