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

Using C programming language: A simplified version of Blackjack -- a well-known

ID: 3730158 • Letter: U

Question

Using C programming language:

A simplified version of Blackjack -- a well-known game.

The basic rules of the game:

A deck of poker cards are used. For simplicity, we have unlimited number of cards together, so we can generate a random card at a time without considering which cards have already dealt. The game here is to play as a player against the computer (the dealer). The aim of the game is to accumulate a higher points total than the dealer’s, but without going over 21. The cards 2 to 10 have their face values as points. J, Q, and K are 10 points each, and the Ace is either 1 point or 11 points (player's choice). To simplify the matter, we consider that the Ace is 11 points only, unless you like to implement the option anyway.

b) Each hand will result in one of the following events for the player

Lose - the player's bet is taken by the dealer.

Win - the player wins as much as s/he bet. If s/he bet $= 10, s/he wins $10 from the dealer.

Blackjack - the player wins 1.5 times the bet. With a bet of $10, s/he wins $15 from the dealer.

Push - the hand is a draw. The player keeps his/her bet, neither winning nor losing money.


The player can keep his hand as it is (stand) or take more cards from the deck (hit), one at a time, until either the player judges that the hand is strong enough to go up against the dealer’s hand and stands, or until it goes over 21, in which case the player immediately loses (busts).


e) The dealer’s turn


If the dealer goes bust, the player wins. Otherwise the player wins if s/he has a higher score, lose if s/he has a lower score, or push if s/he has the same score as the dealer.

specific design of this project

bankroll (in order to keep track of how much money or how many points the player has), the bet at a game, and other information. Let’s use an integer array gamerecord[] to store how many times the player won, lost, hit a blackjack, and got busted.



A separate dealing() function will be used to generate a card number. You will use a random number generator. The random number generator needs to be seeded with the current time at the beginning of the main program. The possible random values generated are 1 to 13, representing the cards’ face values. This function will return the number generated. The return value 1 represent the Ace’s face value, and the return value 11, 12, and 13 are J, Q, and K’s face value, respectively. Any other number is the card’s face value. A large random number n can be converted to a value between 1 to 13 by:  (1 + n%13).

c) “Playing” the game: playing() function


The player can keep his hand as it is (stand) or take more cards from the deck (hit), one at a time, until either the player judges that the hand is strong enough to go up against the dealer's hand and stands, or until it goes over= 21, in which case the player immediately loses the bet.

d) "Ending" and "Beginning" of the game


This part is implemented after you have done your programming as described above already.

You need a separate function ending() to do the following: you should report the current value of the bank roll, how many times the player won, lost, hit a blackjack, and went busted. You need to save the above information into a text file as well.


You need a separate function beginning() to do the following at the beginning of your program in main(): the function will open the text file you used to save the game information for reading if it exists, so that your game can continue from previous played results. If the file does not exist or the bank roll has a balance below the minimum bet, you start the game from scratch as usual, and report “new game” or “continual game”.

Explanation / Answer

#include<stdio.h>
#include<time.h>
#include<stdlib.h>

#define BLACKJECK 21

FILE *game_data;

int money=100;
int bet_in_data=0;
int points=0,com_points_in_data=0;
int game_record[4];

char *deck[]={"A","2","3","4","5","6","7","8","9","10","J","Q","K"};
//..................................................continue_game().....................................................
int continue_game()
{
game_data=fopen("./game_data.txt","r");
if(game_data==NULL)
{
  fclose(game_data);
  return -1;
}
if(fscanf(game_data,"%d %d ",&money,&bet_in_data)==EOF)
{
  fclose(game_data);
  return -1;
}
else
{
  if(money<=0)
  {
   printf("File Not Open-2 ");

   fclose(game_data);
   return 0;
  }
}
if(fscanf(game_data,"%d %d ",&points,&com_points_in_data)!=EOF)
{
  if(fscanf(game_data,"%d %d %d %d",&game_record[0],&game_record[1],&game_record[2],&game_record[3])==EOF)
  {
      printf("File Not Open-3 ");

   game_record[0]=game_record[1]=game_record[2]=game_record[3]=0;
  }
  fclose(game_data);
  return 1;
}
else
{
  game_record[0]=game_record[1]=game_record[2]=game_record[3]=points=com_points_in_data=0;
  fclose(game_data);
  return 1;
}
}
//................................................../continue game()....................................................
//..................................................begaining()....................................................
int begaining(int status)
{
if(status==1)
{
  int temp=continue_game();
  //...----->>>>
  if(temp==-1)
  {
   printf(" Unable to find Previous Data You need to start a New Game.");
   return 0;
  }
  else if(temp==0)
  {
   printf(" You Do Not Have Enough Money In the Bank You need to start a New game.");
   return 0;
  }
  else
  {
   printf(" Previous data loaded ");
   rewind(game_data);
   return 1;
  }
}
else
{
  money=100;
  com_points_in_data=0;
  points=0;
  printf(" Starting a New game ");
  return 1;
}
}
//................................................../begaining()....................................................
//..................................................dealing()....................................................
int dealing()
{

return (1+(rand()%13));
}
//................................................../dealing()....................................................
//..................................................playing()....................................................
void playing(int status)
{
int bet=0;
int flag=1;
int card=0;
int players_points=0;
int com_points=0;
printf(" MONEY:%d ",money);
printf(" POINTS:%d",points);
if(status==1&&bet_in_data>0)
{
  bet=bet_in_data;
  players_points=points;
  com_points=com_points_in_data;
}
else
{
  while(flag)
  {
   printf(" Enter bet money(less then%d):",money);
   scanf("%d",&bet);
   if(bet<=money)
   {
    money-=bet;
    flag=0;
   }
   else
   {
    printf(" Please enter amount less then Money you have(%d)",money);
   }
  }
  money-=bet;
}
flag=1;
while(flag==1)
{
  
  if(com_points!=0)
  {
   printf(" Do you Want to Show Cards_Points(0 for yes / 1 to continue):");
   scanf("%d",&flag);
  }
  if(flag==1)
  {
   srand(time(0));
   printf(" Takeing a card from deck for you::");
   card=dealing();
   printf(" %s ",deck[card]);
   if(card>=1&&card<=9)
   {
    players_points+=card;
   }
   else
   {
    players_points+=10;
   }
   if(players_points==BLACKJECK)
   {
    printf("!!!!!!.........BLACLJECK.......!!!!!!!!");
    money+=bet;
    money+=(int)bet*1.5;
    bet=0;
    players_points=0;
    game_record[2]+=1;
    flag=2;
   }
   else if(players_points>BLACKJECK)
   {
    printf("!!!!!!!.......You LOSE......Points:%d......!!!!!!!",players_points);
    bet=0;
    players_points=0;
    game_record[0]+=1;
    flag=2;
   }
   else
   {
   }
   
   if(flag!=2)
   {
    //----------------------------------------
    //delay(2);
    //srand(time(0));
    printf(" Takeing a card from deck for computer..... ");
    card=dealing();
    printf(" %s ",deck[card]);
    if(card>=1&&card<=9)
    {
     com_points+=card;
    }
    else
    {
     com_points+=10;
    }
    if(com_points==BLACKJECK)
    {
     printf("!!!!!!......... COMPUTERS BLACLJECK.......!!!!!!!!");
     game_record[0]+=1;
     money-=(int)bet*0.5;
     bet=0;
     players_points=com_points=0;
     flag=2;
    }
    else if(com_points>BLACKJECK)
    {
     printf("!!!!!!!.......COMPUTER LOSE.....Points:%d........!!!!!!!",com_points);
     money+=bet*2;
     bet=0;
     players_points=com_points=0;
     game_record[1]+=1;
     flag=2;
    }
    else
    {
    }
    //----------------------------------------------
   }
  }
  
  while(flag!=0&&flag!=2)
  {
   printf(" Press 2 to Save game and exit to main menu(2 for save / 1 for continue):");
   scanf("%d",&flag);
   if(flag==1||flag==2)
   {
    break;
   }
   else
   {
    printf(" Please Enter valid input..!!!");
   }
  }
}

if(flag==0)
{
  printf(" your points:%d Computers point:%d",players_points,com_points);
  if(players_points<com_points)
  {
   printf(" !!!!!!!!!.........You LOSE.........!!!!!!!!!!");
   money-=bet;
   players_points=com_points=0;
   bet=0;
   game_record[2]+=1;
  }
  else if(players_points>com_points)
  {
   printf(" !!!!!!!!!.........You WON.........!!!!!!!!!!");
   money+=bet*2;
   players_points=com_points=0;
   bet=0;
   game_record[2]+=1;
  }
  else
  {
   printf(" !!!!!!!!!.........DRAW.........!!!!!!!!!!");
   money+=bet;
   players_points=com_points=0;
   bet=0;
   game_record[3]+=1;
  }
}
game_data=fopen("./game_data.txt","w");
if(game_data!=NULL)
{
  fprintf(game_data,"%d %d ",money,bet);
  fprintf(game_data,"%d %d ",players_points,com_points);
  fprintf(game_data,"%d %d %d %d",game_record[0],game_record[1],game_record[2],game_record[3]);
}
else
{
  printf(" !!!!!!.....Data Not saved...Try Next Time With The fresh game");
}
fclose(game_data);
}
//................................................../playing()....................................................
//..................................................main()....................................................
int main()
{
int ch=-1;
printf(" .................WELCOME TO THE BLACKJECK.........................");
while(ch!=0)
{
  printf(" Press 1 to Contiue.");
  printf(" Press 2 to New Game.");
  printf(" Press 0 to Exit.");
  printf(" Enter yourr choice:");
  scanf("%d",&ch);
  switch(ch)
  {
   case 1:
    if(begaining(1))
    {
     playing(1);
    }
    break;
   case 2:
    if(begaining(2))
    {
     playing(2);
    }
    break;
    break;
   case 0:
    return 1;
    break;
   default :
    printf(" Invalid choice");
  }
}
}

File Data Should Be In this format

file name:-game_data.txt

-------------------------------------

money<tab>previous_bet<nextline>

players_point<tab>computers_points<next_line>

no_of_lose<tab>no_of_win<tab>no_of_BLACKJECK<tab>no_of_draw

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