Blackjack Code Needs Betting System [closed]











up vote
-6
down vote

favorite












My coding teacher gave us this code and told us to add a betting system.
specifically:



"Add a betting system. The user should start out with $200 and be
able to bet any amount as long as they have money to bet.
EX: You have $200
Place your bet: $50 ...after a few hands
You have $30
Place your bet: $40
Sorry ... you don't have enough to place this bet
You have $75
Do you want to bet or quit?
Quit when the user has no money left, or wants to stop."



Here is the the code:



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

#define BELL 'a'
#define DEALER 0
#define PLAYER 1
#define SCREENLINECOUNT 25


#define ACELOW 0
#define ACEHIGH 1

int askedForName = 0;

void dispTitle(void);
void initCardsScreen(int cards[52], int playerPoints[2], int dealerPoints[2],
int total[2], int *numbCards);
int dealCard(int *numCards, int cards[52]);
void dispCard(int cardDrawn, int points[2]);
void totalIt(int points[2], int total[2], int who);
void dealerGetsCard(int *numCards, int cards[52], int dealerPoints[2]);
void playerGetsCard(int *numCards, int cards[52], int playerPoints[2]);
char getAns(char mesg);
void findWinner(int total[2]);

int main(void) {
int numCards; /* Equals 52 at the beginning of each game. */
int cards[52], playerPoints[2], dealerPoints[2], total[2];
char ans; /* for user's Hit/Stand or Yes/No response. */

do {
initCardsScreen(cards, playerPoints, dealerPoints, total, &numCards);
dealerGetsCard(&numCards, cards, dealerPoints);
printf("n"); /* Prints a blank line */
playerGetsCard(&numCards, cards, playerPoints);
playerGetsCard(&numCards, cards, playerPoints);

do {
ans = getAns("Hit or stand (H/S)? ");
if (ans == 'H') {
playerGetsCard(&numCards, cards, playerPoints);
}
} while (ans != 'S');


/* Generate player's total score once the player stands. */
totalIt(playerPoints, total, PLAYER);
do {
dealerGetsCard(&numCards, cards, dealerPoints);
} while (dealerPoints[ACEHIGH] < 17);

/* Dealer has to stand at 17. */
totalIt(dealerPoints, total, DEALER);

/* Calculate the dealer's hand total. */
findWinner(total);
ans = getAns("nPlay again (Y/N)? ");

} while (ans == 'Y');

return;
}




void initCardsScreen(int cards[52], int playerPoints[2], int dealerPoints[2],
int total[2], int *numCards) {

int sub, val = 1; /* This function's Work variables */
char firstName[15]; /* Holds the user's first name. */
*numCards = 52; /* Holds running total of number of cards */

for (sub = 0; sub <= 51; sub++) {
val = (val == 14) ? 1 : val; /* When val hits 14, reset to 1. */
cards[sub] = val;
val++;
}

for (sub = 0; sub <= 1; sub++) {
/* Reset all the points counters. */
playerPoints[sub] = dealerPoints[sub] = total[sub] = 0;
}

dispTitle();

/* Only ask for the player's name once per session. */
if (askedForName == 0) {
printf("nWhat is your first name? ");
scanf(" %s", firstName); /* Buffer overflow attack possible here. */
askedForName = 1; /* Only ask once. */
printf("Ok, %s, get ready for some casino action!nn", firstName);
getchar(); /* Discards a newline. Text claims one can ignore the */
/* compiler warning generated. We'll see. */
}
return;
}

void playerGetsCard(int *numCards, int cards[52], int playerPoints[2]) {
int newCard;

newCard = dealCard(numCards, cards);
printf("You draw: ");
dispCard(newCard, playerPoints);
}

void dealerGetsCard(int *numCards, int cards[52], int dealerPoints[2]) {
int newCard;

newCard = dealCard(numCards, cards);
printf("The dealer draws: ");
dispCard(newCard, dealerPoints);
}

int dealCard(int *numCards, int cards[52]) {
int cardDrawn, subDraw;
time_t t; /* Gets time for a random value */

srand(time(&t)); /* Seeds the random-number generator. */
subDraw = (rand() % (*numCards)); /* From 0 to numCards */
cardDrawn = cards[subDraw];
cards[subDraw] = cards[*numCards - 1];
(*numCards)--;
return cardDrawn;
}

void dispCard(int cardDrawn, int points[2]) {
switch (cardDrawn) {

case(11) : printf("%sn", "Jack");
points[ACELOW] += 10;
points[ACEHIGH] += 10;
break;
case(12) : printf("%sn", "Queen");
points[ACELOW] += 10;
points[ACEHIGH] += 10;
break;
case(13) : printf("%sn", "King");
points[ACELOW] += 10;
points[ACEHIGH] += 10;
break;
default: points[ACELOW] += cardDrawn;
if (cardDrawn == 1) {
printf("%sn", "Ace");
points[ACEHIGH] += 11;
}
else {
points[ACEHIGH] += cardDrawn;
printf("%dn", cardDrawn);
}
}
return;
}

void totalIt(int points[2], int total[2], int who) {

if ((points[ACELOW] == points[ACEHIGH]) || (points[ACEHIGH] > 21)) {
total[who] = points[ACELOW]; /* Treat all Aces as 1. */
}
else {
total[who] = points[ACEHIGH]; /* Keeps all Aces as 11. */
}

if (who == PLAYER) { /* Determines the message printed. */
printf("You have a total of %dnn", total[PLAYER]);
}
else {
printf("The house stands with a total of %dnn", total[DEALER]);
}
return;
}

void findWinner(int total[2]) {
if (total[DEALER] == 21) {
printf("The house wins.n");
return;
}
if ((total[DEALER] > 21) && (total[PLAYER] > 21)) {
printf("Nobody wins.n");
return;
}
if ((total[DEALER] >= total[PLAYER]) && (total[DEALER] < 21)) {
printf("The house wins.n");
return;
}
if ((total[PLAYER] > 21) && (total[DEALER] < 21)) {
printf("The house wins.n");
return;
}
printf("%s%c", "You win!n", BELL);
return;
}

char getAns(char mesg) {
char ans;

printf("%s", mesg); /* Prints the prompt message passed. */
ans = getchar();
getchar(); /* Discards newline. Claims we can ignore compiler warning here. */
return toupper(ans);
}

void dispTitle(void) {
int i = 0;

while (i < SCREENLINECOUNT) {
printf("n"); /* Clears screen by printing a number of blank lines. */
i++;
}

printf(" .------..------..------..------..------..------..------..------..------. n");
printf(" |B.--. ||L.--. ||A.--. ||C.--. ||K.--. ||J.--. ||A.--. ||C.--. ||K.--. | n");
printf(" | :(): || :/: || (/) || :/: || :/: || :(): || (/) || :/: || :/: | n");
printf(" | ()() || (__) || :/: || :/: || :/: || ()() || :/: || :/: || :/: | n");
printf(" | '--'B|| '--'L|| '--'A|| '--'C|| '--'K|| '--'J|| '--'A|| '--'C|| '--'K| n");
printf(" `------'`------'`------'`------'`------'`------'`------'`------'`------' n");

printf("nnStep right up to the Blackjack tablesnn");
return;
}









share|improve this question













closed as off-topic by Hosch250, Zeta, chux, Dannnno, janos Dec 12 at 19:39


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "Code not implemented or not working as intended: Code Review is a community where programmers peer-review your working code to address issues such as security, maintainability, performance, and scalability. We require that the code be working correctly, to the best of the author's knowledge, before proceeding with a review." – Hosch250, Zeta, chux, Dannnno, janos

If this question can be reworded to fit the rules in the help center, please edit the question.









  • 5




    This is off-topic for two reasons. One you are asking us to write you code. We don't do that. You write the code and we review it. Second you didnt write this. Your teacher did. Please see the help center for more derails.
    – bruglesco
    Dec 12 at 18:57

















up vote
-6
down vote

favorite












My coding teacher gave us this code and told us to add a betting system.
specifically:



"Add a betting system. The user should start out with $200 and be
able to bet any amount as long as they have money to bet.
EX: You have $200
Place your bet: $50 ...after a few hands
You have $30
Place your bet: $40
Sorry ... you don't have enough to place this bet
You have $75
Do you want to bet or quit?
Quit when the user has no money left, or wants to stop."



Here is the the code:



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

#define BELL 'a'
#define DEALER 0
#define PLAYER 1
#define SCREENLINECOUNT 25


#define ACELOW 0
#define ACEHIGH 1

int askedForName = 0;

void dispTitle(void);
void initCardsScreen(int cards[52], int playerPoints[2], int dealerPoints[2],
int total[2], int *numbCards);
int dealCard(int *numCards, int cards[52]);
void dispCard(int cardDrawn, int points[2]);
void totalIt(int points[2], int total[2], int who);
void dealerGetsCard(int *numCards, int cards[52], int dealerPoints[2]);
void playerGetsCard(int *numCards, int cards[52], int playerPoints[2]);
char getAns(char mesg);
void findWinner(int total[2]);

int main(void) {
int numCards; /* Equals 52 at the beginning of each game. */
int cards[52], playerPoints[2], dealerPoints[2], total[2];
char ans; /* for user's Hit/Stand or Yes/No response. */

do {
initCardsScreen(cards, playerPoints, dealerPoints, total, &numCards);
dealerGetsCard(&numCards, cards, dealerPoints);
printf("n"); /* Prints a blank line */
playerGetsCard(&numCards, cards, playerPoints);
playerGetsCard(&numCards, cards, playerPoints);

do {
ans = getAns("Hit or stand (H/S)? ");
if (ans == 'H') {
playerGetsCard(&numCards, cards, playerPoints);
}
} while (ans != 'S');


/* Generate player's total score once the player stands. */
totalIt(playerPoints, total, PLAYER);
do {
dealerGetsCard(&numCards, cards, dealerPoints);
} while (dealerPoints[ACEHIGH] < 17);

/* Dealer has to stand at 17. */
totalIt(dealerPoints, total, DEALER);

/* Calculate the dealer's hand total. */
findWinner(total);
ans = getAns("nPlay again (Y/N)? ");

} while (ans == 'Y');

return;
}




void initCardsScreen(int cards[52], int playerPoints[2], int dealerPoints[2],
int total[2], int *numCards) {

int sub, val = 1; /* This function's Work variables */
char firstName[15]; /* Holds the user's first name. */
*numCards = 52; /* Holds running total of number of cards */

for (sub = 0; sub <= 51; sub++) {
val = (val == 14) ? 1 : val; /* When val hits 14, reset to 1. */
cards[sub] = val;
val++;
}

for (sub = 0; sub <= 1; sub++) {
/* Reset all the points counters. */
playerPoints[sub] = dealerPoints[sub] = total[sub] = 0;
}

dispTitle();

/* Only ask for the player's name once per session. */
if (askedForName == 0) {
printf("nWhat is your first name? ");
scanf(" %s", firstName); /* Buffer overflow attack possible here. */
askedForName = 1; /* Only ask once. */
printf("Ok, %s, get ready for some casino action!nn", firstName);
getchar(); /* Discards a newline. Text claims one can ignore the */
/* compiler warning generated. We'll see. */
}
return;
}

void playerGetsCard(int *numCards, int cards[52], int playerPoints[2]) {
int newCard;

newCard = dealCard(numCards, cards);
printf("You draw: ");
dispCard(newCard, playerPoints);
}

void dealerGetsCard(int *numCards, int cards[52], int dealerPoints[2]) {
int newCard;

newCard = dealCard(numCards, cards);
printf("The dealer draws: ");
dispCard(newCard, dealerPoints);
}

int dealCard(int *numCards, int cards[52]) {
int cardDrawn, subDraw;
time_t t; /* Gets time for a random value */

srand(time(&t)); /* Seeds the random-number generator. */
subDraw = (rand() % (*numCards)); /* From 0 to numCards */
cardDrawn = cards[subDraw];
cards[subDraw] = cards[*numCards - 1];
(*numCards)--;
return cardDrawn;
}

