Question

In: Computer Science

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:

  1. 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...

  2. 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.

  3. 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.

  4. 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.

  5. 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

  1. Once4validcardentrieshavebeenentered,the4cardsshouldbedisplayedlikethis:

    JC, 6S, 9D, 5H

  2. 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.

  3. 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)

Solutions

Expert Solution

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.


Related Solutions

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...
Write a function called play_round that simulates two people drawing cards and comparing their values. High...
Write a function called play_round that simulates two people drawing cards and comparing their values. High card wins. In the case of a tie, draw more cards. Repeat until someone wins the round. the function has two parameters: the name of player 1 and the name of player 2. it returns a string with format ' wins!'. For instance, if the winning player is named Rocket, return 'Rocket wins!'. python
using matlab Write a script that simulates a card game that works as follows: A dealer...
using matlab Write a script that simulates a card game that works as follows: A dealer places 5 cards face down on the table and flips the first card. The player goes down the line, one at a time, and guesses if the next card is higher or lower than the card displayed, and then the next card is revealed. In the end, the player is awarded a point for each correct guess. In terms of coding, your script should...
Write a python program that simulates a simple dice gambling game. The game is played as...
Write a python program that simulates a simple dice gambling game. The game is played as follows: Roll a six sided die. If you roll a 1, 2 or a 3, the game is over. If you roll a 4, 5, or 6, you win that many dollars ($4, $5, or $6), and then roll again. With each additional roll, you have the chance to win more money, or you might roll a game-ending 1, 2, or 3, at which...
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...
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++!
Objective: Write a program which simulates a hot potato game. In this version of a classic...
Objective: Write a program which simulates a hot potato game. In this version of a classic game, two or more players compete to see who can hold onto a potato the longest without getting caught. First the potato is assigned a random value greater than one second and less than three minutes both inclusive. This time is the total amount of time the potato may be held in each round. Next players are put into a circular list. Then each...
In C++  Write a program that simulates coin tossing. For each toss of the coin the program...
In C++  Write a program that simulates coin tossing. For each toss of the coin the program should print heads or tails. Let the program toss the coin 100 times and count the number times each side of the coin appears. Print the results. 0 represents tails and 1 for heads.
Magic the Gathering is a popular card game. Cards can be land cards, or other cards....
Magic the Gathering is a popular card game. Cards can be land cards, or other cards. We consider a game with two players. Each player has a deck of 40 cards. Each player shuffles their deck, then deals seven cards, called their hand. (a) Assume that player one has 10 land cards in their deck and player two has 20. With what probability will each player have four lands in their hand? (b) Assume that player one has 10 land...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT