Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Implement a blackjack cards game with betting between 1 user and the computer (d

ID: 3659159 • Letter: I

Question

Implement a blackjack cards game with betting between 1 user and the computer (dealer). We will use a deck of playing cards with the following cards 2<3<...<10< diamonds < clubs < spades. At the beginning of the game, the user starts with $20 and can bet at most 2 dollars per game (i.e., the user can only bet $1 or $2). At the beginning of each round, the user places his bet in his "betting box". The user box is dealt an initial hand of two cards visible to the user. The computer/dealer's hand receives its first card face up, and its second card face down immediately (the hole card), which the computer does not reveal unless it makes the its hand a blackjack. The user's object is to win money by creating card totals which will turn out to be higher than the dealer's hand, but without exceeding 21 ("busting"/"breaking"). The user must choose whether to "hit" (take a card), "stand" (end their turn), "double" (double wager, take a single card and finish), "split" (if the two cards have the same value (i.e., rank, e.g., the player can split if he has 2 queens), separate them to make two hands) or "surrender" (give up a half-bet and retire from the game). Once the player splits, he is only allowed to play hit and stand. The bets are made before the choices of the player, so it is considered the same for both hands in the case of a split. If one hand looses, then the player continues with the other hand. If both hands win, then the player wins double the bet. Number-cards count as their natural value; the jack, queen, and king (also known as "face cards" or "pictures") count as 10; aces are valued as either 1 or 11 (i.e., the program should sum up with both values (1 and 11) for Ace and take the value that makes the sum less or equal than 21, but closer to 21.) If the hand value exceeds 21 points, it busts, and all bets on it are immediately forfeit. Details about player decisions are available here: http://en.wikipedia.org/wiki/Blackjack#Player_decisions Check that the same cards are not distributed as duplicates in the same game round! After the user finished playing, the dealer's hand is resolved by drawing cards until the hand busts or achieves a value of 17 or higher. The dealer never doubles, splits nor surrenders. If the dealer busts, then the user wins. If the dealer does not bust, the user wins the bet if his hand is higher than the computer's, and loses if it is lower. In the case of a tied score, known as "push" or "standoff", bets are normally returned without adjustment. Wins are paid out at 1:1, or equal to the wager, except for winning blackjacks, which are traditionally paid at 3:2 (meaning the player receives three dollars for every two bet), or one and a half times the wager. Aces are valued 11 by the computer. Your game should play 10 hands and print the purse of the user. import java.awt.Color; import java.awt.FlowLayout; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.Scanner; import java.io.IOException; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.border.Border; import javax.swing.border.LineBorder; import java.util.Scanner; public class Blackjack_GUI extends JFrame { int rounds = 0; int purse = 20; JPanel p0 = new JPanel(); JPanel p01 = new JPanel(); JPanel p02 = new JPanel(); JPanel p1 = new JPanel(); JPanel p12 = new JPanel(); JPanel p2 = new JPanel(); JPanel p21 = new JPanel(); JPanel p211 = new JPanel(); JPanel p22 = new JPanel(); JPanel p222 = new JPanel(); JPanel p3 = new JPanel(); JPanel p32 = new JPanel(); JPanel p4 = new JPanel(); JPanel p5 = new JPanel(); JPanel p6 = new JPanel(); JLabel l1 = new JLabel(); JLabel l21 = new JLabel(); JLabel l22 = new JLabel(); JLabel[] icons1 = new JLabel[10]; JLabel[] icons2 = new JLabel[10]; int bet = 0; JLabel l41 = new JLabel(); JButton b41 = new JButton("$1"); JButton b42 = new JButton("$2"); JLabel l42 = new JLabel(); JLabel l5 = new JLabel(); JButton b51 = new JButton("Stay"); JButton b52 = new JButton("Hit"); JLabel l6 = new JLabel(); JButton b6 = new JButton("Next"); // cards int[][] previous_cards = new int[50][2]; int nop = 0; int[][] hand1 = new int[10][2]; // human hand int no1 = 0; int[][] hand2 = new int[10][2]; // computer int no2 = 0; int sum1 = 0; int sum2 = 0; // Constructor Blackjack_GUI(){ this.setSize(1000, 800); this.setLayout(new GridLayout(2,1)); this.add(p2); this.add(p3); p3.setLayout(new GridLayout(5,1)); p3.add(p0); p3.add(p1); p3.add(p4); p3.add(p5); p3.add(p6); //j11.setIcon(card_to_ImageIcon(sorted1[0])); rounds = 1; p0.setLayout(new GridLayout(1,2)); p0.add(p01); p0.add(p02); p01.add(l21); p02.add(l22); p01.setBorder(new LineBorder(new Color(0,0,0),2)); p02.setBorder(new LineBorder(new Color(0,0,0),2)); p1.add(l1); p2.setLayout(new GridLayout(1,2)); p2.add(p211); p2.add(p222); p211.setBorder(new LineBorder(new Color(0,0,0),2)); p222.setBorder(new LineBorder(new Color(0,0,0),2)); p211.setLayout(new GridLayout(1,5)); p222.setLayout(new GridLayout(1,5)); p4.add(l41); p4.add(b41); p4.add(b42); p4.add(l42); l41.setText("Bet: "); p5.add(l5); l5.setText("Action: "); p5.add(b51); p5.add(b52); p6.add(l6); p6.add(b6); b41.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) { refreshDisplay("b41"); } } ); b42.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) { refreshDisplay("b42"); } } ); b51.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) { refreshDisplay("b51"); } } ); b52.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) { refreshDisplay("b52"); } } ); b6.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) { refreshDisplay("b6"); } } ); // ACTIONS l1.setText("Round "+rounds+" "+"Action: select bet"); // generate one card hand1[no1] = generate_card(previous_cards,nop); previous_cards[nop][0] = hand1[no1][0]; previous_cards[nop][1] = hand1[no1][1]; nop++; no1++; // generate one card hand1[no1] = generate_card(previous_cards,nop); previous_cards[nop][0] = hand1[no1][0]; previous_cards[nop][1] = hand1[no1][1]; nop++; no1++; // generate computer hard hand2[no2] = generate_card(previous_cards,nop); previous_cards[nop][0] = hand2[no2][0]; previous_cards[nop][1] = hand2[no2][1]; nop++; no2++; for(int i=0; i New purse: $ "); b51.disable(); b52.disable(); b6.disable(); setVisible(true); repaint(); } public static void main(String[] args) throws IOException { Blackjack_GUI pg = new Blackjack_GUI(); } public void refreshDisplay(String option){ System.out.println("Option: "+option); if( option.equals("b41") || option.equals("b42") ){ l1.setText("Round "+rounds+" "+"Action: select stand, hit"); if( option.equals("b41") ){ bet = 1; }else bet = 2; l42.setText("Bet: $"+bet); b41.disable(); b42.disable(); b51.enable(); b52.enable(); b6.disable(); } else if(option.equals("b52")){ // generate one card hand1[no1] = generate_card(previous_cards,nop); previous_cards[nop][0] = hand1[no1][0]; previous_cards[nop][1] = hand1[no1][1]; nop++; no1++; p211.removeAll(); for(int i=0; i New purse: $ "); } else if(option.equals("b51")){ } else if(option.equals("b6")){ } else System.out.println("Invalid option: "+option); repaint(); } public static ImageIcon card_to_ImageIcon(int[] c){ String fileString = "images/Playing_card_"; if(c[1]==1) fileString += "heart"; else if(c[1]==2) fileString += "diamond"; else if(c[1]==2) fileString += "club"; else fileString += "spade"; fileString += "_"; if(2<=c[0] && c[0]<=10) fileString += c[0]; else if(c[0]==11) fileString += "J"; else if(c[0]==12) fileString += "Q"; else if(c[0]==13) fileString += "K"; else fileString += "A"; fileString += ".jpg"; return new ImageIcon(fileString); } public static int sum_hand(int[][] hand1,int no1){ int sum = 0; for(int i=0; i<=10) sum += hand1[i][0]; else sum += 10; } return sum; } public static int[] generate_card(int[][] previous_cards,int nop){ boolean duplicate = false; int[] card = new int[2]; do{ duplicate = false; card[0] = (int) (Math.random()*13 + 2); card[1] = (int) (Math.random()*4 + 1); // compare all the previous hands with the current hand for(int i=0; i<=c[0] && c[0]<=10) card += c[0]; else if(c[0]==11) card += "Jack"; else if(c[0]==12) card += "Queen"; else if(c[0]==13) card += "king"; else if(c[0]==14) card += "Ace"; else card += "card_to_String error: card number="+c[0]; card += " of "; if(c[1]==1) card += "hearts"; else if(c[1]==2) card += "diamonds"; else if(c[1]==2) card += "clubs"; else card += "spades"; return card; } }

Explanation / Answer

A complete Blackjack game bit long but a good one

//**************************************
//INCLUDE files for :C++ Blackjack
//**************************************
#include<iostream.h>
#include<time.h>
#inlcude<comm.h>
#include<process.h>
#include<ctype.h>
#include<dos.h>
#include<conio.h>

/************************** SEC 1: GLOBAL******************************************/
void init_mm( );
void gencard();
void play();
void about();
void help();
int noanim = 0;
int number_range( int from, int to );
int number_mm( void );
static int rgiState[2+55]; // leave this alone
int number,suit;
char faceace,soot;
/*****************************SEC 2. MAIN***********************************************/
void main()


{
char a;
clrscr();
textcolor(WHITE);
clrscr();
if(noanim == 0)


{
for(int i=0;i<3;i++)
{ sound(400);cout<<" *";delay(100);nosound();delay(50);clrscr();cout<<" ";}
for(i=0;i<3;i++)
{ sound(500);cout<<" *";delay(100);nosound();delay(50);clrscr();cout<<" ";}
for(i=0;i<3;i++)
{ sound(400);cout<<"*";delay(100);nosound();delay(50);clrscr();cout<<" ";}
for(i=0;i<3;i++)
{ sound(1000);cout<<"*";delay(100);nosound();delay(50);clrscr();cout<<" ";}
}
for(;;)


{
clrscr();
cout<<" C++ BLACKJACK";
cout<<" MENU";
cout<<" 1. NEW GAME";
cout<<" 2. ALL ABOUT BLACKJACK";
cout<<" 3. ABOUT THE PROGRAM";
cout<<" 4. EXIT";
cout<<" PLEASE MAKE YOUR CHOICE";
a=getch();
if(a == '1')
{ sound(1000);delay(100);nosound();play();}
else if(a == '2')
{ sound(1000);delay(100);nosound();help();}
else if(a == '3')
{ sound(1000);delay(100);nosound();about();}
else if(a == '4')
{ sound(1000);delay(100);nosound();exit(0);}
else


{
sound(400);delay(100);nosound();
cout<<" INCORRECT CHOICE";
delay(500);
}
}
}
/***********************************SEC 4: GAMEPLAY**************************************************/
void play()


{
long bet;
int sum1,sum2,b1,b2;
int value;
long money=10000;
int doubledown;
int iter;
char f;
rb:
b1=0;
b2=0;
sum1=0;
sum2=0;
iter = 0;
doubledown=0;
clrscr();
cout<<" AFTER YOUR FIRST TWO CARDS HAVE BEEN DEALT, PRESS: H TO HIT S TO STAND D TO DOUBLE DOWN";
cout<<" You have a total of $ "< cout<<" Enter your bet(not exceeding $ 5000) $ ";
cin>>bet;
if(bet>5000)
{cout<<" NOT exceeding 5000!"; delay(1000); goto rb;}
else if(bet<=0)
{cout<<" INVALID BET!"; delay(1000); goto rb;}
else if(bet>money)
{cout<<" You don't have enough money!"; delay(1000); goto rb;}
char a;
int acecheck = 0;
int prem=0;
cout<<" -->Dealer's First card: ";
gencard();
switch(number)


{
case 1:
{value = 11; acecheck = 1;}break;
case 11:
{value = 10; acecheck = 0;}break;
case 12:
{value = 10; acecheck = 0;}break;
case 13:
{value = 10; acecheck = 0;}break;
}
if(number == 1 || number >10)
sum2+=value;
else
sum2+=number;
if( (acecheck == 1) && (sum2>21) )


{
sum2-=value;
value = 1;
sum2+=value;
}
if(number==1 || number>10)
cout<<" "< else
cout<<" "< delay(1000);
cout<<" -->Your first two cards: ";
for(;;)


{
h:
gencard();
switch(number)


{
case 1:
{value = 11; acecheck = 1;}break;
case 11:
{value = 10; acecheck = 0;}break;
case 12:
{value = 10; acecheck = 0;}break;
case 13:
{value = 10; acecheck = 0;}break;
}
if(number == 1 || number >10)
sum1+=value;
else
sum1+=number;
if( (acecheck == 1) && (sum1>21) )


{
sum1-=value;
value = 1;
sum1+=value;
}
iter++;
if(number==1 || number>10)
cout<<" "< else
cout<<" "< if(iter == 2 && sum1 == 21)


{
cout<<" BLACKJACK!";
sound(1000);delay(100);nosound();
sound(800);delay(200);nosound();
sound(600);delay(300);nosound();
sound(400);delay(400);nosound();
iter = -1;
goto bj;
}
if(sum1>21)


{
cout<<" BUSTED!";
sound(400);delay(500);nosound();
delay(50);sound(400);delay(800);nosound();
b1=1;
break;
}
if(prem == 0)
{prem++; delay(1000); goto h;}
fg:
if(doubledown == 1)
break;
a = getch();
if (a == 's' || a == 'S')
break;
else if(a == 'H' || a == 'h')
goto h;
else if(a == 'D' || a == 'd')


{
if( bet*2 > money)


{
cout<<" YOU CANNOT DOUBLE DOWN! ";
for(int j=0;j<24;j++)
cout<<"";
sound(400);
delay(500);
nosound();
goto fg;
}
else


{
bet*=2;
doubledown = 1;
goto h;
}
}
else
{sound(800);delay(250);nosound(); goto fg;}
}
acecheck = 0;
cout<<" Dealer's turn...";
cout<<" ";
for(;;)


{
gencard();
switch(number)


{
case 1:
{value = 11; acecheck = 1;}break;
case 11:
{value = 10; acecheck = 0;}break;
case 12:
{value = 10; acecheck = 0;}break;
case 13:
{value = 10; acecheck = 0;}break;
}
if(number == 1 || number >10)
sum2+=value;
else
sum2+=number;
if( (acecheck == 1) && (sum2>21) )


{
sum2-=value;
value = 1;
sum2+=value;
}
delay(1500);
if(number==1 || number>10)
cout<<" "< else
cout<<" "< if(sum2>21)


{
cout<<" DEALER BUSTED!";
sound(400);delay(500);nosound();
delay(50);sound(400);delay(800);nosound();
b2=1;
break;
}
if(sum2>=17)
break;
}
bj:
if(iter == -1)


{
for(int i=0;i<10;i++)


{
sound(1000+i);delay(100);nosound();
}
money+=bet;
}
if(b1 == 1 && b2 == 0)


{
cout<<" Dealer wins hand!";
for(int i=10;i>0;i--)


{
sound(1000+i);delay(100);nosound();
}
money-=bet;
}
else if(b1 == 1 && b2 == 1)


{
cout<<" Dealer wins hand!";
for(int i=10;i>10;i--)


{
sound(1000+i);delay(100);nosound();
}
money-=bet;
}
else if(b1==0 && b2 == 1)


{
cout<<" You win the hand!";
for(int i=0;i<10;i++)


{
sound(1000+i);delay(100);nosound();
}
money+=bet;
}
else if(b1==0 && b2 == 0)


{
if(sum1>sum2)


{
cout<<" You win the hand!";
for(int i=0;i<10;i++)


{
sound(1000+i);delay(100);nosound();
}
money+=bet;
}
else if(sum1

{
cout<<" Dealer wins hand!";
for(int i=10;i>0;i--)


{
sound(1000+i);delay(100);nosound();
}
money-=bet;
}
else


{
cout<<" It's a push!";
for(int i=0;i<10;i++)


{
sound(1000+i);delay(100);nosound();
}
for(i=10;i>0;i--)


{
sound(1000+i);delay(100);nosound();
}
}
}
cout<<" Your money: $ "< cout<<" Press Enter to deal again or escape to exit... ";
ra:
f=getch();
if(f == ENTER && money!=0)
goto rb;
else if(f == ESC)
{ noanim = 1; main();}
else if(f == ENTER && money == 0)
{cout<<" NO MONEY"; noanim = 1; sound(800);delay(1000);nosound(); main();}
else


{
sound(800);
delay(500);
nosound();
goto ra;
}
}
/*****************************SEC 5: CARD GENERATOR**********************************/
void gencard()


{
init_mm(); //seed the number generator
number = number_range( 1, 13 );
suit = number_range(1,4);
switch(number)


{
case 1:
{faceace = 'A';}break;
case 11:
{faceace = 'J';}break;
case 12:
{faceace = 'Q';}break;
case 13:
{faceace = 'K';}break;
}
switch(suit)


{
case 1:
{soot = char(3);}break;
case 2:
{soot = char(4);}break;
case 3:
{soot = char(5);}break;
case 4:
{soot = char(6);}break;
}
}
/****************************SEC 6: RANDOM NUMBER GENERATOR*******************************************/
int number_mm( void )


{
int *piState;
int iState1;
int iState2;
int iRand;
piState = &rgiState[2];
iState1 = piState[-2];
iState2 = piState[-1];
iRand = ( piState[iState1] + piState[iState2] )
& ( ( 1 << 30 ) - 1 );
piState[iState1] = iRand;
if ( ++iState1 == 55 )
iState1 = 0;
if ( ++iState2 == 55 )
iState2 = 0;
piState[-2] = iState1;
piState[-1] = iState2;
return iRand >> 6;
}
/*
* Generate a random number.
*/
int number_range( int from, int to )


{
int power;
int number;
if ( ( to = to - from + 1 ) <= 1 )
return from;
for ( power = 2; power < to; power <<= 1 )
;
while ( ( number = number_mm( ) & ( power - 1 ) ) >= to )
;
return from + number;
}
/*
* this is the Mitchell-Moore algorithm from Knuth Volume II.
*/
void init_mm( )


{
int *piState;
int iState;
piState = &rgiState[2];
piState[-2] = 55 - 55;
piState[-1] = 55 - 24;
piState[0] = ( (int) time( NULL ) ) & ( ( 1 << 30 ) - 1 );
piState[1] = 1;
for ( iState = 2; iState < 55; iState++ )


{
piState[iState] = ( piState[iState-1] + piState[iState-2] )
& ( ( 1 << 30 ) - 1 );
}
return;
}
/***************************************SEC 7: HELP AND ABOUT**********************************************************/
void about()


{
clrscr();
cout<<" ABOUT THE PROGRAM";
cout<<" C++ BLACKJACK MADE BY PARITOSH MATHUR IN 2005 PLEASE NOTE THAT THIS GAME IS FOR ENTERTAINMENT PURPOSES ONLY. NO MONEY OF ANY KIND IS AWARDED. DISCLAIMER: THE AUTHOR TAKES AND SHALL TAKE NO RESPONSIBILITY WHATSOEVER TOWARDSANY HARM OR DAMAGE OF ANY KIND THIS PROGRAM MAY CAUSE";
cout<<" PRESS ANY KEY TO CONTINUE...";
getch();
}
void help()


{
clrscr();
cout<<" ALL ABOUT BLACKJACK";
cout<<" BLACKJACK IS A POPULAR CASINO GAME PLAYED BETWEEN THE PLAYER AND THE CASINO DEALER. THE OBJECT OF THE GAME IS TO BEAT THE DEALER'S HAND WITHOUT GOING OVER 21.";
cout<<"THE PLAYER STARTS BY PLACING A BET, AFTER WHICH THE DEALER DEALS HIMSELF A CARD AND THEN DEALS THE PLAYER TWO CARDS. THE SUITS MEAN NOTHING. THE PLAYER SIMPLY ADDS UP THE CARD VALUES (WHERE NUMBERED CARDS HAVE THEIR RESPECTIVE VALUES, FACE CARDS ARE WORTH 10 AND ACES CAN BE WORTH 11 (SOFT ACE) OR 1 (HARD ACE) ).";
cout<<"THE PLAYER THEN DECIDES WHETHER TO ASK THE DEALER FOR ANOTHER CARD (KNOWN AS HITTING), OR ALLOW THE DEALER TO DRAW HIS CARDS (KNOWN AS STAYING OR STANDING). THE DEALER MUST DRAW CARDS TILL HE GETS A TOTAL OF 17 OR HIGHER. IF THE DEALER GOES ABOVE 21 IN THE PROCESS, HE BUSTS. LIKEWISE, THE PLAYER CAN BUSTIF HE OR SHE GOES ABOVE 21 WHILE HITTING.";
cout<<"IF NOBODY BUSTS, THEN THE PERSON WITH THE GREATEST TOTAL WINS THE BET. IF THE PLAYER BUSTS, HE LOSES, IRRESPECTIVE OF WHETHER THE DEALER BUSTS OR NOT.";
cout<<"IF THE PLAYER DOES NOT BUST AND THE DEALER BUSTS, THE PLAYER WINS THE BET.";
cout<<"IN CASE OF BOTH PLAYERS GETTING THE SAME TOTAL, THE SITUATION IS CALLED A PUSH. THE PLAYER GETS BACK THE AMOUNT HE STAKED AND HIS TOTAL EARNINGS REMAIN THE SAME.";
cout<<" ACES ARE NORMALLY VALUED AT 11. HOWEVER, IF A HAND HAS AN ACE OR AN ACE IS DRAWN, SUCH THAT THE TOTAL GOES ABOVE 21, THE ACEIS AUTOMATICALLY REVALUED AT 1, SO AS TO PREVENT A BUST. HOWEVER, A BUST IS INEVITABLE IN CASES LIKE HITTING AND DRAWING AN ACE ON A 21.";
cout<<" APART FROM HITTING AND STANDING, ANOTHER OPTION CALLED DOUBLING DOWN IS AVAILABLE. BY DOUBLING DOWN, THE PLAYER TELLS THE DEALER TO DEAL ONLY ONE MORE CARD TO HIM AND TO DOUBLE THE AMOUNT OF THE BET. IF THIS CARD MAKES THE PLAYER'S TOTAL GREATER THANTHE DEALER'S, OR IF THE DEALER BUSTS, THE THE PLAYER WINS DOUBLE THE AMOUNT HE PREVIOUSLY STAKED. IF, HOWEVER, THE PLAYER GETS A LESSER TOTAL OR BUSTS, HE LOSES DOUBLE THE AMOUNT HE PREVIOUSLY STAKED.";
cout<<" IF THE FIRST TWO CARDS DEALT TO THE PLAYER ARE AN ACE AND A TEN VALUE CARD, IT'S CALLED A BLACKJACK AND MEANS INSTANT VICTORY. THE PLAYER WINS DOUBLE THE AMOUNT HE STAKED.";
cout<<" TO PLAY C++ BLACKJACK, CHOOSE OPTION 1 IN THE MAIN MENU, PLACE A BET UNDER $ 5000, AND WAIT TILL ALL CARDS HAVE BEEN DEALT. THEN CHOOSE H TO HIT, S TO STAND OR D TO DOUBLE DOWN. YOU CANNOT BET AN AMOUNT GREATER THAN YOUR TOTAL EARNINGS. YOU START WITH A TOTAL OF $ 10,000.";
cout<<" PRESS ANY KEY TO CONTINUE...";
getch();
}
/***********************************END OF PROGRAM*****************************************************************/

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote