Question

in C language only. Write a program called gamecards.c that has a card game logic by...

in C language only.

Write a program called gamecards.c that has a card game logic by comparing the cards from 4 people and calculating which player has the best card.

1. Every card must be represented by exactly two chars
representing a rank and a suit.

ranks: '2', '3', '4', '5', '6', '7', '8', '9', 'T', 'J', 'Q', 'K', 'A'. ('T' = 10)

Suits: 'H', 'D', 'S', 'C'.

7D represents the “7 of Diamond” etc..


2. Write a function called isRank(char c) which
calculate if the given character is one of the ranks. It would return a char with a value of 1 if the character is a valid rank and 0 if not.

Write a function called isSuit(char c) which calculate if the given character is one of the suits. It should return a char with a value of 1 if the character is a valid suit and 0 if not.
(Lowercase are not valid for both functions)

Functions should have return value (char) and if it's true or false;

Homework Answers

Answer #1

Thanks for the question.


Below is the code you will be needing Let me know if you have any doubts or if you need anything to change.


Thank You !!


===========================================================================

#include<stdio.h>

int isRank(char c){
  
   if(('2'<=c && c<='9') ||(c=='T' || c=='J' || c=='Q' || c=='K' || c=='A')){
       return 1;
   }
   return 0;
}

int isSuit(char suit){
  
   if(suit=='H' || suit=='D' || suit=='S' || suit=='C'){
       return 1;
   }
   return 0;
}


int cardValue(char suit, char rank){
  
   int value=0;
   char ranks[]={'2','3','4','5','6','7','8','9','T','J','Q','K','A'};
   char suits[]={'H','D','S','C'};
  
   int index;
   for(index=0; index<13; index++){
       if(ranks[index]==rank)value=1+index;
   }
   for(index=0; index<4; index++){
       if(suits[index]==suit) value+= index*13;
   }
  
   return value;
}

void getPlayerCard(char *rank, char *suit){
   while(1){
       printf("Enter rank and suit: ");
       scanf(" %c%c",rank,suit);
       if(isRank(*rank) && isSuit(*suit)){
       return;
       }
       else
       printf("Invalid card. Please try again.\n");
      
   }
}

int main(){
  
   char playerOneRank,playerOneSuit;
   char playerTwoRank,playerTwoSuit;
   char playerThreeRank,playerThreeSuit;
   char playerFourRank,playerFourSuit;
  
   getPlayerCard(&playerOneRank,&playerOneSuit);
   getPlayerCard(&playerTwoRank,&playerTwoSuit);
   getPlayerCard(&playerThreeRank,&playerThreeSuit);
   getPlayerCard(&playerFourRank,&playerFourSuit);
  
   int one = cardValue(playerOneSuit,playerOneRank);
   int two = cardValue(playerTwoSuit,playerTwoRank);
   int three = cardValue(playerThreeSuit,playerThreeRank);
   int four = cardValue(playerFourSuit,playerFourRank);
  
   printf("Player One card value: %d\n",one);
   printf("Player Two card value: %d\n",two);
   printf("Player Three card value: %d\n",three);
   printf("Player Four card value: %d\n",four);
  
  
   if(one>=two && one>=three && one>=four){
       printf("Player 1 Wins\n");
   }
   if(two>=one && two>=three && two>=four){
       printf("Player 2 Wins\n");
   }
   if(three>=two && three>=one && three>=four){
       printf("Player 3 Wins\n");
   }
   if(four>=two && four>=three && four>=one){
       printf("Player 4 Wins\n");
   }
  
}