void dispCard(int cardDrawn, int points[2]) {
switch (cardDrawn) {

case(11) : printf("%sn", "Jack");
points[ACELOW] += 10;
points[ACEHIGH] += 10;
break;
case(12) : printf("%sn", "Queen");
points[ACELOW] += 10;
points[ACEHIGH] += 10;
break;
case(13) : printf("%sn", "King");
points[ACELOW] += 10;
points[ACEHIGH] += 10;
break;
default: points[ACELOW] += cardDrawn;
if (cardDrawn == 1) {
printf("%sn", "Ace");
points[ACEHIGH] += 11;
}
else {
points[ACEHIGH] += cardDrawn;
printf("%dn", cardDrawn);
}
}
return;
}

void totalIt(int points[2], int total[2], int who) {

if ((points[ACELOW] == points[ACEHIGH]) || (points[ACEHIGH] > 21)) {
total[who] = points[ACELOW]; /* Treat all Aces as 1. */
}
else {
total[who] = points[ACEHIGH]; /* Keeps all Aces as 11. */
}

if (who == PLAYER) { /* Determines the message printed. */
printf("You have a total of %dnn", total[PLAYER]);
}
else {
printf("The house stands with a total of %dnn", total[DEALER]);
}
return;
}

void findWinner(int total[2]) {
if (total[DEALER] == 21) {
printf("The house wins.n");
return;
}
if ((total[DEALER] > 21) && (total[PLAYER] > 21)) {
printf("Nobody wins.n");
return;
}
if ((total[DEALER] >= total[PLAYER]) && (total[DEALER] < 21)) {
printf("The house wins.n");
return;
}
if ((total[PLAYER] > 21) && (total[DEALER] < 21)) {
printf("The house wins.n");
return;
}
printf("%s%c", "You win!n", BELL);
return;
}

char getAns(char mesg) {
char ans;

printf("%s", mesg); /* Prints the prompt message passed. */
ans = getchar();
getchar(); /* Discards newline. Claims we can ignore compiler warning here. */
return toupper(ans);
}

void dispTitle(void) {
int i = 0;

while (i < SCREENLINECOUNT) {
printf("n"); /* Clears screen by printing a number of blank lines. */
i++;
}

printf(" .------..------..------..------..------..------..------..------..------. n");
printf(" |B.--. ||L.--. ||A.--. ||C.--. ||K.--. ||J.--. ||A.--. ||C.--. ||K.--. | n");
printf(" | :(): || :/: || (/) || :/: || :/: || :(): || (/) || :/: || :/: | n");
printf(" | ()() || (__) || :/: || :/: || :/: || ()() || :/: || :/: || :/: | n");
printf(" | '--'B|| '--'L|| '--'A|| '--'C|| '--'K|| '--'J|| '--'A|| '--'C|| '--'K| n");
printf(" `------'`------'`------'`------'`------'`------'`------'`------'`------' n");

printf("nnStep right up to the Blackjack tablesnn");
return;
}









share|improve this question













closed as off-topic by Hosch250, Zeta, chux, Dannnno, janos Dec 12 at 19:39


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "Code not implemented or not working as intended: Code Review is a community where programmers peer-review your working code to address issues such as security, maintainability, performance, and scalability. We require that the code be working correctly, to the best of the author's knowledge, before proceeding with a review." – Hosch250, Zeta, chux, Dannnno, janos

If this question can be reworded to fit the rules in the help center, please edit the question.









  • 5




    This is off-topic for two reasons. One you are asking us to write you code. We don't do that. You write the code and we review it. Second you didnt write this. Your teacher did. Please see the help center for more derails.
    – bruglesco
    Dec 12 at 18:57















up vote
-6
down vote

favorite









up vote
-6
down vote

favorite











My coding teacher gave us this code and told us to add a betting system.
specifically:



"Add a betting system. The user should start out with $200 and be
able to bet any amount as long as they have money to bet.
EX: You have $200
Place your bet: $50 ...after a few hands
You have $30
Place your bet: $40
Sorry ... you don't have enough to place this bet
You have $75
Do you want to bet or quit?
Quit when the user has no money left, or wants to stop."



