Question

In: Computer Science

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)

Solutions

Expert Solution

Please find the c program for the above problem:

#include<stdio.h>

//global variables to hold the value set by both the functions
char rankout, suitout;
int isRankthere, isSuitthere;

//declaration of both the methods
void isRank(char c);
void isSuit(char c);

int main()
{
   char rank, suit;
   printf("Make sure you enter the Rank and Suit in capital letters only..\n");
   printf("Enter the rank of the card\n");
   scanf("%c",&rank);
   getchar();
   printf("Enter the suit of the card\n");
   scanf("%c",&suit);
   getchar();
  
   // Calling both the methods
   isRank(rank);
   isSuit(suit);
  
   // Checking if the isRank method has returned true or false(0 or 1)
   if(isRankthere == 1)
   {
       printf("The rank is : %c\n",rankout);
   }
   else{
       printf("Rank is invalid\n");
   }
  
   // Checking if the isSuit method has returned true or false(0 or 1)
   if(isSuitthere == 1)
   {
       printf("The suit is : %c",suitout);
   }
   else{
       printf("Suit is invalid");
   }
}

//Method to check whether the rank exists or not
void isRank(char c)
{
   int i;
   char ranks[] = {'2','3','4','5','6','7','8','9','T','J','Q','K','A','\0'};
   for(i=0; i<sizeof(ranks); i++)
   {
       if(c == ranks[i]){
           isRankthere = 1;
           rankout = ranks[i];
       }
   }
}

//Method to check whether the suit exists or not
void isSuit(char c)
{
   int i;
   char suits[] = {'H','C','S','D','\0'};
   for(i=0; i<sizeof(suits); i++)
   {
       if(c == suits[i]){
           isSuitthere = 1;
           suitout = suits[i];
       }
   }
}

*The explanation has been given in comments.

Thanks!



Related Solutions

Write a C program called cards.c that simulates some card game logic by comparing the cards...
Write a C program called cards.c that simulates some card game logic by comparing the cards from 4 people and determining which person has the best card. The program MUST work as follows: Eachcardmustberepresentedbyexactlytwocharsrepresenting a rank and a suit. The possible rank options are: '2', '3', '4', '5', '6', '7', '8', '9', 'T', 'J', 'Q', 'K', 'A'. The possiblesuit options are: 'H', 'D', 'S', 'C'. So 6H represents the “6 of hearts”, JC represents the “Jack of Clubs” etc... YouMUSTwriteafunctioncalledisValidRank(charc)which...
Write a program in C language that implements the logic of Binary Trees with the following...
Write a program in C language that implements the logic of Binary Trees with the following requirements: 1- Elements of the tree should be read from the user. 2- Keep the binary tree heigh-balanced that is the absloute value of ((hight of left sub-tree) - ( height of right sub-tree)) should not be greater than 1. If so, then the binary tree is no longer balanced. Hint: The best approach is to maintain balance during insertion. *Remember, we are talking...
IN C++ Write a program to play the Card Guessing Game. Your program must give the...
IN C++ Write a program to play the Card Guessing Game. Your program must give the user the following choices: - Guess only the face value of the card. -Guess only the suit of the card. -Guess both the face value and suit of the card. Before the start of the game, create a deck of cards. Before each guess, use the function random_shuffle to randomly shuffle the deck.
In C++, Write the following program using a vector: A common game is the lottery card....
In C++, Write the following program using a vector: A common game is the lottery card. The card has numbered spots of which a certain number are selected at random. Write a Lotto() function that takes two arguments. The first should be the number of spots on a lottery card and the second should be the number of spots selected at random. The function should return a vector object that contains, in sorted order, the numbers selected at random. Use...
C LANGUAGE ONLY Write a C program to count the frequency of each element in an...
C LANGUAGE ONLY Write a C program to count the frequency of each element in an array. Enter the number of elements to be stored in the array: 3 Input 3 elements of the array: element [0]: 25 element [1]: 12 element [2]: 43 Expected output: The frequency of all elements of an array: 25 occurs 1 times 12 occurs 1 times 3 occurs 1 times
ONLY IN C LANGUAGE Write a C program to print all the unique elements of an...
ONLY IN C LANGUAGE Write a C program to print all the unique elements of an array. Print all unique elements of an array Enter the number of elements to be stored in the array: 4 Input 4 elements in the arrangement: element [0]: 3 element [1]: 2 element [2]: 2 element [3]: 5 Expected output: The only items found in the array are: 3 5
Programming Language: C++ Overview For this assignment, write a program that will simulate a single game...
Programming Language: C++ Overview For this assignment, write a program that will simulate a single game of Craps. Craps is a game of chance where a player (the shooter) will roll 2 six-sided dice. The sum of the dice will determine whether the player (and anyone that has placed a bet) wins immediately, loses immediately, or if the game continues. If the sum of the first roll of the dice is equal to 7 or 11, the player wins immediately....
Can you write a program for the card game WAR using linked lists in c++!
Can you write a program for the card game WAR using linked lists in c++!
C LANGUAGE ONLY Write a C program to count the total number of duplicate elements in...
C LANGUAGE ONLY Write a C program to count the total number of duplicate elements in an array. Enter the number of elements to be stored in the array: 3 Input 3 elements in the arrangement: element [0]: 5 element [1]: 1 element [2]: 1 Expected output: The total number of duplicate elements found in the array is: 1
Using c# programming language Write a program that mimics a lottery game. Have the user enter...
Using c# programming language Write a program that mimics a lottery game. Have the user enter 3 distinct numbers between 1 and 10 and match them with 3 distinct, randomly generated numbers between 1 and 10. If all the numbers match, then the user will earn $10, if 2 matches are recorded then the user will win $3, else the user will lose $5. Keep tab of the user earnings for, let say 5 rounds. The user will start with...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT