In: Computer Science
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)
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!