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;
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");
}
}
=========================================================================
Get Answers For Free
Most questions answered within 1 hours.