=========================================================================

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
JAVA Exercise_Pick a Card Write a program that simulates the action of picking of a card...
JAVA Exercise_Pick a Card Write a program that simulates the action of picking of a card from a deck of 52 cards. Your program will display the rank and suit of the card, such as “King of Diamonds” or “Ten of Clubs”. You will need: • To generate a random number between 0 and 51 • To define two String variables: a. The first String should be defined for the card Suit (“Spades”, “Hearts”, “Diamonds”, “Clubs”) b. The second String...
C programming question: Write a function char isValid (char c), return a with a char value...
C programming question: Write a function char isValid (char c), return a with a char value of 1 if it is a valid char, and return 0 if it is invalid. (DO NOT USE POINTER!) valid char are '1' '2' 3' '4' '5' 'A' 'B' 'C' for example: lower case is not valid '1' is valid 'A' is valid 'B' is valid 'ABC' is not valid 'D' is not valid '9' is not valid '1234' is not valid For Testing:...
(c programming) Given the function declaration and documentation below, write the function definition. Do not use...
(c programming) Given the function declaration and documentation below, write the function definition. Do not use the the standard library function fputc. /// @brief Writes a character to a specified file output stream. /// @param outstream A FILE* variable for an output stream to write a character to. /// @param outchar The character to be written to an output stream. /// @return On success, returns the written character. /// @note This function assumes #include <stdio.h> and a working, valid FILE*...
(c programming) Given the function declaration and documentation below, write the function definition. Do not use...
(c programming) Given the function declaration and documentation below, write the function definition. Do not use the standard library function fgetc. /// @brief Reads a character from specified file input stream. /// @param instream A FILE* variable for an input stream to read a character from. /// @return A character of type char from instream. /// @note This function assumes #include <stdio.h> and a working, valid FILE* variable to an input stream. char grabchar(FILE* instream);
For a C program hangman game: Create the function int setup_game [int setup_game ( Game *g,...
For a C program hangman game: Create the function int setup_game [int setup_game ( Game *g, char wordlist[][MAX_WORD_LENGTH], int numwords)] for a C program hangman game. (The existing code for other functions and the program is below, along with what the function needs to do) What int setup_game needs to do setup_game() does exactly what the name suggests. It sets up a new game of hangman. This means that it picks a random word from the supplied wordlist array and...
Programming in C language (not C++) Write a main function with a function call to a...
Programming in C language (not C++) Write a main function with a function call to a function called Calculation which has two integer arguments/ parameters. The function returns a character. Declare and initialize the necessary data variables and assign balues needed to make it executable and to prevent loss of information. //input 2 integer values //returns the characterequivalent of the higher of the two integer values char Calculation(int, int);
With the code given write python code that prints the probability of getting a flush when...
With the code given write python code that prints the probability of getting a flush when you run 10**5 trails. this is what i have so far but it says that isFlush is not defined. why? # Print out probability that a 5-card hand has all the same suit #seed(0) num_trials = 10**5 trials = [dealHand for k in range(num_trials)] # 5 card hand prob = sum([l for h in trials if isFlush(h)])/num_trials # sum the list of numbers that...
A. Write C code to create a structure called time_of_day, which stores the current time in...
A. Write C code to create a structure called time_of_day, which stores the current time in hours, minutes, and seconds. All the fields should be integers except for seconds, which should be a floating point value. B. Write a C function called check_time, which takes a pointer to a time_of_day structure as input, and return 1 if the time is valid (0 to 23 hours, 0 to 59 minutes, 0 to 59.999999 seconds) and 0 otherwise. Assume that times are...
Write a C program that prompts the user to enter a line of text on the...
Write a C program that prompts the user to enter a line of text on the keyboard then echoes the entire line. The program should continue echoing each line until the user responds to the prompt by not entering any text and hitting the return key. Your program should have two functions, writeStr and readLn, in addition to the main function. The text string itself should be stored in a char array in main. Both functions should operate on NUL-terminated...
Write a program that reads a string and outputs the number of lowercase vowels in the...
Write a program that reads a string and outputs the number of lowercase vowels in the string. Your program must contain a function with a parameter of a char variable that returns an int. The function will return a 1 if the char being passed in is a lowercase vowel, and a 0 for any other character. The output for your main program should be: There are XXXX lowercase vowels in string yyyyyyyyyyyyyyyyyyyyyy Where XXXX is the count of lowercase...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT