Question: I need some help fixing my code. This is a blackjack game and I am having a little trouble with when the player wins or
I need some help fixing my code. This is a blackjack game and I am having a little trouble with when the player wins or loses. It is saying sometimes when the Player = 18 points and the Dealer = 23 points that the Player won when they didn't. I am a little stuck on this.
#include
#include
#include
#include
//function prototypes
char getachar(void);
void report(double bankroll, double bet, int gamerecord[]);
int dealing(void);
void playing(double *bankroll, double *bet, int gamerecord[]);
int facevalue(int face_value);
void beginning(double *bankroll, int gamerecord[]);
void ending(double bankroll, int gamerecord[]);
int main(void){
double bankroll = 1000;
double bet = 0;
int gamerecord[] = {0, 0, 0, 0};
char enter = 'n';
// reading saved results if any
beginning(&bankroll, gamerecord);
ending(bankroll, gamerecord);
/* playing a game */
do {
playing(&bankroll, &bet, gamerecord);
//printf("Dealing value: %d ", dealing());
//report the results;
report(bankroll, bet, gamerecord);
//quit by input
printf(" Press y to continue playing:");
enter = getchar();
}
while ((enter == 'Y' || enter == 'y') && (bankroll >= 10));
//ending();
printf(" Thanks for plying! ");
}
// void report
char getachar(void) {
char c = ' ';
/*system(*stty raw echo*);
scanf(" %c", &c);
system(*stty cooked echo*);
return(c); */
return c;
}
void report(double bankroll, double bet , int gamerecord[]){
printf(" The amount of money win or lost : %f ", bet);
printf(" The current value of the bank roll : %f ", bankroll);
printf(" The number of time the play won : %d ", gamerecord[0]);
printf(" The number of time the play hit blackjack: %d ", gamerecord[1]);
printf("The number of time the play hit blackjack: %d ", gamerecord[2]);
printf(" The number of time the play got busted : %d ", gamerecord[3]);
}
int dealing(void) {
int random_n;
random_n = 1 + rand() % 13;
switch (random_n) {
case 1:
printf("Dealing: A "); break;
case 11:
printf("Dealing: J "); break;
case 12:
printf("Dealing: Q "); break;
case 13:
printf("Dealing: K "); break;
default:
printf("Dealing: %d ", random_n);
}
if (random_n > 10){
random_n = 10;
return random_n;
}
}
void playing(double *bankroll, double *bet, int gamerecord[]) {
int player1, player2, player_total;
int dealer1, dealer2, dealer_total;
char ch;
srand((unsigned int)(time(NULL)));
printf(" Please place bet from 10 to 1000 within your bank roll of %.2f:", *bankroll);
scanf(" %lf",bet);
// dealing
printf(" Press a key for the player card 1. ");
ch = getachar();
player1= dealing();
printf(" Press a key for the player card 2. ");
ch = getachar();
player2= dealing();
printf("Press a key for the dealer card 1. ");
ch = getchar();
dealer1 = dealing();
printf("Press a key for the dealer card 2. ");
ch = getchar();
dealer2 = dealing();
//report initial dealing ad get points
printf(" The dealer has a hidden face value card ");
dealer1 = facevalue(dealer1);
printf(" The player has: ");
player1 = facevalue(player1);
player2 = facevalue(player2);
printf(" Press any key to continues. ");
//player hitting
player_total = player1 + player2;
ch = ' '; //space
if (player_total == 21) ch ='a';
while (player_total < 21 && ch == ' ') {
printf(" Player: press space key to hit or any other key to stand. ");
ch = getchar();
if (ch == ' ') {
player1 = dealing();
player1= facevalue(player1);
player_total += player1;
}
}
if (player_total >=21) {
printf(" Player: you are busted with total points: %d ", player_total);
*bet = -(*bet);
*bankroll += *bet;
gamerecord[1]++;
gamerecord[3]++;
}
else {
printf(" The dealer's hole card face value: ");
dealer2 = facevalue(dealer2);
dealer_total = dealer1 + dealer2;
printf(" and total points: %d ", dealer_total);
//if (dealer_total == 21 && playr_total == 21 && ch == "a")
//dealer hitting
while (dealer_total<17) {
dealer1 = dealing();
dealer1 = facevalue(dealer1);
dealer_total += dealer1;
}
//final results update
if (dealer_total>21 || dealer_total < player_total) {
printf(" Player: you win!");
printf(" Player you win: your points= %d, the dealer points =%d ", player_total, dealer_total);
*bankroll += *bet;
gamerecord[0]++;
}
else if (dealer_total > player_total) {
printf(" Player you lost: your points= %d, the dealer points =%d ",player_total, dealer_total);
*bet = -(*bet);
*bankroll += *bet;
gamerecord[1]++;
}
else {
printf(" Player it's a draw: your points= %d, the dealer points =%d ",player_total, dealer_total);
*bet = 0;
}
}
}
int facevalue(int face_value) {
switch (face_value) {
case 1:
printf("A "); break;
case 11:
printf("J "); break;
case 12:
printf("Q "); break;
case 13:
printf("K "); break;
default:
printf(" %d ", face_value);
}
if (face_value > 10)
return 10;
if (face_value ==1)
return 11;
else
return face_value;
}
void beginning(double *bankroll, int gamerecord[]){
FILE *fp;
int status = 0;
fp = fopen("Project1", "r");
if (fp != NULL){
status = fscanf(fp," %lf%d%d%d%d", bankroll, &gamerecord[0], &gamerecord[1], &gamerecord[2], &gamerecord[3]);
fclose(fp);
}
if (status != 5 || *bankroll <10){
*bankroll = 1000.0;
for(status = 0; status <4 ; status++)
gamerecord[status] =0;
}
return;
}
void ending(double bankroll, int gamerecord[]) {
FILE *fp;
fp = fopen ("Project1", "w");
fprintf(fp," %.2f %d %d %d %d", bankroll, gamerecord[0], gamerecord[1], gamerecord[2], gamerecord[3]);
fclose(fp);
return;
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
