I dont even know where to start with this program. I have been really busy and he tells us what to do. If anyone could help with this it would be great. here is what it says to do
ASSIGNMENT DESCRIPTION
This program will simulate the card game WAR!.
The following is quoted verbatim from a Wikipedia page:
The deck is divided evenly among the two players, giving each a face-down stack. In unison, each player reveals the top card on his stack (a "battle"), and the player with the higher card takes both the cards played and moves them to the bottom of his stack. If the two cards played are of equal value, each player lays down three face-down cards and a fourth card face-up (a "war"), and the higher-valued card wins all of the cards on the table, which are then added to the bottom of the player's stack. In the case of another tie, the war process is repeated until there is no tie.
A player wins by collecting all the cards. If a player runs out of cards while dealing the face-down cards of a war, he may play the last card in his deck as his face-up card and still have a chance to stay in the game.
ASSIGNMENT SPECIFICATIONS
Arrays will be required in this assignment -- it would be nearly impossible to make a readable program without them.
Arrays may serve in various different ways:
* A deck of cards may be represented as 52 distinct values in an array. The precise meaning and interpretation of each value is not specified in this assignment, but the 52 cards should be distinguishable. Rank is necessary for the game; suit is not, but may provide some color.
The values representing the cards could simply be integers. 0 to 12 (or 1 to 13) could represent 13 ranks of cards; or 0 to 51 could represent 52 diffderent possible cards.
* Each player has a stack, which would best be represented by an array.
* Battle piles representing the cards currently in competition, can also be represented as arrays -- especially in the cases of a war where the winner is not yet known. Those cards would have no known owner until the battle is resolved.
* Arrays may be assist in interpreting values in user-friendly ways -- e.g. translating some integer subscript to the name of a rank "King" or "Two". The subscript itself would be obtained from the numeric value used to stand for the card (as in the first bullet point above).
It should be noted that large segments of duplicated code that only differ in variable names strongly suggest a need for arrays.
SIMULATING A PLAYER DECK
Here are three feasible ways of handling player decks. The method chosen will affect the parameter list required to use them.
I: The simplest
0 1 2 3 49 50 51
+---+---+---+---+-- --+---+---+-- --+---+---+---+
| X | X | X | X | .... | X | | .... | | | |
+---+---+---+---+-- --+---+---+-- --+---+---+---+
size-1 size
Each deck is stored in an array (maximum 52 cards)
with the top card in position 0. Adding a card just
goes to the place indicated by size (which increases).
When a card is drawn, the rest are shifted one position
(1 to 0, 2 to 1, etc.)
Primary Advantage: simple to understand, only one array required
Primary Drawback: a lot of card shifting whenever a card is drawn
II: Maintaining a separate discard pile
0 1 2 3 49 50 51
+---+---+---+---+-- --+---+---+-- --+---+---+---+
| | | X | X | .... | X | | .... | | | |
+---+---+---+---+-- --+---+---+-- --+---+---+---+
next size-1 size
0 1 2 3 49 50 51
+---+---+---+---+-- --+---+---+-- --+---+---+---+
| X | X | X | X | .... | X | | .... | | | |
+---+---+---+---+-- --+---+---+-- --+---+---+---+
disc-1 discards
One array holds the cards that have not yet been drawn,
with a 'next' subscript indicating where the next card is.
All the collected cards are placed into a discard pile.
Once all the cards are drawn from the draw stack, then
treat the discard pile as the new draw pile. That is,
the size of the deck is equal to the number discarded into it,
and the next card to draw will be from position 0.
Primary Advantage: no shifting of cards required (very fast)
Primary Drawback: may be a little tricky switching the discard
pile into a draw pile, especially in
the middle of a big war
III: The Circular Array
0 1 2 3 49 50 51 0 1 2 3
+---+---+---+---+-- --+---+---+-- --+---+---+---+---+---+---+---+--
| | | X | X | .... | X | | .... | | | | | | | | ....
+---+---+---+---+-- --+---+---+-- --+---+---+---+---+---+---+---+--
next last-1 last
Pretend that the first element of the array immediately follows
the last element of the array. This can be done by assuring that
every time you increase any subscript, that it gets reset to zero
whenever it reaches 52.
When a card is drawn, increase the subscript that says where to draw next.
When a card is added to the deck, increase the subscript that says where
the last card is.
Primary Advantage: no shifting of cards, or manipulating multiple arrays
Primary Disadvantage: just a mental adjustment to this apparently infinite array
In CMPSC 122, you will encounter the "Double-Ended Queue",
which is most efficiently implemented as a circular array, such as this one.
IMPLEMENTATION AND DEBUGGING HINT
It may be simpler to write and test your program if you first assume that there will be no ties. This will allow you to test the ability to draw a card and collect a card successfully, and also to see that the program runs smoothly.
Then once you are confident that much of the program works correctly, you can add the feature that handles the larger battles.
OTHER DESIGN NOTES
Shuffling a deck is simply an application of a random number generator. Initialize an array to a series of known values (i.e. known to have distinct values for each card); then visit each subscript in turn and exchange that array element with some element randomly chosen from anywhere in the array. Every card will have been moved somewhere -- and no one should have any idea where.
Shuffling would be a necessary first step to this program. (If you don't believe me, see how your program runs if you don't shuffle.)
To keep the user interface simple, implement the following between each battle:
char input[3]; // not much input here
cin.getline( input, 3 );
This will proceed with a simple carriage return, even if no value is typed. No prompt is required, to keep the display uncluttered.
You could then also check the first character of this input for some value (like the letter 'q') to allow for the program to end early. I do not expect the TA to run every program until one player has completely won all 52 cards.
EXTRA CREDIT OPTION
Allow two ways to run this program (selected by user input)
Verbose Mode
As above, displaying each battle in turn, showing the cards, then accepting a carriage return to go to the next battle.
Silent Mode
Runs an entire game from beginning to the end, with no input or output, simply reporting how many battles were played along the way.
For extra value, allow this silent mode to play as many games as a user desires, reporting the length of each game.
The Wikipedia page suggests that the average number of battles per game is near 250. (Don't require 250 carriage returns to determine that!)
also here is a sample of what he gave us
Sample Functional Design for WAR!
Main Program:
Provides an overall user interface, especially for purposes
of the Extra Credit option (which will not be described below)
Function playWar:
Simulates the two-player game of WAR!
To facilitate use of the deck operations described below,
every skirmish will have a battle pile for each player.
Draw from your deck into the battle pile, and then move
those cards into the deck when a winner is determined.
Return Value: number of skirmishes required in complete game
Calls: shuffle, draw, collect
Function shuffle:
Creates and shuffles a deck of cards
Parameter: deck (output array) the deck of cards
Function draw: (simplest version)
Draws exactly one card a deck
Parameter: deck (in/out array) the deck to draw from
size (in/out integer) how many cards are in the deck
card (output integer) the card drawn
Function collect: (simplest version)
Collects cards from a battle pile into winner's deck
(This will be called twice to collect from both piles)
parameters: playerDeck (in/out array) the deck to collect into
size (in/out integer) size of that deck
rewards (input array) the cards to collect
rewardSize (input integer) how many to collect
Function printCardName:
Translates the numeric code for a card into a string for display
Uses two arrays representing the rank and suit of the card.
parameter: card the integer value representing the card
-----------------------------
Here are three feasible ways of handling player decks.
(The method chosen will affect the parameter list required to use them)
I: The simplest
0 1 2 3 49 50 51
+---+---+---+---+-- --+---+---+-- --+---+---+---+
| X | X | X | X | .... | X | | .... | | | |
+---+---+---+---+-- --+---+---+-- --+---+---+---+
size-1 size
Each deck is stored in an array (maximum 52 cards)
with the top card in position 0. Adding a card just
goes to the place indicated by size (which increases).
When a card is drawn, the rest are shifted one position
(1 to 0, 2 to 1, etc.)
Primary Advantage: simple to understand, only one array required
Primary Drawback: a lot of card shifting whenever a card is drawn
II: Maintaining a separate discard pile
0 1 2 3 49 50 51
+---+---+---+---+-- --+---+---+-- --+---+---+---+
| | | X | X | .... | X | | .... | | | |
+---+---+---+---+-- --+---+---+-- --+---+---+---+
next size-1 size
0 1 2 3 49 50 51
+---+---+---+---+-- --+---+---+-- --+---+---+---+
| X | X | X | X | .... | X | | .... | | | |
+---+---+---+---+-- --+---+---+-- --+---+---+---+
disc-1 discards
One array holds the cards that have not yet been drawn,
with a 'next' subscript indicating where the next card is.
All the collected cards are placed into a discard pile.
Once all the card are drawn from the draw stack, then
treat the discard pile as the new draw pile. That is,
the size of the deck is equal to the number discarded into it,
and the next card to draw will be from position 0.
Primary Advantage: no shifting of cards required (very fast)
Primary Drawback: may be a little tricky switching the discard
pile into a draw pile, especially in
the middle of a big war
III: The Circular Array
0 1 2 3 49 50 51 0 1 2 3
+---+---+---+---+-- --+---+---+-- --+---+---+---+---+---+---+---+--
| | | X | X | .... | X | | .... | | | | | | | | ....
+---+---+---+---+-- --+---+---+-- --+---+---+---+---+---+---+---+--
next last-1 last
Pretend that the first element of the array immediately follows
the last element of the array. This can be done by assuring that
every time you increase any subscript, that it gets reset to zero
whenever it reaches 0.
When a card is drawn, increase the subscript that says where to draw next.
When a card is added to the deck, increase the subscript that says where
the last card is.
Primary Advantage: no shifting of cards, or manipulating multiple arrays
Primary Disadvantage: just a mental adjustment to this apparently infinite array
Thanks if you can help