Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

A program named that simulates the \"war\" card game. For the computer player: g

ID: 3767109 • Letter: A

Question

A program named that simulates the "war" card game.
For the computer player:
generate a random number from 2-14 to represent a card value for the computer
Output the computer's card's value, outputting 2-10 as that value, but output 11 as Jack, 12 as Queen, 13 as King, and 14 as Ace
generate a random number from 0-3 to represent a card's suit for the computer
Output the computer's card's suit, but output 0 as Spades, 1 as Diamonds, 2 as Hearts, and 3 as Clubs
For the human player:
generate a random number from 2-14 to represent a card value for the user
Output the user's card's value, outputting 2-10 as that value, but output 11 as Jack, 12 as Queen, 13 as King, and 14 as Ace
generate a random number from 0-3 to represent a card's suit for the user
Output the user's card's suit, but output 0 as Spades, 1 as Diamonds, 2 as Hearts, and 3 as Clubs
Determine the winner:
If the computer's card's value is LESS than the human's card's value, output "Human wins!"
If the computer's card's value is EQUAL TO the human's card's value, output "It's a tie."
If the computer's card's value is GREATER than the human's card's value, output "Computer wins!"

For example:
Computer's card is a Jack of Spades
Human's card is a 10 of Clubs
Computer wins!

Replay if there is a tie and deal 2 new cards, one for the computer and one for the user.
Keep track of the score: number of computer wins and number of user wins.

Use a structure specification named PlayingCard, with two whole number data members named suit
and value. Create two PlayingCard records, one for the human player
and one for the computer player.
Write a void function to take a PlayingCard object as a parameter, and output its value and suit, like this: Jack of
Clubs.
Write a value-returning function named getYesOrNo, to prompt the user for the "Continue? [Y/N]"
input. It needs no parameters. It should include the validation loop, moved into it from main. It can
return either an int, char, or bool -- your choice.

For example:
Computer's card is a 2 of Diamonds
Human's card is a 8 of Hearts
--<< Human wins! >>--
Computer wins: 0, Human wins: 1

Continue? [Y/N] x
Continue? [Y/N] 100
Continue? [Y/N] y

Computer's card is a 2 of Spades
Human's card is a Ace of Hearts
--<< Human wins! >>--
Computer wins: 0, Human wins: 2

Continue? [Y/N] y

Computer's card is a Queen of Diamonds
Human's card is a 7 of Diamonds
--<< Computer wins! >>--
Computer wins: 1, Human wins: 2

Continue? [Y/N] Q
Continue? [Y/N] n

Explanation / Answer

#include <iostream>
#include <time.h>
#include <stdio.h>
#include <stdlib.h>

using namespace std;

typedef struct PC
{
   int value,suit;
}PlayingCard;

void printPC(PlayingCard obj)
{
   if(obj.value>=2 && obj.value<=10)
       cout<<obj.value<<" of ";
   else if(obj.value == 11)
       cout<<"Jack of ";
   else if(obj.value == 12)
       cout<<"Queen of ";
   else if(obj.value == 13)
       cout<<"King of ";
   else if(obj.value == 14)
       cout<<"Ace of ";
   else
       cout<<"Wrong card";

   switch(obj.suit)
   {
       case 0: cout<<"Spades ";break;
       case 1: cout<<"Diamonds ";break;
       case 2: cout<<"Hearts ";break;
       case 3: cout<<"Clubs ";break;
       default: "Wrong card ";

   }
}

int getYesorNo()
{
   char choice;
   cout<<"Do you want to continue? (Y/N) ";
   cin>>choice;
   if(choice=='y'||choice=='Y')
       return 1;
   else if(choice=='n'||choice=='N')
       return 0;
   else return getYesorNo();
}

PlayingCard generatecard()
{
   PlayingCard temp;
   temp.value = rand()%13+2;
   temp.suit = rand()%4;
   return temp;
}

int main()
{
   srand(time(NULL));
   int score_computer = 0,score_human = 0;
   PlayingCard Computer,Human;

   do
   {
       Computer = generatecard();
       cout<<"Computer's card is ";
       printPC(Computer);
       cout<<"Human's card is ";
       Human = generatecard();
       printPC(Human);
       if(Computer.value<Human.value)
           {
               cout<<"Human wins!"<<endl;
               score_human++;
           }
       else if(Computer.value>Human.value)
           {
               cout<<"Computer wins!"<<endl;
               score_computer++;
           }
       else
           continue;

   }while(getYesorNo());
   cout<<"Score of Computer "<<score_computer<<endl;
   cout<<"Score of human "<<score_human<<endl;
return 0;   
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote