60 734
Assignments Done
97,9%
Successfully Done
In April 2018

Answer to Question #60756 in C++ for john

Question #60756
how to write program for blackjack
Expert's answer
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

/*
I declare that the attached assignment is wholly my own work in accordance
with Seneca Academic Policy. No part of this assignment has been copied
manually or electronically from any other source (including web sites) or
distributed to other students."

Name ________________________ Student ID _________________
*/

#define HIGH 11

int generateRandomCard();
void printCards(char[], int[], unsigned int, unsigned int);

int main() {
srand(time(NULL));
int playerCards[5], dealerCards[5];
unsigned int playerNum = 2, dealerNum = 2;

playerCards[0] = generateRandomCard();
playerCards[1] = generateRandomCard();
unsigned int playerSum = playerCards[0] + playerCards[1];
if (playerSum > 21) {
printf("Your cards are: %d %d = %d OVER! You Lose!\n", playerCards[0], playerCards[1], playerSum);
return 0;
}
printCards("Your", playerCards, playerNum, playerSum);

dealerCards[0] = generateRandomCard();
dealerCards[1] = generateRandomCard();
unsigned int dealerSum = dealerCards[0] + dealerCards[1];
if (dealerSum > 21) {
printf("Dealer cards are: %d %d = %d OVER! You Win!\n", dealerCards[0], dealerCards[1], dealerSum);
return 0;
}
printf("Dealer cards are: ? %d\n", dealerCards[1]);

while (playerNum < 5 && playerSum <= 21) {
int answer;
printf("Do you want another card? 1=(YES) 0=(NO): ");
scanf("%d", &answer);
if (!answer) break;
playerCards[playerNum++] = generateRandomCard();
playerSum += playerCards[playerNum - 1];
printCards("Your", playerCards, playerNum, playerSum);
}
if (playerSum > 21) {
printf("Sorry you went OVER 21! You Lose!\n");
return 0;
}

while (dealerNum < 5 && dealerSum < 17) {
int answer;
dealerCards[dealerNum++] = generateRandomCard();
dealerSum += dealerCards[dealerNum - 1];
printCards("Dealer", dealerCards, dealerNum, dealerSum);
}
if (dealerSum > 21) {
printf("Dealer is OVER 21! You Win!!!\n");
return 0;
}

if (playerSum == dealerSum) {
printf("Player has %d Dealer has %d... A Tie!\n", playerSum, dealerSum);
} else if(playerSum > dealerSum) {
printf("Player has %d Dealer has %d... You WIN!!!\n", playerSum, dealerSum);
}
else {
printf("Dealer has %d Player has %d... Dealer Wins, you Lose!\n", dealerSum, playerSum);
}

return 0;
}

int generateRandomCard() {
return rand() % HIGH + 1;
}

void printCards(char name[], int cards[], unsigned int cardsNum, unsigned int sum) {
printf("%s cards are:", name);
for (int i = 0; i < cardsNum; i++) {
printf(" %d", cards[i]);
}
printf(" = %d\n", sum);
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be first!

Leave a comment

Ask Your question

Submit
Privacy policy Terms and Conditions