Im trying to write a blackjack game for class. Im confusedabout how to use funct
ID: 3609811 • Letter: I
Question
Im trying to write a blackjack game for class. Im confusedabout how to use functions in C. I dont know how to call themto the main function. I think my logic is correct, but myunderstanding of the technical aspect isnt there. Forinstance, in the compare function I dont know how to access thefunction so that it displays what it prints out. Here is thecode I have written so far. I would appreciate any help:#include<stdio.h>
#include <stdlib.h>
#include <time.h>
int randNum(void);
int Dealer(Dcard1, Dcard2, Dscore);
int PlayerScore(card1, card2, choice, score);
int Compare
int main(void)
{
srand(time(NULL))
printf("Welcome to Blackjack!");
}
int randNum(void)
{
return rand() % 9 + 1;
}
int Dealer(int Dcard1, Dcard2, Dscore)
{
randNum() = Dcard1
randNum() = Dcard2
printf("Dealer shows: %d", Dcard1);
Dscore = Dcard1 + Dcard2
while (Dscore < 16)
{
Dscore = randNum() + Dscore
}
return Dscore;
}
int PlayerScore(card1, card2, choice, score)
{
randNum() = card1
randNum() = card2
score = card1 + card2
printf("You have %d", score);
printf("Press 0 to hit and 1 to stay");
scanf("%d", &choice);
while(score < 21 && choice != 1)
{
score = score + randNum();
printf("You have %d", score);
printf("Press 0 to hit and 1 tostay");
scanf("%d", &choice);
}
return score;
}
int Compare(x)
{
if(Dealer > 21 && PlayerScore <= 21)
printf("Dealer busts, youWIN!");
if(PlayerScore > 21)
printf("Bust, You LOSE");
if(PlayerScore == 21)
printf("Player has Blackjack! You win!");
if(PlayerScore == Dealer)
printf("It is a tie, you LOSE");
if(PlayerScore > Dealer)
printf("You WIN");
if(Dealer > PlayerScore)
printf("You Lose");
return x;
}
Explanation / Answer
#include #include #include int randNum(); int Dealer(); int PlayerScore(); int Compare(int x);// int main()// { srand(time(NULL));// printf("Welcome to Blackjack! "); Compare(1); return 0;// } int randNum() { return rand() % 9 + 1; } int Dealer()// { int Dcard1 = randNum(); // int Dcard2 = randNum(); // int Dscore; printf("Dealer shows: %d ", Dcard1); Dscore = Dcard1 + Dcard2; while (Dscore < 16) { Dscore = randNum() + Dscore; } return Dscore; } int PlayerScore() { int card1 = randNum(); int card2 = randNum(); int score = card1 + card2; int choice; printf("You have %d ", score); printf("Press 0 to hit and 1 to stay: "); scanf("%d", &choice); while(score < 21 && choice != 1) { score = score + randNum(); printf("You have %d ", score); printf("Press 0 to hit and 1 tostay: "); scanf("%d", &choice); } return score; } int Compare(int x) { if(Dealer() > 21 && PlayerScore() 21) printf("Bust, You LOSE "); if(PlayerScore() == 21) printf("Player has Blackjack! You win! "); if(PlayerScore() == Dealer()) printf("It is a tie, youLOSE "); if(PlayerScore() > Dealer()) printf("You WIN "); if(Dealer() > PlayerScore()) printf("You Lose "); return x; }Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.