Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

For your program you will complete code that plays the War card game. In this game, the deck of cards is evenly divided among two

For your 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 card wins both cards and places the cards on the bottom of his hand. For our version of the game, in the case of a tie, each player places their card back at the bottom of their hand (the real game has slightly different rules for ties). Game play continues until one of the players has all of the cards. In order to simplify our program, our "deck" is a series of random numbers ranging from 1 to 13 (because a real deck of cards has 13 values). The size of the deck is set by a constant in the warGame.cpp file. This game can take a long time to play, so a deck size of 10 seems reasonable for testing the game. You can change the deck size if you wish. I have written the main function for you: warGame.cpp. You should not make any modifications to this file (other than altering the constant for the deck size). You will modify the code in the warGame.h file. I have provided the function to print the results of each round of the game. I have also provided the function to create the "deck" of cards. You will write two functions, described below. Remember to write small parts of the program at a time and test each part before moving on to the next part. Function 1: Deal the Cards. This function takes the deck of cards and splits it between the two players by alternately dealing a card to each player. Note that the array for each player's hand is the same size of the deck. This is because we need to be able to store all cards in the winner's hand. Therefore, the array for each player's hand is a partially filled array. In the main function I initialized all elements of the players' hand arrays to zero. In our game, when an array element has the value zero it indicates that there is no card at that index. All of the cards must be placed at the beginning of the array. An example of a deck dealt to two players is shown below. Deck: 3 1 13 5 8 4 3 10 6 9 Player 1's Hand: 3 13 8 3 6 0 0 0 0 0 Player 2's Hand: 1 5 4 10 9 0 0 0 0 0 Function 2: Shift the Cards Forward. This function moves all cards in a hand one place forward, removing the first card and setting the value of the last card to zero. This simulates a player removing a card from her hand. An example of a hand of cards before and after shifting is shown below. Before shift: 3 13 8 3 6 0 0 0 0 0 After shift: 13 8 3 6 0 0 0 0 0 0 Note: I have made a design decision to write my functions to be very generic. I named the functions based on the task they perform, not based on the fact that we are using them for a card game. For example, I call the function that deals the cards "splitArray" not "dealCards". I have also given the function parameters generic names. This will make it easier for us to reuse these functions in another program without needing to make modifications to the functions. image text in transcribed?(MAIN FUNCTION)

#include "warGame.h"

const int SIZE = 10;

int main() { int deck[SIZE]; // The deck of cards int hand_one[SIZE] = {0}; // A hand of cards, with all elements initialized to zero int hand_two[SIZE] = {0}; // A hand of cards, with all elements initialized to zero int tmp1; // Temporary variable to hold a card int tmp2; // Temporary variable to hold a card int size1 = SIZE / 2; // Number of cards in hand 1 int size2 = SIZE / 2; // Number of cards in hand 2 string tmpStr; // Used for asking the user to press enter to continue

// Seed the random number generator srand(static_cast(time(NULL))); // Get a deck of numbers (to represent cards) // Because we are simulating a deck of cards, we will get random numbers // in the range of 1 to 13. getNums(deck, 13, SIZE); // Deal the deck between the two players splitArray(hand_one, hand_two, deck, SIZE);

//loop until one player has no more cards. while( size1 != 0 && size2 != 0 ) { // Show the card at the front of each players hand showCard(hand_one, hand_two, size1, size2); // Store the front cards into temporary variables tmp1 = hand_one[0]; tmp2 = hand_two[0]; // Shift the cards forward in each hand shift(hand_one, size1); shift(hand_two, size2); // If the first player has the higher card if ( tmp1 > tmp2 ) { cout

// We are going to add one card to Player 1's hand and // remove a card from Player 2's hand. So increase the size // of the Player 1 array and decrease the size of the // Player 2 array by one. size1++; size2--; //add both cards to the end of Player 1's hand hand_one[size1 - 2] = tmp1; hand_one[size1 - 1] = tmp2; }

// If the computer has the higher card else if ( tmp2 > tmp1 ) { cout

// Pause the game at the end of each turn cout

// Output the winner of the game if(size1 == 0) { cout

return 0; } (NEXT ARE THE FUNCTIONS NEEDED TO BE COMPLETED)

using namespace std;

// // Generate random numbers in the range between one and the given range // to fill an array // // DO NOT MODIFY THIS FUNCTION // void getNums(int arr[], int range, int size) { int i; // A counter for( i = 0; i

// // Display the cards each player pulled and the number of cards they have left // // DO NOT MODIFY THIS FUNCTION // void showCard(int hand_one[], int hand_two[], int one, int two) { cout

// // Split the elements of a source array into two destination arrays // The given size is the size of the destination arrays (they are the same size) // // WRITE CODE TO COMPLETE THIS FUNCTION // void splitArray(int dest_one[], int dest_two[], int source[], int size) { int ctr = 0; // Counter that will go through the source[] array. int i; // A counter

// Loop through the destination arrays. // Assign each element in a destination array the next element // in the source array. Use the ctr variable to keep track of the // current position in the source array. }

// // Shifts the items in an array one place to the left // Throws out the first element in the array and sets the last element // in the array to zero. // // WRITE CODE TO COMPLETE THIS FUNCTION // void shift(int arr[], int size) { int i; // A counter // shift the array

// Set the last element in the array to zero

} complete functions to have program working exactly like output!! thanks!!

C:proj2015_rlReleasel proj2015_r.exe Number of cards in each player's deck**** ou: 5 Computer: 5 Youhave the card: 4 The computer has the card: 1 You have the higher card? Press return to continue. Number of cards in each player' s deck**** ou: 6 Computer: 4 Youhave the card: 1 The computer has the card: 2 You have the higher card? Press return to continue. Number of cards in each player' s deck**** ou: 7 Computer: 3 Youhave the card 11 The computer has the card: 5 You have the higher card? Press return to continue. Number of cards in each player' s deck**** ou: 8 Computer: 2 You have the card 12 The computer has the card: 6 You have the higher card? Press return to continue. Number of cards in each player' s deck**** ou: 9 Computer: 1 You have the card 5 The computer has the card: 1 You have the higher card? Press return to continue. You win the game! Press any key to continue . . . C:proj2015_rlReleasel proj2015_r.exe Number of cards in each player's deck**** ou: 5 Computer: 5 Youhave the card: 4 The computer has the card: 1 You have the higher card? Press return to continue. Number of cards in each player' s deck**** ou: 6 Computer: 4 Youhave the card: 1 The computer has the card: 2 You have the higher card? Press return to continue. Number of cards in each player' s deck**** ou: 7 Computer: 3 Youhave the card 11 The computer has the card: 5 You have the higher card? Press return to continue. Number of cards in each player' s deck**** ou: 8 Computer: 2 You have the card 12 The computer has the card: 6 You have the higher card? Press return to continue. Number of cards in each player' s deck**** ou: 9 Computer: 1 You have the card 5 The computer has the card: 1 You have the higher card? Press return to continue. You win the game! Press any key to continue

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Data Management Databases And Organizations

Authors: Richard T. Watson

2nd Edition

0471180742, 978-0471180746

More Books

Students also viewed these Databases questions

Question

Connect with your audience

Answered: 1 week ago