my teacher gave me an assignment to complete the blackjack program but I have little understanding of it. I have the majority of it but i'm missing pieces.

NOTE: All of the sections where it says TO DO are the missing pieces.
PLEASE HELP.

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

#define HEARTS   1
#define CLUBS    2
#define SPADES   3
#define DIAMONDS 4

#define ACE    1
#define JACK  11
#define QUEEN 12
#define KING  13

/////////////////////////////////////////////////////////////////////
void print_card(int rank, int suit)
{
	switch (suit)
		{
	case DIAMONDS:
		printf("Diamonds\n");
		break;
	case SPADES:
		printf("Spades\n");
		break;
	case CLUBS:
		printf("Clubs\n");
		break;
	case HEARTS:
		printf("Hearts\n");
		break;
		
	default:
		printf("\n");
	}

	switch (rank)
		{	
	case ACE:
	printf("ACE\n");
	break;
	case JACK:
	printf("JACK\n");
	break;
	case QUEEN:
	printf("QUEEN\n");
	break;
	case KING:
	printf("KING\n");
	break;
		
	default:
		printf("\n");
		}

}

int test_print(void)
{
  print_card(JACK, SPADES);     // Jack of Spades
  print_card(ACE, HEARTS);      // Ace of Hearts
  print_card(4, DIAMONDS);      // 4 of Diamonds
  print_card(6, CLUBS);         // 6 of Clubs
  return 0;
}

void choose_card(int& rank, int& suit)
	{
	rank=rand() % 13+1;
	suit=rand() %  4+1;
  
	}

int test_choose(void)
{
  srand(time(NULL));
  int r, s;
  for(int i = 0;  i < 10;  i++)
    {
      choose_card(r, s);
      print_card(r, s);
    }
  return 0;
}

/* This function should return the score of a card in the game of
   blackjack.  Numeric cards are simple; their scores are the same as
   their rank.  (Three of clubs worth three points.)  Face cards
   (Jack, Queen, King) are each worth 10 points, and the Ace is worth
   11 points.  (In the real game, an Ace may be used either as 1 or as
   11 points, but that makes scoring more complicated so just treat it
   as 11 points for nom.) */
int score_card(int rank)
{
  // TO DO
  return 0;
}


int test_score(void)
{
  printf("%d\n", score_card(ACE));   // 11
  printf("%d\n", score_card(QUEEN)); // 10
  printf("%d\n", score_card(4));     //  4
  printf("%d\n", score_card(KING));  // 10
  printf("%d\n", score_card(2));     //  2
  return 0;
}


void ignore_to_end_of_line(void)
{
  char ignored;
  do {
    scanf("%c", &ignored);
  } while( ignored != '\n' );
}

/* This function should print the prompt (the 'char*' just means we're
   passing a double-quoted string; you can print it in printf with
   "%s".  The function should then read a character and return true
   or false.  If the user typed something wrong, then print an error
   message and try again.  The function should at least accept upper-
   and lower-case y/n.  */

bool ask_yes_no(char* prompt)
{
  char answer;
  printf("%s (y/n)? ", prompt);
  scanf("%c", &answer);
  ignore_to_end_of_line();
  // TO DO..
  return false;
}

int test_ask(void)
{
  if( ask_yes_no("Does this work") )
    {
      printf("You said YES\n");
    }
  else
    {
      printf("You said NO\n");
    }
  return 0;
}


int play_one_hand(void)
{
  bool hit = true;
  int score = 0;
  int num = 0;
  // TO DO... 
  return score;
}


void print_final_result(int score)
{
  printf("Your final score is %d", score);
  if(score == 21)
    {
      printf(" (BLACKJACK)");
    }
  else if(score > 21)
    {
      printf(" (BUST)");
    }
  printf("\n");
}


int main(void)
{
  srand(time(NULL));
  printf("WELCOME TO BLACKJACK.\n");
  do {
    print_final_result(play_one_hand());
  } while(ask_yes_no("Play again"));
  return 0;
}

Recommended Answers

All 2 Replies

Please use CODE tags in your code. You typed right over the watermark that explained how to use them.

Well, the first thing "TO DO" is to decide what you need "to do" for each section you labeled "TO DO". That is the first part of your assignment. Once you figure that out, we can help you implement the code (starting of course with your attempt).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.