In: Computer Science
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 determines if the given character is one of the ranks mentioned above. It should return a char with a value of 1 if the character is a valid rank and 0 otherwise. Lowercase letters are not valid.
YouMUSTwriteafunctioncalledisValidSuit(charc)whichdeterminesifthegivencharacter is one of the suits mentioned above. It should return a char with a value of 1 if the character is a valid suit and 0 otherwise. Lowercase letters are not valid.
You MUST have a function called getTrump() that returns a char. It should prompt the user for a trump suit, which must be 'H', 'D', 'S' or 'C'. It should be robust, in that any invalid input is not accepted. It should only return from the function when a valid suit has been entered, and it must make use of the isValidRank() function. For any invalid entry, an appropriate error message should be given. Blank entries are invalid and so are lowercase letters.
The main function should first get the trump suit, by calling the above function. It should then enter an infinite loop to do the following: (1) ask for 4 cards from the user, (2) display the 4 cards entered, (3) determine and display which player wins the round (i.e., which one has the “best” card). These steps will be explained below.
6. Whenenteringthecards...yourcode should be robust and handle any input, just like you did in thegetTrump() function. For each of the 4 cards entered, your code should allow the user to enter two characters and then press enter. If the first character (i.e., the rank) is invalid (use the function you wrote earlier), then an appropriate error message should be displayed and the second character (i.e., the suit) should not be prompted for. If it was valid, then the suit character should be prompted for.
Player 1: Enter card
rank andRC
Invalid card, please re-enter Player 1: Enter card rank and4F
Invalid card, please
re-enter Player 1: Enter card rank and6
Invalid card, please re-enter Player 1: Enter card rank andH
Invalid card, please
re-enter Player 1: Enter card rank andJC
Player 2: Enter card rank and6S
Player 3: Enter card rank and
suit (e.g., 2S, TC, KD)
suit (e.g., 2S, TC, KD)
suit (e.g., 2S, TC, KD)
suit (e.g., 2S, TC, KD)
suit (e.g., 2S, TC, KD) suit (e.g., 2S, TC, KD) suit (e.g., 2S, TC, KD)
suit (e.g., 2S, TC, KD)
suit (e.g., 2S, TC, KD) suit (e.g., 2S, TC, KD)
is invalid, another
error message
should be shown. Either way, the
code should keep prompting until a
valid card is entered before moving on
to get the next player’s card. Here is
an example of what you should do à 5H
If it
Invalid card, please
re-enter Player 3: Enter card rank and9d
Invalid card, please re-enter Player 3: Enter card rank and9D
Player 4: Enter card rank and
Once4validcardentrieshavebeenentered,the4cardsshouldbedisplayedlikethis:
JC, 6S, 9D, 5H
You must then determine which card wins the round. That is, which player has the best card. To do this, you must follow these rules:
A card which is of the trump suit always beats a card that is a non-trump suit.
If two cards have the same suit, the one with the higher rank is better. 'A' is the highest
rank and '2' is the lowest.
The card played by player 1 is called the “suit led”. If no other player has a higher ranking card of the same suit as the suit led, and no other player has the trump suit, then player 1 has the best card and wins.
Inyourmainfunction,iftherankofthefirst(orany)playerisa'.'character,thentheprogram should quit. The TA’s will need this functionality in order to test your program.
Show transcribed image text
View comments (1)
The following code has been commented and explained
it has been run successfully on codeBlocks IDE:-
if this solution helps you, please upvote, and if you have any doubt anywhere in this code, please ask through the comments
****************************************************************************************************
#include <stdio.h>
#include <stdlib.h>
char isValidRank(char rank)
{
if((rank <= '9' && rank>='2') || rank == 'A' || rank
== 'T' || rank == 'J' || rank == 'Q' || rank == 'K')
return '1';
else
return '0';
}
char isValidSuit(char suit)
{
if(suit == 'H' || suit == 'D' || suit == 'S' || suit =='C')
return '1';
else if(suit == ' ')
return '0';
else
return '0';
}
char getTrump()
{
printf("Hello user, please enter the trump suit\n");
char suit;
scanf("%c",&suit);
while( isValidSuit(suit) == '0')
{
printf("invalid suit entered.(valid suits are: H,D,S or
C)\n");
printf("please enter a valid suit\n");
scanf(" %c",&suit); // notice that the space before%c helps us
consume \n when user presses enter
}
return suit;
}
int bigger(char a, char b) //this function returns 1 if a is
bigger than b
//else return 0
{
if((a<='9' && a>='0') &&( b<='9'
&& b>='0') )
{
if(a>b)
return 1;
else
return 0;
}
else if((a<='9' && a>='0'))
return 0;
else{
if(a=='A' && b!= 'A')
return 1;
else if( a == 'K' && b != 'A' && b!= 'K')
return 1;
else if( a == 'Q' && b != 'A' && b!= 'K' &&
b!='Q')
return 1;
else if (a=='J' && b != 'A' && b!= 'K' &&
b!='Q' && b!= 'J')
return 1;
else if (a == 'T' && b != 'A' && b!= 'K' &&
b!='Q' && b!= 'J' && b != 'T')
return 1;
else
return 0;
}
}
int main()
{
char rank,suit,trump,suitLed; //trump stores the trump suit, suit n
rank are used to take input
//of suit and rank, suitLed is used to store the suit of first
player
int trumpIndicator = 0; // this variable indicates whether or not
we found the trump card
int winner = -1; //initialising winner to -1
char winningRank = '0'; // winnimgRank holds the rank of the winner
so far
char winningSuit = 'Z'; //wining suit holds the suit of the winner
so far.
char ranks[4]; //ranks and suits arrays to store all the ranks and
suits
char suits[4];
trump = getTrump(); // storing the trump suit in trump
int id =1; // player id
int i = 0; //for iterating loops
while(1) // this is always true and hence loops infinitely
{
for(id = 1; id <=4; id++){ // loop 4 times to get each players
input
printf("player %d: enter card rank ",id);
scanf(" %c",&rank);
if(rank == 'a'){
printf("\n exiting the game\n");
return 0;
}
while(isValidRank(rank)== '0'){
printf("invalid card : please re-enter rank \nplayer %d:
",id);
scanf(" %c",&rank);
}
ranks[id-1] = rank;
printf("player %d: enter card suit ",id);
scanf(" %c",&suit);
while(isValidSuit(suit) == '0')
{
printf("invalid card : please re-enter suit\nplayer %d:
",id);
scanf(" %c",&suit);
}
suits[id - 1] = suit;
if(id==1)
suitLed = suit;
}
for(i=0;i<4;i++)
{
printf("%c%c ",ranks[i],suits[i]);
}
for(i = 0;i<4;i++)
{
if(suits[i] == trump )
{
if( winningSuit != trump ) // ie no trump card has been seen so
far
// so this card is winning so far
{
winner = i+1;
winningRank = ranks[i];
winningSuit=trump;
}
else{ // we have already seen a trump card so we just need
//to see which of them has the bigger rank
if(bigger(ranks[i],winningRank)){
winner = i+1;
winningRank = ranks[i];
winningSuit = trump;
}
}
trumpIndicator = 1; // tells that we have a trump card
}
}
if(trumpIndicator == 0) // if we have not seen any trump, then only
this is entered
{ winner = 1; //player 1 is initialised as winner.
winningRank = ranks[0];
winningSuit = suitLed;
for(i=1;i<4;i++)
{
if(suits[i] == suitLed &&
bigger(ranks[i],winningRank))
//another card of suitLed bigger than player1
{
winner = i+1;
winningRank=ranks[i];
}
}
}
printf("\nplayer %d has won \n",winner);
trumpIndicator = 0;
winner = -1;
winningRank = '0';
winningSuit = 'Z';
}
}
****************************************************************************************************************
please upvote if it helped.