Here is the the code:



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

#define BELL 'a'
#define DEALER 0
#define PLAYER 1
#define SCREENLINECOUNT 25


#define ACELOW 0
#define ACEHIGH 1

int askedForName = 0;

void dispTitle(void);
void initCardsScreen(int cards[52], int playerPoints[2], int dealerPoints[2],
int total[2], int *numbCards);
int dealCard(int *numCards, int cards[52]);
void dispCard(int cardDrawn, int points[2]);
void totalIt(int points[2], int total[2], int who);
void dealerGetsCard(int *numCards, int cards[52], int dealerPoints[2]);
void playerGetsCard(int *numCards, int cards[52], int playerPoints[2]);
char getAns(char mesg);
void findWinner(int total[2]);

int main(void) {
int numCards; /* Equals 52 at the beginning of each game. */
int cards[52], playerPoints[2], dealerPoints[2], total[2];
char ans; /* for user's Hit/Stand or Yes/No response. */

do {
initCardsScreen(cards, playerPoints, dealerPoints, total, &numCards);
dealerGetsCard(&numCards, cards, dealerPoints);
printf("n"); /* Prints a blank line */
playerGetsCard(&numCards, cards, playerPoints);
playerGetsCard(&numCards, cards, playerPoints);

do {
ans = getAns("Hit or stand (H/S)? ");
if (ans == 'H') {
playerGetsCard(&numCards, cards, playerPoints);
}
} while (ans != 'S');


/* Generate player's total score once the player stands. */
totalIt(playerPoints, total, PLAYER);
do {
dealerGetsCard(&numCards, cards, dealerPoints);
} while (dealerPoints[ACEHIGH] < 17);

/* Dealer has to stand at 17. */
totalIt(dealerPoints, total, DEALER);

/* Calculate the dealer's hand total. */
findWinner(total);
ans = getAns("nPlay again (Y/N)? ");

} while (ans == 'Y');

return;
}




void initCardsScreen(int cards[52], int playerPoints[2], int dealerPoints[2],
int total[2], int *numCards) {

int sub, val = 1; /* This function's Work variables */
char firstName[15]; /* Holds the user's first name. */
*numCards = 52; /* Holds running total of number of cards */

for (sub = 0; sub <= 51; sub++) {
val = (val == 14) ? 1 : val; /* When val hits 14, reset to 1. */
cards[sub] = val;
val++;
}

for (sub = 0; sub <= 1; sub++) {
/* Reset all the points counters. */
playerPoints[sub] = dealerPoints[sub] = total[sub] = 0;
}

dispTitle();

/* Only ask for the player's name once per session. */
if (askedForName == 0) {
printf("nWhat is your first name? ");
scanf(" %s", firstName); /* Buffer overflow attack possible here. */
askedForName = 1; /* Only ask once. */
printf("Ok, %s, get ready for some casino action!nn", firstName);
getchar(); /* Discards a newline. Text claims one can ignore the */
/* compiler warning generated. We'll see. */
}
return;
}

void playerGetsCard(int *numCards, int cards[52], int playerPoints[2]) {
int newCard;

newCard = dealCard(numCards, cards);
printf("You draw: ");
dispCard(newCard, playerPoints);
}

void dealerGetsCard(int *numCards, int cards[52], int dealerPoints[2]) {
int newCard;

newCard = dealCard(numCards, cards);
printf("The dealer draws: ");
dispCard(newCard, dealerPoints);
}

int dealCard(int *numCards, int cards[52]) {
int cardDrawn, subDraw;
time_t t; /* Gets time for a random value */

srand(time(&t)); /* Seeds the random-number generator. */
subDraw = (rand() % (*numCards)); /* From 0 to numCards */
cardDrawn = cards[subDraw];
cards[subDraw] = cards[*numCards - 1];
(*numCards)--;
return cardDrawn;
}

void dispCard(int cardDrawn, int points[2]) {
switch (cardDrawn) {

case(11) : printf("%sn", "Jack");
points[ACELOW] += 10;
points[ACEHIGH] += 10;
break;
case(12) : printf("%sn", "Queen");
points[ACELOW] += 10;
points[ACEHIGH] += 10;
break;
case(13) : printf("%sn", "King");
points[ACELOW] += 10;
points[ACEHIGH] += 10;
break;
default: points[ACELOW] += cardDrawn;
if (cardDrawn == 1) {
printf("%sn", "Ace");
points[ACEHIGH] += 11;
}
else {
points[ACEHIGH] += cardDrawn;
printf("%dn", cardDrawn);
}
}
return;
}

void totalIt(int points[2], int total[2], int who) {

if ((points[ACELOW] == points[ACEHIGH]) || (points[ACEHIGH] > 21)) {
total[who] = points[ACELOW]; /* Treat all Aces as 1. */
}
else {
total[who] = points[ACEHIGH]; /* Keeps all Aces as 11. */
}

if (who == PLAYER) { /* Determines the message printed. */
printf("You have a total of %dnn", total[PLAYER]);
}
else {
printf("The house stands with a total of %dnn", total[DEALER]);
}
return;
}

void findWinner(int total[2]) {
if (total[DEALER] == 21) {
printf("The house wins.n");
return;
}
if ((total[DEALER] > 21) && (total[PLAYER] > 21)) {
printf("Nobody wins.n");
return;
}
if ((total[DEALER] >= total[PLAYER]) && (total[DEALER] < 21)) {
printf("The house wins.n");
return;
}
if ((total[PLAYER] > 21) && (total[DEALER] < 21)) {
printf("The house wins.n");
return;
}
printf("%s%c", "You win!n", BELL);
return;
}

char getAns(char mesg) {
char ans;

printf("%s", mesg); /* Prints the prompt message passed. */
ans = getchar();
getchar(); /* Discards newline. Claims we can ignore compiler warning here. */
return toupper(ans);
}

void dispTitle(void) {
int i = 0;

while (i < SCREENLINECOUNT) {
printf("n"); /* Clears screen by printing a number of blank lines. */
i++;
}

printf(" .------..------..------..------..------..------..------..------..------. n");
printf(" |B.--. ||L.--. ||A.--. ||C.--. ||K.--. ||J.--. ||A.--. ||C.--. ||K.--. | n");
printf(" | :(): || :/: || (/) || :/: || :/: || :(): || (/) || :/: || :/: | n");
printf(" | ()() || (__) || :/: || :/: || :/: || ()() || :/: || :/: || :/: | n");
printf(" | '--'B|| '--'L|| '--'A|| '--'C|| '--'K|| '--'J|| '--'A|| '--'C|| '--'K| n");
printf(" `------'`------'`------'`------'`------'`------'`------'`------'`------' n");

printf("nnStep right up to the Blackjack tablesnn");
return;
}









share|improve this question













My coding teacher gave us this code and told us to add a betting system.
specifically:



"Add a betting system. The user should start out with $200 and be
able to bet any amount as long as they have money to bet.
EX: You have $200
Place your bet: $50 ...after a few hands
You have $30
Place your bet: $40
Sorry ... you don't have enough to place this bet
You have $75
Do you want to bet or quit?
Quit when the user has no money left, or wants to stop."



Here is the the code:



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

#define BELL 'a'
#define DEALER 0
#define PLAYER 1
#define SCREENLINECOUNT 25


#define ACELOW 0
#define ACEHIGH 1

int askedForName = 0;

void dispTitle(void);
void initCardsScreen(int cards[52], int playerPoints[2], int dealerPoints[2],
int total[2], int *numbCards);
int dealCard(int *numCards, int cards[52]);
void dispCard(int cardDrawn, int points[2]);
void totalIt(int points[2], int total[2], int who);
void dealerGetsCard(int *numCards, int cards[52], int dealerPoints[2]);
void playerGetsCard(int *numCards, int cards[52], int playerPoints[2]);
char getAns(char mesg);
void findWinner(int total[2]);

int main(void) {
int numCards; /* Equals 52 at the beginning of each game. */
int cards[52], playerPoints[2], dealerPoints[2], total[2];
char ans; /* for user's Hit/Stand or Yes/No response. */

do {
initCardsScreen(cards, playerPoints, dealerPoints, total, &numCards);
dealerGetsCard(&numCards, cards, dealerPoints);
printf("n"); /* Prints a blank line */
playerGetsCard(&numCards, cards, playerPoints);
playerGetsCard(&numCards, cards, playerPoints);

do {
ans = getAns("Hit or stand (H/S)? ");
if (ans == 'H') {
playerGetsCard(&numCards, cards, playerPoints);
}
} while (ans != 'S');


/* Generate player's total score once the player stands. */
totalIt(playerPoints, total, PLAYER);
do {
dealerGetsCard(&numCards, cards, dealerPoints);
} while (dealerPoints[ACEHIGH] < 17);

/* Dealer has to stand at 17. */
totalIt(dealerPoints, total, DEALER);

/* Calculate the dealer's hand total. */
findWinner(total);
ans = getAns("nPlay again (Y/N)? ");

} while (ans == 'Y');

return;
}




void initCardsScreen(int cards[52], int playerPoints[2], int dealerPoints[2],
int total[2], int *numCards) {

int sub, val = 1; /* This function's Work variables */
char firstName[15]; /* Holds the user's first name. */
*numCards = 52; /* Holds running total of number of cards */

for (sub = 0; sub <= 51; sub++) {
val = (val == 14) ? 1 : val; /* When val hits 14, reset to 1. */
cards[sub] = val;
val++;
}

for (sub = 0; sub <= 1; sub++) {
/* Reset all the points counters. */
playerPoints[sub] = dealerPoints[sub] = total[sub] = 0;
}

dispTitle();

/* Only ask for the player's name once per session. */
if (askedForName == 0) {
printf("nWhat is your first name? ");
scanf(" %s", firstName); /* Buffer overflow attack possible here. */
askedForName = 1; /* Only ask once. */
printf("Ok, %s, get ready for some casino action!nn", firstName);
getchar(); /* Discards a newline. Text claims one can ignore the */
/* compiler warning generated. We'll see. */
}
return;
}

void playerGetsCard(int *numCards, int cards[52], int playerPoints[2]) {
int newCard;

newCard = dealCard(numCards, cards);
printf("You draw: ");
dispCard(newCard, playerPoints);
}

void dealerGetsCard(int *numCards, int cards[52], int dealerPoints[2]) {
int newCard;

newCard = dealCard(numCards, cards);
printf("The dealer draws: ");
dispCard(newCard, dealerPoints);
}

int dealCard(int *numCards, int cards[52]) {
int cardDrawn, subDraw;
time_t t; /* Gets time for a random value */

srand(time(&t)); /* Seeds the random-number generator. */
subDraw = (rand() % (*numCards)); /* From 0 to numCards */
cardDrawn = cards[subDraw];
cards[subDraw] = cards[*numCards - 1];
(*numCards)--;
return cardDrawn;
}

void dispCard(int cardDrawn, int points[2]) {
switch (cardDrawn) {

case(11) : printf("%sn", "Jack");
points[ACELOW] += 10;
points[ACEHIGH] += 10;
break;
case(12) : printf("%sn", "Queen");
points[ACELOW] += 10;
points[ACEHIGH] += 10;
break;
case(13) : printf("%sn", "King");
points[ACELOW] += 10;
points[ACEHIGH] += 10;
break;
default: points[ACELOW] += cardDrawn;
if (cardDrawn == 1) {
printf("%sn", "Ace");
points[ACEHIGH] += 11;
}
else {
points[ACEHIGH] += cardDrawn;
printf("%dn", cardDrawn);
}
}
return;
}

void totalIt(int points[2], int total[2], int who) {

if ((points[ACELOW] == points[ACEHIGH]) || (points[ACEHIGH] > 21)) {
total[who] = points[ACELOW]; /* Treat all Aces as 1. */
}
else {
total[who] = points[ACEHIGH]; /* Keeps all Aces as 11. */
}

if (who == PLAYER) { /* Determines the message printed. */
printf("You have a total of %dnn", total[PLAYER]);
}
else {
printf("The house stands with a total of %dnn", total[DEALER]);
}
return;
}

void findWinner(int total[2]) {
if (total[DEALER] == 21) {
printf("The house wins.n");
return;
}
if ((total[DEALER] > 21) && (total[PLAYER] > 21)) {
printf("Nobody wins.n");
return;
}
if ((total[DEALER] >= total[PLAYER]) && (total[DEALER] < 21)) {
printf("The house wins.n");
return;
}
if ((total[PLAYER] > 21) && (total[DEALER] < 21)) {
printf("The house wins.n");
return;
}
printf("%s%c", "You win!n", BELL);
return;
}

char getAns(char mesg) {
char ans;

printf("%s", mesg); /* Prints the prompt message passed. */
ans = getchar();
getchar(); /* Discards newline. Claims we can ignore compiler warning here. */
return toupper(ans);
}

void dispTitle(void) {
int i = 0;

while (i < SCREENLINECOUNT) {
printf("n"); /* Clears screen by printing a number of blank lines. */
i++;
}

printf(" .------..------..------..------..------..------..------..------..------. n");
printf(" |B.--. ||L.--. ||A.--. ||C.--. ||K.--. ||J.--. ||A.--. ||C.--. ||K.--. | n");
printf(" | :(): || :/: || (/) || :/: || :/: || :(): || (/) || :/: || :/: | n");
printf(" | ()() || (__) || :/: || :/: || :/: || ()() || :/: || :/: || :/: | n");
printf(" | '--'B|| '--'L|| '--'A|| '--'C|| '--'K|| '--'J|| '--'A|| '--'C|| '--'K| n");
printf(" `------'`------'`------'`------'`------'`------'`------'`------'`------' n");

printf("nnStep right up to the Blackjack tablesnn");
return;
}






c






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Dec 12 at 18:53









Yuri

1




1




closed as off-topic by Hosch250, Zeta, chux, Dannnno, janos Dec 12 at 19:39


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "Code not implemented or not working as intended: Code Review is a community where programmers peer-review your working code to address issues such as security, maintainability, performance, and scalability. We require that the code be working correctly, to the best of the author's knowledge, before proceeding with a review." – Hosch250, Zeta, chux, Dannnno, janos

If this question can be reworded to fit the rules in the help center, please edit the question.




closed as off-topic by Hosch250, Zeta, chux, Dannnno, janos Dec 12 at 19:39


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "Code not implemented or not working as intended: Code Review is a community where programmers peer-review your working code to address issues such as security, maintainability, performance, and scalability. We require that the code be working correctly, to the best of the author's knowledge, before proceeding with a review." – Hosch250, Zeta, chux, Dannnno, janos

If this question can be reworded to fit the rules in the help center, please edit the question.








  • 5




    This is off-topic for two reasons. One you are asking us to write you code. We don't do that. You write the code and we review it. Second you didnt write this. Your teacher did. Please see the help center for more derails.
    – bruglesco
    Dec 12 at 18:57
















  • 5




    This is off-topic for two reasons. One you are asking us to write you code. We don't do that. You write the code and we review it. Second you didnt write this. Your teacher did. Please see the help center for more derails.
    – bruglesco
    Dec 12 at 18:57










5




5




This is off-topic for two reasons. One you are asking us to write you code. We don't do that. You write the code and we review it. Second you didnt write this. Your teacher did. Please see the help center for more derails.
– bruglesco
Dec 12 at 18:57






This is off-topic for two reasons. One you are asking us to write you code. We don't do that. You write the code and we review it. Second you didnt write this. Your teacher did. Please see the help center for more derails.
– bruglesco
Dec 12 at 18:57

















active

oldest

votes






















active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes

Popular posts from this blog

Список кардиналов, возведённых папой римским Каликстом III

Deduzione

Mysql.sock missing - “Can't connect to local MySQL server through socket”