Question

C++ program

This program involves writing a VERY simplified version of the card game War. You may know this game or not but my rules are these 1. Split the deck between player1 and player2. Only the face values matter (2-14) and not the suits 2. Each player puts a card down on the table. The higher face value wins that hand. If the card values match, you will simply indicate tie and neither player wins.The original rules would require you to use a queue, linked list, or dynamic array so that the winning player keeps the cards from the hand. lve cut that part out of this game. Just play each players 26 card deck 1 time and tally the results You can use a 2D array or 2 1D arrays to hold the 52 card deck. No need to store suits. You will want to place the face values (4 each of 2-14) into one array and a boolean or other appropriate value in the other array to make sure you dont draw the same card twice. Your players will each have their own array of 26 cards -The cards MUST be randomly given to the players so that their order within each players deck is never the same. Just generate a random number between O and 51. If the value in the deck has already been given out, generate another random number and try again. How do you know if it has been given out? Thats what the second array (2 x 1D arrays) or second row (in a 2D array) is used for -The player with the most hand wins is the game winner. If there is a tie, indicate that the game is a tie -You will output the game to the screen in this way: Player1 Player2 Winner 12 3 7 13 etc.. Game Winner is Player1! Player1 Tie Player2 Player1 14 2

0 0
Add a comment Improve this question Transcribed image text
Answer #1

//Code to copy
//main.cpp
#include<iostream>
#include<string>
#include<math.h>
#include<iomanip>
#include<fstream>
using namespace std;

int main()
{
   srand(time(0));
   const int CARDS=26;
   const int size=2;
   const int MIN=2;
   const int MAX=14;
   int i=0;
   int score1=0,score2=0;
   int player1card=0;
   int player2card=0;

   //create two 2D arrays
   //index 0 represents player score
   //index 1 represents the player drawn status
   int hand1[CARDS][size];
   int hand2[CARDS][size];


   int player1Wins=0;
   int player2Wins=0;

   //set 0 indicate that all cards are not drawn
   for(i=0;i<CARDS;i++)
   {
       hand1[i][1]=0;
       hand2[i][1]=0;
   }


   cout<<setw(10)<<"Player1"
       <<setw(10)<<"Player2"
       <<setw(10)<<"Winner"<<endl;

   for(i=0;i<CARDS;i++)
   {
       score1=rand()%(MAX-MIN)+MIN;
       score2=rand()%(MAX-MIN)+MIN;

       //check card is new
       do
       {
           player1card=rand()%(26);
       }while(hand1[player1card][1]!=0);

       //set score1
       hand1[player1card][0]=score1;
       hand1[player1card][1]=1;

       //check card is new
       do
       {
           player2card=rand()%(26);
       }while(hand2[player2card][1]!=0);
      
       //set score2
       hand2[player2card][0]=score2;
       hand2[player2card][1]=1;

   }


   //print winners on console
   for(i=0;i<CARDS;i++)
   {
       cout<<setw(10)<<hand1[i][0]
       <<setw(10)<<hand2[i][0];

       if(hand1[i][0]>hand2[i][0])
       {
           player1Wins++;
           cout<<setw(10)<<"Player1"<<endl;
       }
       else if(hand1[i][0]<hand2[i][0])
       {
           player2Wins++;
           cout<<setw(10)<<"Player2"<<endl;
       }
       else
           cout<<setw(10)<<"Tie"<<endl;
   }

   cout<<"Game Winner is ";
   (player1Wins>player2Wins)?cout<<"Player1!"<<endl:cout<<"Player2!"<<endl;

   system("pause");
   return 0;
}

Sample Output:

Player Player2 9 Winner Tie 6 Player2 3 Player2 4 Player1 8 Player1 2 Player1 12 Player2 Tie 5 Player2 9 Player2 13 Player2 1

Add a comment
Know the answer?
Add Answer to:
C++ program This program involves writing a VERY simplified version of the card game War. You...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • War—A Card game Playing cards are used in many computer games, including versions of such classic...

    War—A Card game Playing cards are used in many computer games, including versions of such classics as solitaire, hearts, and poker. War:    Deal two Cards—one for the computer and one for the player—and determine the higher card, then display a message indicating whether the cards are equal, the computer won, or the player won. (Playing cards are considered equal when they have the same value, no matter what their suit is.) For this game, assume the Ace (value 1) is...

  • Please to indent and follow structure!!!!! Assignment 3 - The card game: War Due Date: June...

    Please to indent and follow structure!!!!! Assignment 3 - The card game: War Due Date: June 9th, 2018 @ 23:55 Percentage overall grade: 5% Penalties: No late assignments allowed Maximum Marks: 10 Pedagogical Goal: Refresher of Python and hands-on experience with algorithm coding, input validation, exceptions, file reading, Queues, and data structures with encapsulation. The card game War is a card game that is played with a deck of 52 cards. The goal is to be the first player to...

  • I need to build the card game of War in C++. It will be a 2...

    I need to build the card game of War in C++. It will be a 2 player game. Each player will have their own deck of 52 cards. 2-10, Jack=11, Queen=12, King=13, Ace=14. Each player will draw one card from their deck. The player with the higher card wins both cards. If the card drawn is the same, then each player will draw 3 cards and on the 4th card drawn will show it. The player that shows the higher...

  • Program 4: C++ The Game of War The game of war is a card game played by children and budding comp...

    Program 4: C++ The Game of War The game of war is a card game played by children and budding computer scientists. From Wikipedia: The objective of the game is to win all cards [Source: Wikipedia]. There are different interpretations on how to play The Game of War, so we will specify our SMU rules below: 1) 52 cards are shuffled and split evenly amongst two players (26 each) a. The 26 cards are placed into a “to play” pile...

  • Complete a program In C#: some code is included, you edit the areas that have not...

    Complete a program In C#: some code is included, you edit the areas that have not been filled. For your C# program you will complete code that plays the War card game. In this game, the deck of cards is evenly divided among two players. The players are not allowed to look at the cards in their hand. On each round of the game, both players lay down the top card from their hand. The player with the higher value...

  • Create a simplified Blackjack game using the Deck and Card classes (download the attached files to...

    Create a simplified Blackjack game using the Deck and Card classes (download the attached files to start).  There is also a "TestCard" class that creates a Deck and runs some simple code. You may choose to use this file to help you get started with your game program. 1) Deal 2 cards to the "player" and 2 cards to the "dealer".  Print both the player's cards, print one of the dealer's cards. Print the total value of the player's hand. 2) Ask...

  • I am playing a simplified version of the card game, "War," with my brother. We are...

    I am playing a simplified version of the card game, "War," with my brother. We are playing with a subset of the deck, all of the numbered cards 2 through 10 (2:10). There are 4 cards of each number, resulting in 36 total cards in a single pile. First, I turn over a card. Next, he turns over a card. Whoever has the higher card wins and these "used" cards are discarded. If we turn over the same card, we...

  • C only (not C++) Write a C program that simulates a game. There are two players...

    C only (not C++) Write a C program that simulates a game. There are two players (called Player1 and Player2). At the start of the game, each player chooses a number from 1 to 10 (cannot be the same). Then, the program will generate a random number from 1 to 10. The game ends when either Player1 or Player2 hits the number generated by the program. Display the winning message for the winner. Example output: Player 1: 6 Player 2:...

  • 4. In one version of the game played by a dealer and a player, 2 cards...

    4. In one version of the game played by a dealer and a player, 2 cards of a standard 52-card bridge deck are dealt to the player and 2 cards to the dealer. For this exercise, assume that drawing an ace and a face card (Jack, Queen and King for each shape) is called blackjack. If the dealer does not draw a blackjack and the player does, the player wins. If both the dealer and player draw blackjack, a tie...

  • Suppose, we have 3 players and they are playing a card game. In the card game,...

    Suppose, we have 3 players and they are playing a card game. In the card game, the deck contains 10 cards where there are 3 kings, 3 aces, and 4 jacks. Each player gets a card from the deck randomly. The winner of the game is determined based on the strength of the card where a king has 10 strength, an ace has 15 strength and a jack has 20 strength. Your task is to write a function called playgame()...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT